1 package org
.argeo
.slc
.services
.impl
;
3 import java
.util
.ArrayList
;
6 import org
.apache
.commons
.logging
.Log
;
7 import org
.apache
.commons
.logging
.LogFactory
;
8 import org
.argeo
.slc
.dao
.runtime
.SlcAgentDescriptorDao
;
9 import org
.argeo
.slc
.runtime
.SlcAgent
;
10 import org
.argeo
.slc
.runtime
.SlcAgentDescriptor
;
11 import org
.argeo
.slc
.runtime
.SlcAgentFactory
;
12 import org
.argeo
.slc
.services
.AgentService
;
13 import org
.springframework
.beans
.factory
.DisposableBean
;
14 import org
.springframework
.beans
.factory
.InitializingBean
;
16 public class AgentServiceImpl
implements AgentService
, InitializingBean
,
18 private final static Log log
= LogFactory
.getLog(AgentServiceImpl
.class);
20 private final SlcAgentDescriptorDao slcAgentDescriptorDao
;
21 private final SlcAgentFactory agentFactory
;
23 private Long pingCycle
= 60000l;
25 private Boolean pingThreadActive
= true;
27 public AgentServiceImpl(SlcAgentDescriptorDao slcAgentDescriptorDao
,
28 SlcAgentFactory agentFactory
) {
29 this.slcAgentDescriptorDao
= slcAgentDescriptorDao
;
30 this.agentFactory
= agentFactory
;
33 public void register(SlcAgentDescriptor slcAgentDescriptor
) {
34 slcAgentDescriptorDao
.create(slcAgentDescriptor
);
35 log
.info("Registered agent #" + slcAgentDescriptor
.getUuid());
38 public void unregister(SlcAgentDescriptor slcAgentDescriptor
) {
39 slcAgentDescriptorDao
.delete(slcAgentDescriptor
);
40 log
.info("Unregistered agent #" + slcAgentDescriptor
.getUuid());
43 public void afterPropertiesSet() throws Exception
{
45 new PingThread().start();
48 public synchronized void destroy() throws Exception
{
49 pingThreadActive
= false;
53 public void setPingCycle(Long pingCycle
) {
54 this.pingCycle
= pingCycle
;
57 protected class PingThread
extends Thread
{
60 // FIXME: temporary hack so that the ping starts after the server
61 // has been properly started.
63 Thread
.sleep(5 * 1000);
64 } catch (InterruptedException e1
) {
68 log
.info("Start pinging agents.");
69 while (pingThreadActive
) {
70 List
<SlcAgentDescriptor
> lst
= slcAgentDescriptorDao
71 .listSlcAgentDescriptors();
72 List
<String
> agentIds
= new ArrayList
<String
>();
73 for (SlcAgentDescriptor ad
: lst
)
74 agentIds
.add(ad
.getUuid());
76 if (log
.isTraceEnabled())
77 log
.debug("Ping " + agentIds
.size() + " agent.");
78 for (String agentId
: agentIds
) {
79 SlcAgent agent
= agentFactory
.getAgent(agentId
);
81 log
.info("Agent " + agentId
+ " did not reply to ping,"
82 + " removing its descriptor...");
83 slcAgentDescriptorDao
.delete(agentId
);
87 lst
= slcAgentDescriptorDao
.listSlcAgentDescriptors();
88 agentIds
= new ArrayList
<String
>();
89 for (SlcAgentDescriptor ad
: lst
)
90 agentIds
.add(ad
.getUuid());
91 agentFactory
.pingAll(agentIds
);
93 synchronized (AgentServiceImpl
.this) {
95 AgentServiceImpl
.this.wait(pingCycle
);
96 } catch (InterruptedException e
) {
101 log
.info("Stopped pinging agents.");