2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.services
.impl
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
21 import java
.util
.concurrent
.Executor
;
23 import org
.apache
.commons
.logging
.Log
;
24 import org
.apache
.commons
.logging
.LogFactory
;
25 import org
.argeo
.slc
.dao
.runtime
.SlcAgentDescriptorDao
;
26 import org
.argeo
.slc
.runtime
.SlcAgent
;
27 import org
.argeo
.slc
.runtime
.SlcAgentDescriptor
;
28 import org
.argeo
.slc
.runtime
.SlcAgentFactory
;
29 import org
.argeo
.slc
.services
.AgentService
;
30 import org
.springframework
.beans
.factory
.DisposableBean
;
31 import org
.springframework
.beans
.factory
.InitializingBean
;
33 public class AgentServiceImpl
implements AgentService
, InitializingBean
,
35 private final static Log log
= LogFactory
.getLog(AgentServiceImpl
.class);
37 private final SlcAgentDescriptorDao slcAgentDescriptorDao
;
38 private final SlcAgentFactory agentFactory
;
40 private Executor systemExecutor
;
42 private Long pingCycle
= 20000l;
44 private Boolean pingThreadActive
= true;
46 public AgentServiceImpl(SlcAgentDescriptorDao slcAgentDescriptorDao
,
47 SlcAgentFactory agentFactory
) {
48 this.slcAgentDescriptorDao
= slcAgentDescriptorDao
;
49 this.agentFactory
= agentFactory
;
52 public void register(SlcAgentDescriptor slcAgentDescriptor
) {
53 if (slcAgentDescriptorDao
.getAgentDescriptor(slcAgentDescriptor
55 slcAgentDescriptorDao
.create(slcAgentDescriptor
);
56 log
.info("Registered agent #" + slcAgentDescriptor
.getUuid());
59 public void unregister(SlcAgentDescriptor slcAgentDescriptor
) {
60 slcAgentDescriptorDao
.delete(slcAgentDescriptor
);
61 log
.info("Unregistered agent #" + slcAgentDescriptor
.getUuid());
64 public void afterPropertiesSet() throws Exception
{
66 // new PingThread().start();
68 Thread authenticatedThread
= new Thread("SLC Agents Ping") {
70 systemExecutor
.execute(new AgentsPing());
73 authenticatedThread
.start();
78 public synchronized void destroy() throws Exception
{
79 pingThreadActive
= false;
83 public void setPingCycle(Long pingCycle
) {
84 this.pingCycle
= pingCycle
;
87 public void setSystemExecutor(Executor securityService
) {
88 this.systemExecutor
= securityService
;
91 protected class AgentsPing
implements Runnable
{
94 // FIXME: temporary hack so that the ping starts after the server
95 // has been properly started.
97 Thread
.sleep(10 * 1000);
98 } catch (InterruptedException e1
) {
102 log
.info("Start pinging agents.");
103 while (pingThreadActive
) {
104 List
<SlcAgentDescriptor
> lst
= slcAgentDescriptorDao
105 .listSlcAgentDescriptors();
106 List
<String
> agentIds
= new ArrayList
<String
>();
107 for (SlcAgentDescriptor ad
: lst
)
108 agentIds
.add(ad
.getUuid());
110 if (log
.isTraceEnabled())
111 log
.trace("Ping " + agentIds
.size() + " agent(s).");
112 for (String agentId
: agentIds
) {
113 SlcAgent agent
= agentFactory
.getAgent(agentId
);
115 log
.info("Agent " + agentId
+ " did not reply to ping,"
116 + " removing its descriptor...");
117 slcAgentDescriptorDao
.delete(agentId
);
121 lst
= slcAgentDescriptorDao
.listSlcAgentDescriptors();
122 agentIds
= new ArrayList
<String
>();
123 for (SlcAgentDescriptor ad
: lst
)
124 agentIds
.add(ad
.getUuid());
125 agentFactory
.pingAll(agentIds
);
127 synchronized (AgentServiceImpl
.this) {
129 AgentServiceImpl
.this.wait(pingCycle
);
130 } catch (InterruptedException e
) {
135 log
.info("Stopped pinging agents.");