1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.jms; |
12 | |
|
13 | |
import org.mule.impl.MuleMessage; |
14 | |
import org.mule.providers.ConnectException; |
15 | |
import org.mule.providers.SingleAttemptConnectionStrategy; |
16 | |
import org.mule.providers.TransactedPollingMessageReceiver; |
17 | |
import org.mule.providers.jms.filters.JmsSelectorFilter; |
18 | |
import org.mule.transaction.TransactionCoordination; |
19 | |
import org.mule.umo.UMOComponent; |
20 | |
import org.mule.umo.UMOTransaction; |
21 | |
import org.mule.umo.endpoint.UMOEndpoint; |
22 | |
import org.mule.umo.lifecycle.InitialisationException; |
23 | |
import org.mule.umo.provider.UMOConnector; |
24 | |
import org.mule.umo.provider.UMOMessageAdapter; |
25 | |
import org.mule.util.ClassUtils; |
26 | |
import org.mule.util.MapUtils; |
27 | |
|
28 | |
import java.util.List; |
29 | |
|
30 | |
import javax.jms.Destination; |
31 | |
import javax.jms.JMSException; |
32 | |
import javax.jms.Message; |
33 | |
import javax.jms.MessageConsumer; |
34 | |
import javax.jms.Session; |
35 | |
|
36 | |
import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; |
37 | |
|
38 | |
public class XaTransactedJmsMessageReceiver extends TransactedPollingMessageReceiver |
39 | |
{ |
40 | |
public static final long DEFAULT_JMS_POLL_FREQUENCY = 100; |
41 | 0 | public static final TimeUnit DEFAULT_JMS_POLL_TIMEUNIT = TimeUnit.MILLISECONDS; |
42 | |
|
43 | |
protected final JmsConnector connector; |
44 | |
protected boolean reuseConsumer; |
45 | |
protected boolean reuseSession; |
46 | 0 | protected final ThreadContextLocal context = new ThreadContextLocal(); |
47 | |
protected final long timeout; |
48 | |
protected final RedeliveryHandler redeliveryHandler; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 0 | protected static class JmsThreadContext |
54 | |
{ |
55 | |
public Session session; |
56 | |
public MessageConsumer consumer; |
57 | |
} |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | 0 | protected static class ThreadContextLocal extends ThreadLocal |
63 | |
{ |
64 | |
public JmsThreadContext getContext() |
65 | |
{ |
66 | 0 | return (JmsThreadContext)get(); |
67 | |
} |
68 | |
|
69 | |
protected Object initialValue() |
70 | |
{ |
71 | 0 | return new JmsThreadContext(); |
72 | |
} |
73 | |
} |
74 | |
|
75 | |
public XaTransactedJmsMessageReceiver(UMOConnector umoConnector, UMOComponent component, UMOEndpoint endpoint) |
76 | |
throws InitialisationException |
77 | |
{ |
78 | 0 | super(umoConnector, component, endpoint); |
79 | |
|
80 | |
|
81 | 0 | this.setFrequency(DEFAULT_JMS_POLL_FREQUENCY); |
82 | 0 | this.setTimeUnit(DEFAULT_JMS_POLL_TIMEUNIT); |
83 | 0 | this.connector = (JmsConnector) umoConnector; |
84 | 0 | this.timeout = endpoint.getTransactionConfig().getTimeout(); |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | 0 | if (this.connectionStrategy instanceof SingleAttemptConnectionStrategy) |
90 | |
{ |
91 | 0 | this.reuseConsumer = true; |
92 | 0 | this.reuseSession = true; |
93 | |
} |
94 | |
|
95 | |
|
96 | 0 | this.reuseConsumer = MapUtils.getBooleanValue(endpoint.getProperties(), "reuseConsumer", |
97 | |
this.reuseConsumer); |
98 | 0 | this.reuseSession = MapUtils.getBooleanValue(endpoint.getProperties(), "reuseSession", |
99 | |
this.reuseSession); |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | 0 | final boolean topic = connector.getTopicResolver().isTopic(endpoint); |
106 | |
|
107 | |
|
108 | |
|
109 | 0 | this.setUseMultipleTransactedReceivers(!topic); |
110 | |
|
111 | |
try |
112 | |
{ |
113 | 0 | redeliveryHandler = this.connector.createRedeliveryHandler(); |
114 | 0 | redeliveryHandler.setConnector(this.connector); |
115 | |
} |
116 | 0 | catch (Exception e) |
117 | |
{ |
118 | 0 | throw new InitialisationException(e, this); |
119 | 0 | } |
120 | |
|
121 | 0 | } |
122 | |
|
123 | |
protected void doDispose() |
124 | |
{ |
125 | |
|
126 | 0 | } |
127 | |
|
128 | |
protected void doConnect() throws Exception |
129 | |
{ |
130 | 0 | if (connector.isConnected() && connector.isEagerConsumer()) |
131 | |
{ |
132 | 0 | createConsumer(); |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
} |
147 | 0 | } |
148 | |
|
149 | |
protected void doDisconnect() throws Exception |
150 | |
{ |
151 | 0 | if (connector.isConnected()) |
152 | |
{ |
153 | 0 | closeConsumer(true); |
154 | |
} |
155 | 0 | } |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public void poll() throws Exception |
161 | |
{ |
162 | |
try |
163 | |
{ |
164 | 0 | JmsThreadContext ctx = context.getContext(); |
165 | |
|
166 | 0 | if (ctx.consumer == null) |
167 | |
{ |
168 | 0 | createConsumer(); |
169 | |
} |
170 | |
|
171 | 0 | super.poll(); |
172 | |
} |
173 | 0 | catch (Exception e) |
174 | |
{ |
175 | |
|
176 | 0 | closeConsumer(true); |
177 | 0 | throw e; |
178 | |
} |
179 | |
finally |
180 | |
{ |
181 | |
|
182 | 0 | closeConsumer(false); |
183 | 0 | } |
184 | 0 | } |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
protected List getMessages() throws Exception |
192 | |
{ |
193 | |
|
194 | |
|
195 | 0 | JmsThreadContext ctx = context.getContext(); |
196 | |
|
197 | 0 | UMOTransaction tx = TransactionCoordination.getInstance().getTransaction(); |
198 | 0 | if (tx != null) |
199 | |
{ |
200 | 0 | tx.bindResource(connector.getConnection(), ctx.session); |
201 | |
} |
202 | |
|
203 | |
|
204 | 0 | Message message = null; |
205 | |
try |
206 | |
{ |
207 | 0 | message = ctx.consumer.receive(timeout); |
208 | |
} |
209 | 0 | catch (JMSException e) |
210 | |
{ |
211 | |
|
212 | 0 | if (!this.isConnected()) |
213 | 0 | { |
214 | |
|
215 | |
} |
216 | |
else |
217 | |
{ |
218 | 0 | throw e; |
219 | |
} |
220 | 0 | } |
221 | 0 | if (message == null) |
222 | |
{ |
223 | 0 | if (tx != null) |
224 | |
{ |
225 | 0 | tx.setRollbackOnly(); |
226 | |
} |
227 | 0 | return null; |
228 | |
} |
229 | 0 | message = connector.preProcessMessage(message, ctx.session); |
230 | |
|
231 | |
|
232 | 0 | if (logger.isDebugEnabled()) |
233 | |
{ |
234 | 0 | logger.debug("Message received it is of type: " + |
235 | |
ClassUtils.getSimpleName(message.getClass())); |
236 | 0 | if (message.getJMSDestination() != null) |
237 | |
{ |
238 | 0 | logger.debug("Message received on " + message.getJMSDestination() + " (" |
239 | |
+ message.getJMSDestination().getClass().getName() + ")"); |
240 | |
} |
241 | |
else |
242 | |
{ |
243 | 0 | logger.debug("Message received on unknown destination"); |
244 | |
} |
245 | 0 | logger.debug("Message CorrelationId is: " + message.getJMSCorrelationID()); |
246 | 0 | logger.debug("Jms Message Id is: " + message.getJMSMessageID()); |
247 | |
} |
248 | |
|
249 | 0 | if (message.getJMSRedelivered()) |
250 | |
{ |
251 | 0 | if (logger.isDebugEnabled()) |
252 | |
{ |
253 | 0 | logger.debug("Message with correlationId: " + message.getJMSCorrelationID() |
254 | |
+ " is redelivered. handing off to Exception Handler"); |
255 | |
} |
256 | 0 | redeliveryHandler.handleRedelivery(message); |
257 | |
} |
258 | |
|
259 | 0 | if (tx instanceof JmsClientAcknowledgeTransaction) |
260 | |
{ |
261 | 0 | tx.bindResource(message, null); |
262 | |
} |
263 | |
|
264 | 0 | UMOMessageAdapter adapter = connector.getMessageAdapter(message); |
265 | 0 | routeMessage(new MuleMessage(adapter)); |
266 | 0 | return null; |
267 | |
} |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
protected void processMessage(Object msg) throws Exception |
275 | |
{ |
276 | |
|
277 | |
|
278 | 0 | } |
279 | |
|
280 | |
protected void closeConsumer(boolean force) |
281 | |
{ |
282 | 0 | JmsThreadContext ctx = context.getContext(); |
283 | 0 | if (ctx == null) |
284 | |
{ |
285 | 0 | return; |
286 | |
} |
287 | |
|
288 | 0 | if (force || !reuseSession || !reuseConsumer) |
289 | |
{ |
290 | 0 | connector.closeQuietly(ctx.consumer); |
291 | 0 | ctx.consumer = null; |
292 | |
} |
293 | |
|
294 | |
|
295 | 0 | if (force || !reuseSession) |
296 | |
{ |
297 | 0 | connector.closeQuietly(ctx.session); |
298 | 0 | ctx.session = null; |
299 | |
} |
300 | 0 | } |
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
protected void createConsumer() throws Exception |
308 | |
{ |
309 | |
try |
310 | |
{ |
311 | 0 | JmsSupport jmsSupport = this.connector.getJmsSupport(); |
312 | 0 | JmsThreadContext ctx = context.getContext(); |
313 | |
|
314 | 0 | if (ctx.session == null) |
315 | |
{ |
316 | 0 | ctx.session = this.connector.getSession(endpoint); |
317 | |
} |
318 | |
|
319 | |
|
320 | 0 | final boolean topic = connector.getTopicResolver().isTopic(endpoint); |
321 | 0 | Destination dest = jmsSupport.createDestination(ctx.session, endpoint.getEndpointURI() |
322 | |
.getAddress(), topic); |
323 | |
|
324 | |
|
325 | 0 | String selector = null; |
326 | 0 | if (endpoint.getFilter() != null && endpoint.getFilter() instanceof JmsSelectorFilter) |
327 | |
{ |
328 | 0 | selector = ((JmsSelectorFilter)endpoint.getFilter()).getExpression(); |
329 | |
} |
330 | 0 | else if (endpoint.getProperties() != null) |
331 | |
{ |
332 | |
|
333 | |
|
334 | 0 | selector = (String)endpoint.getProperties().get(JmsConstants.JMS_SELECTOR_PROPERTY); |
335 | |
} |
336 | 0 | String tempDurable = (String)endpoint.getProperties().get("durable"); |
337 | 0 | boolean durable = connector.isDurable(); |
338 | 0 | if (tempDurable != null) |
339 | |
{ |
340 | 0 | durable = Boolean.valueOf(tempDurable).booleanValue(); |
341 | |
} |
342 | |
|
343 | |
|
344 | 0 | String durableName = (String)endpoint.getProperties().get("durableName"); |
345 | 0 | if (durableName == null && durable && topic) |
346 | |
{ |
347 | 0 | durableName = "mule." + connector.getName() + "." + endpoint.getEndpointURI().getAddress(); |
348 | 0 | logger.debug("Jms Connector for this receiver is durable but no durable name has been specified. Defaulting to: " |
349 | |
+ durableName); |
350 | |
} |
351 | |
|
352 | |
|
353 | 0 | ctx.consumer = jmsSupport.createConsumer(ctx.session, dest, selector, connector.isNoLocal(), |
354 | |
durableName, topic); |
355 | |
} |
356 | 0 | catch (JMSException e) |
357 | |
{ |
358 | 0 | throw new ConnectException(e, this); |
359 | 0 | } |
360 | 0 | } |
361 | |
} |