1 package org
.argeo
.slc
.jms
;
3 import java
.net
.InetAddress
;
4 import java
.net
.UnknownHostException
;
8 import javax
.jms
.Destination
;
9 import javax
.jms
.JMSException
;
10 import javax
.jms
.Message
;
11 import javax
.jms
.MessageListener
;
13 import org
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.argeo
.slc
.SlcException
;
16 import org
.argeo
.slc
.core
.runtime
.DefaultAgent
;
17 import org
.argeo
.slc
.execution
.ExecutionModuleDescriptor
;
18 import org
.argeo
.slc
.msg
.ExecutionAnswer
;
19 import org
.argeo
.slc
.msg
.MsgConstants
;
20 import org
.argeo
.slc
.msg
.ReferenceList
;
21 import org
.argeo
.slc
.process
.SlcExecution
;
22 import org
.argeo
.slc
.runtime
.SlcAgentDescriptor
;
23 import org
.springframework
.beans
.factory
.DisposableBean
;
24 import org
.springframework
.beans
.factory
.InitializingBean
;
25 import org
.springframework
.jms
.JmsException
;
26 import org
.springframework
.jms
.core
.JmsTemplate
;
27 import org
.springframework
.jms
.core
.MessagePostProcessor
;
29 /** JMS based implementation of SLC Agent. */
30 public class JmsAgent
extends DefaultAgent
implements InitializingBean
,
31 DisposableBean
, MessageListener
{
32 public final static String PROPERTY_QUERY
= "query";
33 public final static String QUERY_PING_ALL
= "pingAll";
35 private final static Log log
= LogFactory
.getLog(JmsAgent
.class);
37 private final SlcAgentDescriptor agentDescriptor
;
38 private JmsTemplate jmsTemplate
;
39 private Destination agentRegister
;
40 private Destination agentUnregister
;
42 private Destination responseDestination
;
46 agentDescriptor
= new SlcAgentDescriptor();
47 agentDescriptor
.setUuid(UUID
.randomUUID().toString());
48 agentDescriptor
.setHost(InetAddress
.getLocalHost().getHostName());
49 } catch (UnknownHostException e
) {
50 throw new SlcException("Unable to create agent descriptor.", e
);
54 public void afterPropertiesSet() throws Exception
{
56 jmsTemplate
.convertAndSend(agentRegister
, agentDescriptor
);
57 log
.info("Agent #" + agentDescriptor
.getUuid() + " registered to "
59 } catch (JmsException e
) {
61 .warn("Could not register agent "
62 + agentDescriptor
.getUuid()
65 + ". The agent will stay offline but will keep listening for a ping all sent by server.");
66 if (log
.isTraceEnabled())
67 log
.debug("Original error.", e
);
71 public void destroy() throws Exception
{
73 jmsTemplate
.convertAndSend(agentUnregister
, agentDescriptor
);
74 log
.info("Agent #" + agentDescriptor
.getUuid()
75 + " unregistered from " + agentUnregister
);
76 } catch (JmsException e
) {
77 log
.warn("Could not unregister agent " + agentDescriptor
.getUuid()
78 + ": " + e
.getMessage());
79 if (log
.isTraceEnabled())
80 log
.debug("Original error.", e
);
84 public void setAgentRegister(Destination agentRegister
) {
85 this.agentRegister
= agentRegister
;
88 public void setAgentUnregister(Destination agentUnregister
) {
89 this.agentUnregister
= agentUnregister
;
92 public String
getMessageSelector() {
93 String messageSelector
= "slc_agentId='" + agentDescriptor
.getUuid()
95 // if (log.isDebugEnabled())
96 // log.debug("Message selector: " + messageSelector);
97 return messageSelector
;
100 public void onMessage(final Message message
) {
102 final String correlationId
;
104 query
= message
.getStringProperty(PROPERTY_QUERY
);
105 correlationId
= message
.getJMSCorrelationID();
106 } catch (JMSException e1
) {
107 throw new SlcException("Cannot analyze incoming message " + message
);
110 final Object response
;
111 final Destination destinationSend
;
112 if (QUERY_PING_ALL
.equals(query
)) {
113 ReferenceList refList
= (ReferenceList
) convertFrom(message
);
114 if (!refList
.getReferences().contains(agentDescriptor
.getUuid())) {
115 response
= agentDescriptor
;
116 destinationSend
= agentRegister
;
117 log
.info("Agent #" + agentDescriptor
.getUuid()
118 + " registering to " + agentRegister
119 + " in reply to a " + QUERY_PING_ALL
+ " query");
124 response
= process(query
, message
);
125 destinationSend
= responseDestination
;
129 jmsTemplate
.convertAndSend(destinationSend
, response
,
130 new MessagePostProcessor() {
131 public Message
postProcessMessage(Message messageToSend
)
132 throws JMSException
{
133 messageToSend
.setStringProperty(PROPERTY_QUERY
, query
);
134 messageToSend
.setStringProperty(
135 MsgConstants
.PROPERTY_SLC_AGENT_ID
,
136 agentDescriptor
.getUuid());
137 messageToSend
.setJMSCorrelationID(correlationId
);
138 return messageToSend
;
141 if (log
.isTraceEnabled())
142 log
.debug("Sent response to query '" + query
143 + "' with correlationId " + correlationId
);
146 /** @return response */
147 public Object
process(String query
, Message message
) {
149 if ("getExecutionModuleDescriptor".equals(query
)) {
150 String moduleName
= message
.getStringProperty("moduleName");
151 String version
= message
.getStringProperty("version");
152 return getExecutionModuleDescriptor(moduleName
, version
);
153 } else if ("listExecutionModuleDescriptors".equals(query
)) {
155 List
<ExecutionModuleDescriptor
> lst
= listExecutionModuleDescriptors();
156 SlcAgentDescriptor agentDescriptorToSend
= new SlcAgentDescriptor(
158 agentDescriptorToSend
.setModuleDescriptors(lst
);
159 return agentDescriptorToSend
;
160 } else if ("runSlcExecution".equals(query
)) {
161 final SlcExecution slcExecution
= (SlcExecution
) convertFrom(message
);
164 runSlcExecution(slcExecution
);
167 return ExecutionAnswer
.ok("Execution started on agent "
168 + agentDescriptor
.getUuid());
169 } else if ("ping".equals(query
)) {
170 return ExecutionAnswer
.ok("Agent " + agentDescriptor
.getUuid()
173 throw new SlcException("Unsupported query " + query
);
175 } catch (Exception e
) {
176 log
.error("Processing of query " + query
+ " failed", e
);
177 return ExecutionAnswer
.error(e
);
181 protected Object
convertFrom(Message message
) {
183 return jmsTemplate
.getMessageConverter().fromMessage(message
);
184 } catch (JMSException e
) {
185 throw new SlcException("Cannot convert message", e
);
189 public void setResponseDestination(Destination responseDestination
) {
190 this.responseDestination
= responseDestination
;
193 public void setJmsTemplate(JmsTemplate jmsTemplate
) {
194 this.jmsTemplate
= jmsTemplate
;