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
.security
.ArgeoSecurityService
;
9 import org
.argeo
.slc
.dao
.runtime
.SlcAgentDescriptorDao
;
10 import org
.argeo
.slc
.runtime
.SlcAgent
;
11 import org
.argeo
.slc
.runtime
.SlcAgentDescriptor
;
12 import org
.argeo
.slc
.runtime
.SlcAgentFactory
;
13 import org
.argeo
.slc
.services
.AgentService
;
14 import org
.springframework
.beans
.factory
.DisposableBean
;
15 import org
.springframework
.beans
.factory
.InitializingBean
;
17 public class AgentServiceImpl
implements AgentService
, InitializingBean
,
19 private final static Log log
= LogFactory
.getLog(AgentServiceImpl
.class);
21 private final SlcAgentDescriptorDao slcAgentDescriptorDao
;
22 private final SlcAgentFactory agentFactory
;
24 private ArgeoSecurityService securityService
;
26 private Long pingCycle
= 20000l;
28 private Boolean pingThreadActive
= true;
30 public AgentServiceImpl(SlcAgentDescriptorDao slcAgentDescriptorDao
,
31 SlcAgentFactory agentFactory
) {
32 this.slcAgentDescriptorDao
= slcAgentDescriptorDao
;
33 this.agentFactory
= agentFactory
;
36 public void register(SlcAgentDescriptor slcAgentDescriptor
) {
37 if (slcAgentDescriptorDao
.getAgentDescriptor(slcAgentDescriptor
39 slcAgentDescriptorDao
.create(slcAgentDescriptor
);
40 log
.info("Registered agent #" + slcAgentDescriptor
.getUuid());
43 public void unregister(SlcAgentDescriptor slcAgentDescriptor
) {
44 slcAgentDescriptorDao
.delete(slcAgentDescriptor
);
45 log
.info("Unregistered agent #" + slcAgentDescriptor
.getUuid());
48 public void afterPropertiesSet() throws Exception
{
50 // new PingThread().start();
52 Thread authenticatedThread
= new Thread(securityService
53 .wrapWithSystemAuthentication(new AgentsPing()),
55 authenticatedThread
.start();
60 public synchronized void destroy() throws Exception
{
61 pingThreadActive
= false;
65 public void setPingCycle(Long pingCycle
) {
66 this.pingCycle
= pingCycle
;
69 public void setSecurityService(ArgeoSecurityService securityService
) {
70 this.securityService
= securityService
;
73 protected class AgentsPing
implements Runnable
{
76 // FIXME: temporary hack so that the ping starts after the server
77 // has been properly started.
79 Thread
.sleep(10 * 1000);
80 } catch (InterruptedException e1
) {
84 log
.info("Start pinging agents.");
85 while (pingThreadActive
) {
86 List
<SlcAgentDescriptor
> lst
= slcAgentDescriptorDao
87 .listSlcAgentDescriptors();
88 List
<String
> agentIds
= new ArrayList
<String
>();
89 for (SlcAgentDescriptor ad
: lst
)
90 agentIds
.add(ad
.getUuid());
92 if (log
.isTraceEnabled())
93 log
.debug("Ping " + agentIds
.size() + " agent.");
94 for (String agentId
: agentIds
) {
95 SlcAgent agent
= agentFactory
.getAgent(agentId
);
97 log
.info("Agent " + agentId
+ " did not reply to ping,"
98 + " removing its descriptor...");
99 slcAgentDescriptorDao
.delete(agentId
);
103 lst
= slcAgentDescriptorDao
.listSlcAgentDescriptors();
104 agentIds
= new ArrayList
<String
>();
105 for (SlcAgentDescriptor ad
: lst
)
106 agentIds
.add(ad
.getUuid());
107 agentFactory
.pingAll(agentIds
);
109 synchronized (AgentServiceImpl
.this) {
111 AgentServiceImpl
.this.wait(pingCycle
);
112 } catch (InterruptedException e
) {
117 log
.info("Stopped pinging agents.");