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
.jms
;
19 import java
.util
.List
;
20 import java
.util
.UUID
;
22 import javax
.jms
.Destination
;
23 import javax
.jms
.JMSException
;
24 import javax
.jms
.Message
;
25 import javax
.jms
.Session
;
26 import javax
.jms
.TextMessage
;
28 import org
.apache
.commons
.logging
.Log
;
29 import org
.apache
.commons
.logging
.LogFactory
;
30 import org
.argeo
.slc
.SlcException
;
31 import org
.argeo
.slc
.execution
.ExecutionModuleDescriptor
;
32 import org
.argeo
.slc
.msg
.ExecutionAnswer
;
33 import org
.argeo
.slc
.msg
.MsgConstants
;
34 import org
.argeo
.slc
.process
.SlcExecution
;
35 import org
.argeo
.slc
.runtime
.SlcAgent
;
36 import org
.argeo
.slc
.runtime
.SlcAgentDescriptor
;
37 import org
.springframework
.jms
.core
.JmsTemplate
;
38 import org
.springframework
.jms
.core
.MessageCreator
;
40 public class JmsAgentProxy
implements SlcAgent
{
41 private final static Log log
= LogFactory
.getLog(JmsAgentProxy
.class);
43 private final String agentUuid
;
44 private final Destination requestDestination
;
45 private final Destination responseDestination
;
46 private final JmsTemplate jmsTemplate
;
48 public JmsAgentProxy(String agentUuid
, Destination requestDestination
,
49 Destination responseDestination
, JmsTemplate jmsTemplate
) {
50 this.agentUuid
= agentUuid
;
51 this.requestDestination
= requestDestination
;
52 this.responseDestination
= responseDestination
;
53 this.jmsTemplate
= jmsTemplate
;
56 public ExecutionModuleDescriptor
getExecutionModuleDescriptor(
57 final String moduleName
, final String version
) {
58 return (ExecutionModuleDescriptor
) sendReceive(new AgentMC(
59 "getExecutionModuleDescriptor") {
60 public void setArguments(Message message
) throws JMSException
{
61 message
.setStringProperty("moduleName", moduleName
);
62 message
.setStringProperty("version", version
);
67 public List
<ExecutionModuleDescriptor
> listExecutionModuleDescriptors() {
68 return ((SlcAgentDescriptor
) sendReceive(new AgentMC(
69 "listExecutionModuleDescriptors"))).getModuleDescriptors();
72 public void runSlcExecution(SlcExecution slcExecution
) {
73 sendReceive(new AgentMC("runSlcExecution", slcExecution
));
76 public boolean ping() {
77 Object response
= sendReceive(new AgentMC("ping"), false);
81 ExecutionAnswer answer
= (ExecutionAnswer
) response
;
82 return ExecutionAnswer
.OK
.equals(answer
.getStatus());
86 protected Object
sendReceive(AgentMC messageCreator
) {
87 long begin
= System
.currentTimeMillis();
90 res
= sendReceive(messageCreator
, true);
92 if (log
.isTraceEnabled())
93 log
.trace("To agent #" + agentUuid
+ " in "
94 + (System
.currentTimeMillis() - begin
) + " ms, query '"
95 + messageCreator
.getQuery() + "', correlationId "
96 + messageCreator
.getCorrelationId());
102 * @param timeoutException
103 * if true throws an exception if reception timeouted, else
106 protected Object
sendReceive(AgentMC messageCreator
,
107 boolean timeoutException
) {
108 String correlationId
= UUID
.randomUUID().toString();
109 messageCreator
.setCorrelationId(correlationId
);
110 send(messageCreator
);
112 Object response
= processResponse(messageCreator
, timeoutException
);
114 if (response
instanceof ExecutionAnswer
) {
115 ExecutionAnswer answer
= (ExecutionAnswer
) response
;
116 if (ExecutionAnswer
.ERROR
.equals(answer
.getStatus()))
117 throw new SlcException("Execution of '"
118 + messageCreator
.getQuery() + "' failed on the agent "
119 + agentUuid
+ ": " + answer
.getMessage()
120 + " (correlationId=" + correlationId
+ ")");
128 protected void send(AgentMC messageCreator
) {
129 jmsTemplate
.send(requestDestination
, messageCreator
);
132 protected Object
processResponse(AgentMC messageCreator
,
133 boolean timeoutException
) {
134 String correlationId
= messageCreator
.getCorrelationId();
135 String query
= messageCreator
.getQuery();
136 Message responseMsg
= null;
138 responseMsg
= jmsTemplate
.receiveSelected(responseDestination
,
139 "JMSCorrelationID='" + correlationId
+ "'");
140 } catch (Exception e
) {
141 throw new SlcException("Could not receive response from agent "
142 + agentUuid
+ " with correlationId " + correlationId
143 + " (query '" + query
+ "')", e
);
146 if (responseMsg
== null) {// timeout
147 if (timeoutException
)
148 throw new SlcException("TIMEOUT: Query '" + query
+ "'"
149 + " with correlationId " + correlationId
150 + " sent to agent " + agentUuid
+ " timed out.");
156 return fromMessage(responseMsg
);
157 } catch (Exception e
) {
158 throw new SlcException("Could not convert response from agent "
159 + agentUuid
+ " with correlationId " + correlationId
160 + " (query '" + query
+ "')", e
);
164 protected Object
fromMessage(Message message
) throws JMSException
{
165 return jmsTemplate
.getMessageConverter().fromMessage(message
);
168 protected Message
toMessage(Object obj
, Session session
)
169 throws JMSException
{
170 return jmsTemplate
.getMessageConverter().toMessage(obj
, session
);
173 protected class AgentMC
implements MessageCreator
{
174 private final String query
;
175 private Object body
= null;
176 private String correlationId
;
178 public AgentMC(String query
) {
182 public AgentMC(String query
, Object body
) {
187 public final Message
createMessage(Session session
) throws JMSException
{
188 if (agentUuid
== null)
189 throw new SlcException("Agent UUID not set");
190 if (correlationId
== null)
191 throw new SlcException("JMSCorrelationID not set");
194 msg
= session
.createTextMessage();
196 msg
= toMessage(body
, session
);
198 .setStringProperty(MsgConstants
.PROPERTY_SLC_AGENT_ID
,
200 msg
.setStringProperty(JmsAgent
.PROPERTY_QUERY
, query
);
201 msg
.setJMSCorrelationID(correlationId
);
203 if (msg
instanceof TextMessage
) {
204 TextMessage textMessage
= (TextMessage
) msg
;
205 if (textMessage
.getText() == null) {
206 // TODO: remove workaround when upgrading to ActiveMQ 5.3
208 // https://issues.apache.org/activemq/browse/AMQ-2046
209 textMessage
.setText("");
215 protected void setArguments(Message message
) throws JMSException
{
218 public String
getQuery() {
222 public String
getCorrelationId() {
223 return correlationId
;
226 public void setCorrelationId(String correlationId
) {
227 this.correlationId
= correlationId
;