1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.jms; |
12 | |
|
13 | |
import org.mule.api.MuleException; |
14 | |
import org.mule.api.MuleRuntimeException; |
15 | |
import org.mule.api.construct.FlowConstruct; |
16 | |
import org.mule.api.endpoint.InboundEndpoint; |
17 | |
import org.mule.api.lifecycle.CreateException; |
18 | |
import org.mule.api.lifecycle.LifecycleException; |
19 | |
import org.mule.api.transaction.Transaction; |
20 | |
import org.mule.api.transaction.TransactionException; |
21 | |
import org.mule.api.transport.Connector; |
22 | |
import org.mule.config.i18n.MessageFactory; |
23 | |
import org.mule.transaction.TransactionCollection; |
24 | |
import org.mule.transport.AbstractMessageReceiver; |
25 | |
import org.mule.transport.AbstractReceiverWorker; |
26 | |
import org.mule.transport.ConnectException; |
27 | |
import org.mule.transport.jms.filters.JmsSelectorFilter; |
28 | |
import org.mule.transport.jms.redelivery.RedeliveryHandler; |
29 | |
import org.mule.util.ClassUtils; |
30 | |
|
31 | |
import java.util.ArrayList; |
32 | |
import java.util.Iterator; |
33 | |
import java.util.List; |
34 | |
|
35 | |
import javax.jms.Destination; |
36 | |
import javax.jms.JMSException; |
37 | |
import javax.jms.Message; |
38 | |
import javax.jms.MessageConsumer; |
39 | |
import javax.jms.MessageListener; |
40 | |
import javax.jms.Session; |
41 | |
import javax.resource.spi.work.WorkException; |
42 | |
|
43 | |
import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList; |
44 | |
|
45 | |
import org.apache.commons.logging.Log; |
46 | |
import org.apache.commons.logging.LogFactory; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 0 | public class MultiConsumerJmsMessageReceiver extends AbstractMessageReceiver |
54 | |
{ |
55 | |
protected final List<SubReceiver> consumers; |
56 | |
|
57 | |
protected final int receiversCount; |
58 | |
|
59 | |
private final JmsConnector jmsConnector; |
60 | |
|
61 | |
public MultiConsumerJmsMessageReceiver(Connector connector, FlowConstruct flowConstruct, InboundEndpoint endpoint) |
62 | |
throws CreateException |
63 | |
{ |
64 | 0 | super(connector, flowConstruct, endpoint); |
65 | |
|
66 | 0 | jmsConnector = (JmsConnector) connector; |
67 | |
|
68 | 0 | final boolean isTopic = jmsConnector.getTopicResolver().isTopic(endpoint, true); |
69 | 0 | if (isTopic && jmsConnector.getNumberOfConsumers() != 1) |
70 | |
{ |
71 | 0 | if (logger.isInfoEnabled()) |
72 | |
{ |
73 | 0 | logger.info("Destination " + getEndpoint().getEndpointURI() + " is a topic, but " + jmsConnector.getNumberOfConsumers() + |
74 | |
" receivers have been requested. Will configure only 1."); |
75 | |
} |
76 | 0 | receiversCount = 1; |
77 | |
} |
78 | |
else |
79 | |
{ |
80 | 0 | receiversCount = jmsConnector.getNumberOfConsumers(); |
81 | |
} |
82 | 0 | if (logger.isDebugEnabled()) |
83 | |
{ |
84 | 0 | logger.debug("Creating " + receiversCount + " sub-receivers for " + endpoint.getEndpointURI()); |
85 | |
} |
86 | |
|
87 | 0 | consumers = new CopyOnWriteArrayList(); |
88 | 0 | } |
89 | |
|
90 | |
@Override |
91 | |
protected void doStart() throws MuleException |
92 | |
{ |
93 | 0 | logger.debug("doStart()"); |
94 | |
SubReceiver sub; |
95 | 0 | for (Iterator<SubReceiver> it = consumers.iterator(); it.hasNext();) |
96 | |
{ |
97 | 0 | sub = it.next(); |
98 | 0 | sub.doStart(); |
99 | |
} |
100 | 0 | } |
101 | |
|
102 | |
@Override |
103 | |
protected void doStop() throws MuleException |
104 | |
{ |
105 | 0 | logger.debug("doStop()"); |
106 | 0 | if (consumers != null) |
107 | |
{ |
108 | |
SubReceiver sub; |
109 | 0 | for (Iterator<SubReceiver> it = consumers.iterator(); it.hasNext();) |
110 | |
{ |
111 | 0 | sub = it.next(); |
112 | 0 | sub.doStop(true); |
113 | |
} |
114 | |
} |
115 | 0 | } |
116 | |
|
117 | |
@Override |
118 | |
protected void doConnect() throws Exception |
119 | |
{ |
120 | 0 | logger.debug("doConnect()"); |
121 | |
|
122 | 0 | if (!consumers.isEmpty()) |
123 | |
{ |
124 | 0 | throw new IllegalStateException("List should be empty, there may be a concurrency issue here (see EE-1275)"); |
125 | |
} |
126 | |
|
127 | |
SubReceiver sub; |
128 | 0 | for (int i = 0; i < receiversCount; i++) |
129 | |
{ |
130 | 0 | sub = new SubReceiver(); |
131 | 0 | sub.doConnect(); |
132 | 0 | consumers.add(sub); |
133 | |
} |
134 | 0 | } |
135 | |
|
136 | |
@Override |
137 | |
protected void doDisconnect() throws Exception |
138 | |
{ |
139 | 0 | logger.debug("doDisconnect()"); |
140 | |
|
141 | |
SubReceiver sub; |
142 | 0 | for (Iterator<SubReceiver> it = consumers.iterator(); it.hasNext();) |
143 | |
{ |
144 | 0 | sub = it.next(); |
145 | |
try |
146 | |
{ |
147 | 0 | sub.doDisconnect(); |
148 | 0 | } |
149 | |
finally |
150 | |
{ |
151 | 0 | sub = null; |
152 | 0 | } |
153 | |
} |
154 | 0 | consumers.clear(); |
155 | 0 | } |
156 | |
|
157 | |
@Override |
158 | |
protected void doDispose() |
159 | |
{ |
160 | 0 | logger.debug("doDispose()"); |
161 | 0 | } |
162 | |
|
163 | |
@Override |
164 | |
protected boolean isDoStartMustFollowDoConnect() |
165 | |
{ |
166 | 0 | return true; |
167 | |
} |
168 | |
|
169 | 0 | private class SubReceiver implements MessageListener |
170 | |
{ |
171 | 0 | private final Log subLogger = LogFactory.getLog(getClass()); |
172 | |
|
173 | |
private volatile Session session; |
174 | |
private volatile MessageConsumer consumer; |
175 | |
|
176 | |
protected volatile boolean connected; |
177 | |
protected volatile boolean started; |
178 | |
|
179 | |
protected void doConnect() throws MuleException |
180 | |
{ |
181 | 0 | subLogger.debug("SUB doConnect()"); |
182 | |
try |
183 | |
{ |
184 | 0 | createConsumer(); |
185 | |
} |
186 | 0 | catch (Exception e) |
187 | |
{ |
188 | 0 | throw new LifecycleException(e, this); |
189 | 0 | } |
190 | 0 | connected = true; |
191 | 0 | } |
192 | |
|
193 | |
protected void doDisconnect() throws MuleException |
194 | |
{ |
195 | 0 | subLogger.debug("SUB doDisconnect()"); |
196 | 0 | if (started) |
197 | |
{ |
198 | 0 | doStop(true); |
199 | |
} |
200 | 0 | closeConsumer(); |
201 | 0 | connected = false; |
202 | 0 | } |
203 | |
|
204 | |
protected void closeConsumer() |
205 | |
{ |
206 | 0 | jmsConnector.closeQuietly(consumer); |
207 | 0 | consumer = null; |
208 | 0 | jmsConnector.closeQuietly(session); |
209 | 0 | session = null; |
210 | 0 | } |
211 | |
|
212 | |
protected void doStart() throws MuleException |
213 | |
{ |
214 | 0 | subLogger.debug("SUB doStart()"); |
215 | 0 | if (!connected) |
216 | |
{ |
217 | 0 | doConnect(); |
218 | |
} |
219 | |
|
220 | |
try |
221 | |
{ |
222 | 0 | consumer.setMessageListener(this); |
223 | 0 | started = true; |
224 | |
} |
225 | 0 | catch (JMSException e) |
226 | |
{ |
227 | 0 | throw new LifecycleException(e, this); |
228 | 0 | } |
229 | 0 | } |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
protected void doStop(boolean force) throws MuleException |
237 | |
{ |
238 | 0 | subLogger.debug("SUB doStop()"); |
239 | |
|
240 | 0 | if (consumer != null) |
241 | |
{ |
242 | |
try |
243 | |
{ |
244 | 0 | consumer.setMessageListener(null); |
245 | 0 | started = false; |
246 | |
} |
247 | 0 | catch (JMSException e) |
248 | |
{ |
249 | 0 | if (force) |
250 | |
{ |
251 | 0 | logger.warn("Unable to cleanly stop subreceiver: " + e.getMessage()); |
252 | 0 | started = false; |
253 | |
} |
254 | |
else |
255 | |
{ |
256 | 0 | throw new LifecycleException(e, this); |
257 | |
} |
258 | 0 | } |
259 | |
} |
260 | 0 | } |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
protected void createConsumer() throws Exception |
266 | |
{ |
267 | 0 | subLogger.debug("SUB createConsumer()"); |
268 | |
|
269 | |
try |
270 | |
{ |
271 | 0 | JmsSupport jmsSupport = jmsConnector.getJmsSupport(); |
272 | 0 | boolean topic = jmsConnector.getTopicResolver().isTopic(endpoint, true); |
273 | |
|
274 | |
|
275 | 0 | if (session == null) |
276 | |
{ |
277 | 0 | session = jmsConnector.getSession(endpoint); |
278 | |
} |
279 | |
|
280 | |
|
281 | 0 | Destination dest = jmsSupport.createDestination(session, endpoint); |
282 | |
|
283 | |
|
284 | 0 | String selector = null; |
285 | 0 | if (endpoint.getFilter() != null && endpoint.getFilter() instanceof JmsSelectorFilter) |
286 | |
{ |
287 | 0 | selector = ((JmsSelectorFilter) endpoint.getFilter()).getExpression(); |
288 | |
} |
289 | |
else |
290 | |
{ |
291 | 0 | if (endpoint.getProperties() != null) |
292 | |
{ |
293 | |
|
294 | |
|
295 | 0 | selector = (String) endpoint.getProperties().get(JmsConstants.JMS_SELECTOR_PROPERTY); |
296 | |
} |
297 | |
} |
298 | 0 | String tempDurable = (String) endpoint.getProperties().get(JmsConstants.DURABLE_PROPERTY); |
299 | 0 | boolean durable = jmsConnector.isDurable(); |
300 | 0 | if (tempDurable != null) |
301 | |
{ |
302 | 0 | durable = Boolean.valueOf(tempDurable); |
303 | |
} |
304 | |
|
305 | |
|
306 | 0 | String durableName = (String) endpoint.getProperties().get(JmsConstants.DURABLE_NAME_PROPERTY); |
307 | 0 | if (durableName == null && durable && topic) |
308 | |
{ |
309 | 0 | durableName = "mule." + jmsConnector.getName() + "." + endpoint.getEndpointURI().getAddress(); |
310 | 0 | logger.debug("Jms Connector for this receiver is durable but no durable name has been specified. Defaulting to: " |
311 | |
+ durableName); |
312 | |
} |
313 | |
|
314 | |
|
315 | 0 | consumer = jmsSupport.createConsumer(session, dest, selector, jmsConnector.isNoLocal(), durableName, |
316 | |
topic, endpoint); |
317 | |
} |
318 | 0 | catch (JMSException e) |
319 | |
{ |
320 | 0 | throw new ConnectException(e, MultiConsumerJmsMessageReceiver.this); |
321 | 0 | } |
322 | 0 | } |
323 | |
|
324 | |
public void onMessage(final Message message) |
325 | |
{ |
326 | |
try |
327 | |
{ |
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | 0 | getWorkManager().doWork(new JmsWorker(message, MultiConsumerJmsMessageReceiver.this, this)); |
333 | |
} |
334 | 0 | catch (WorkException e) |
335 | |
{ |
336 | 0 | throw new MuleRuntimeException(MessageFactory.createStaticMessage( |
337 | |
"Couldn't submit a work item to the WorkManager"), e); |
338 | 0 | } |
339 | 0 | } |
340 | |
} |
341 | |
|
342 | |
protected class JmsWorker extends AbstractReceiverWorker |
343 | |
{ |
344 | |
private final SubReceiver subReceiver; |
345 | |
|
346 | |
public JmsWorker(Message message, AbstractMessageReceiver receiver, SubReceiver subReceiver) |
347 | 0 | { |
348 | 0 | super(new ArrayList<Object>(1), receiver); |
349 | 0 | this.subReceiver = subReceiver; |
350 | 0 | messages.add(message); |
351 | 0 | } |
352 | |
|
353 | |
@Override |
354 | |
protected Object preProcessMessage(Object message) throws Exception |
355 | |
{ |
356 | 0 | Message m = (Message) message; |
357 | |
|
358 | 0 | if (logger.isDebugEnabled()) |
359 | |
{ |
360 | 0 | logger.debug("Message received it is of type: " + |
361 | |
ClassUtils.getSimpleName(message.getClass())); |
362 | 0 | if (m.getJMSDestination() != null) |
363 | |
{ |
364 | 0 | logger.debug("Message received on " + m.getJMSDestination() + " (" |
365 | |
+ m.getJMSDestination().getClass().getName() + ")"); |
366 | |
} |
367 | |
else |
368 | |
{ |
369 | 0 | logger.debug("Message received on unknown destination"); |
370 | |
} |
371 | 0 | logger.debug("Message CorrelationId is: " + m.getJMSCorrelationID()); |
372 | 0 | logger.debug("Jms Message Id is: " + m.getJMSMessageID()); |
373 | |
} |
374 | |
|
375 | 0 | if (m.getJMSRedelivered()) |
376 | |
{ |
377 | |
|
378 | 0 | RedeliveryHandler redeliveryHandler = jmsConnector.getRedeliveryHandlerFactory().create(); |
379 | 0 | redeliveryHandler.setConnector(jmsConnector); |
380 | 0 | if (logger.isDebugEnabled()) |
381 | |
{ |
382 | 0 | logger.debug("Message with correlationId: " + m.getJMSCorrelationID() |
383 | |
+ " has redelivered flag set, handing off to Redelivery Handler"); |
384 | |
} |
385 | 0 | redeliveryHandler.handleRedelivery(m, receiver.getEndpoint(), receiver.getFlowConstruct()); |
386 | |
} |
387 | 0 | return m; |
388 | |
|
389 | |
} |
390 | |
|
391 | |
@Override |
392 | |
protected void bindTransaction(Transaction tx) throws TransactionException |
393 | |
{ |
394 | 0 | if (tx instanceof JmsTransaction || tx instanceof TransactionCollection) |
395 | |
{ |
396 | 0 | if (logger.isDebugEnabled()) |
397 | |
{ |
398 | 0 | logger.debug("Binding " + subReceiver.session + " to " + jmsConnector.getConnection()); |
399 | |
} |
400 | 0 | tx.bindResource(jmsConnector.getConnection(), subReceiver.session); |
401 | |
} |
402 | |
else |
403 | |
{ |
404 | 0 | if (tx instanceof JmsClientAcknowledgeTransaction) |
405 | |
{ |
406 | |
|
407 | |
|
408 | |
|
409 | 0 | ((JmsClientAcknowledgeTransaction) tx).setMessage((Message) messages.get(0)); |
410 | |
} |
411 | |
} |
412 | 0 | } |
413 | |
} |
414 | |
|
415 | |
} |