X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.server%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fservices%2Fimpl%2Fruntime%2FAgentServiceImpl.java;h=021f573645f78f98540ccee7b11d4048cd2df7f3;hb=44dd5750650b46d9979b4e06e4cc76c0b0003f4f;hp=f6566c82832788c8e7fbddefada0ee6dffe56c8c;hpb=ab459c7e28f29c0695d159f7971337e3b5884c6e;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/services/impl/runtime/AgentServiceImpl.java b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/services/impl/runtime/AgentServiceImpl.java index f6566c828..021f57364 100644 --- a/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/services/impl/runtime/AgentServiceImpl.java +++ b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/services/impl/runtime/AgentServiceImpl.java @@ -1,17 +1,33 @@ package org.argeo.slc.services.impl.runtime; +import java.util.ArrayList; +import java.util.List; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.slc.dao.runtime.SlcAgentDescriptorDao; +import org.argeo.slc.runtime.SlcAgent; import org.argeo.slc.runtime.SlcAgentDescriptor; +import org.argeo.slc.runtime.SlcAgentFactory; +import org.argeo.slc.services.runtime.AgentService; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; -public class AgentServiceImpl { +public class AgentServiceImpl implements AgentService, InitializingBean, + DisposableBean { private final static Log log = LogFactory.getLog(AgentServiceImpl.class); private final SlcAgentDescriptorDao slcAgentDescriptorDao; + private final SlcAgentFactory agentFactory; + + private Long pingCycle = 60000l; - public AgentServiceImpl(SlcAgentDescriptorDao slcAgentDescriptorDao) { + private Boolean pingThreadActive = true; + + public AgentServiceImpl(SlcAgentDescriptorDao slcAgentDescriptorDao, + SlcAgentFactory agentFactory) { this.slcAgentDescriptorDao = slcAgentDescriptorDao; + this.agentFactory = agentFactory; } public void register(SlcAgentDescriptor slcAgentDescriptor) { @@ -24,4 +40,57 @@ public class AgentServiceImpl { log.info("Unregistered agent #" + slcAgentDescriptor.getUuid()); } + public void afterPropertiesSet() throws Exception { + if (pingCycle > 0) + new PingThread().start(); + } + + public synchronized void destroy() throws Exception { + pingThreadActive = false; + notifyAll(); + } + + public void setPingCycle(Long pingCycle) { + this.pingCycle = pingCycle; + } + + protected class PingThread extends Thread { + public void run() { + log.info("Start pinging agents."); + while (pingThreadActive) { + List lst = slcAgentDescriptorDao + .listSlcAgentDescriptors(); + List agentIds = new ArrayList(); + for (SlcAgentDescriptor ad : lst) + agentIds.add(ad.getUuid()); + + if (log.isTraceEnabled()) + log.debug("Ping " + agentIds.size() + " agent."); + for (String agentId : agentIds) { + SlcAgent agent = agentFactory.getAgent(agentId); + if (!agent.ping()) { + log.info("Agent " + agentId + " did not reply to ping," + + " removing its descriptor..."); + slcAgentDescriptorDao.delete(agentId); + } + } + + lst = slcAgentDescriptorDao.listSlcAgentDescriptors(); + agentIds = new ArrayList(); + for (SlcAgentDescriptor ad : lst) + agentIds.add(ad.getUuid()); + agentFactory.pingAll(agentIds); + + synchronized (AgentServiceImpl.this) { + try { + AgentServiceImpl.this.wait(pingCycle); + } catch (InterruptedException e) { + // silent + } + } + } + log.info("Stopped pinging agents."); + } + + } }