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.MuleMessage; |
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.StartException; |
19 | |
import org.mule.api.lifecycle.StopException; |
20 | |
import org.mule.api.transaction.Transaction; |
21 | |
import org.mule.api.transaction.TransactionCallback; |
22 | |
import org.mule.api.transport.Connector; |
23 | |
import org.mule.api.transport.MessageReceiver; |
24 | |
import org.mule.transaction.TransactionCoordination; |
25 | |
import org.mule.transaction.TransactionTemplate; |
26 | |
import org.mule.transport.AbstractMessageReceiver; |
27 | |
import org.mule.transport.ConnectException; |
28 | |
import org.mule.transport.jms.filters.JmsSelectorFilter; |
29 | |
import org.mule.transport.jms.redelivery.RedeliveryHandler; |
30 | |
import org.mule.util.ClassUtils; |
31 | |
|
32 | |
import javax.jms.Destination; |
33 | |
import javax.jms.JMSException; |
34 | |
import javax.jms.Message; |
35 | |
import javax.jms.MessageConsumer; |
36 | |
import javax.jms.MessageListener; |
37 | |
import javax.jms.Session; |
38 | |
import javax.jms.Topic; |
39 | |
import javax.resource.spi.work.Work; |
40 | |
|
41 | 0 | public class TransactedSingleResourceJmsMessageReceiver extends AbstractMessageReceiver |
42 | |
implements MessageListener |
43 | |
{ |
44 | |
protected JmsConnector connector; |
45 | |
protected RedeliveryHandler redeliveryHandler; |
46 | |
protected MessageConsumer consumer; |
47 | |
protected Session session; |
48 | 0 | protected boolean startOnConnect = false; |
49 | |
|
50 | |
|
51 | 0 | protected boolean receiveMessagesInTransaction = true; |
52 | |
|
53 | |
|
54 | 0 | protected boolean useMultipleReceivers = true; |
55 | |
|
56 | |
|
57 | |
public TransactedSingleResourceJmsMessageReceiver(Connector connector, |
58 | |
FlowConstruct flowConstruct, |
59 | |
InboundEndpoint endpoint) throws CreateException |
60 | |
{ |
61 | |
|
62 | 0 | super(connector, flowConstruct, endpoint); |
63 | |
|
64 | 0 | this.connector = (JmsConnector) connector; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
try |
70 | |
{ |
71 | 0 | redeliveryHandler = this.connector.getRedeliveryHandlerFactory().create(); |
72 | 0 | redeliveryHandler.setConnector(this.connector); |
73 | |
} |
74 | 0 | catch (Exception e) |
75 | |
{ |
76 | 0 | throw new CreateException(e, this); |
77 | 0 | } |
78 | 0 | } |
79 | |
|
80 | |
@Override |
81 | |
protected void doDispose() |
82 | |
{ |
83 | |
|
84 | 0 | } |
85 | |
|
86 | |
@Override |
87 | |
protected void doConnect() throws Exception |
88 | |
{ |
89 | |
try |
90 | |
{ |
91 | 0 | JmsSupport jmsSupport = this.connector.getJmsSupport(); |
92 | |
|
93 | 0 | if (session == null) |
94 | |
{ |
95 | 0 | session = this.connector.getSession(endpoint); |
96 | |
} |
97 | |
|
98 | |
|
99 | 0 | boolean topic = connector.getTopicResolver().isTopic(endpoint); |
100 | |
|
101 | 0 | Destination dest = jmsSupport.createDestination(session, endpoint); |
102 | |
|
103 | 0 | String selector = null; |
104 | 0 | if (endpoint.getFilter() != null && endpoint.getFilter() instanceof JmsSelectorFilter) |
105 | |
{ |
106 | 0 | selector = ((JmsSelectorFilter) endpoint.getFilter()).getExpression(); |
107 | |
} |
108 | 0 | else if (endpoint.getProperties() != null) |
109 | |
{ |
110 | |
|
111 | |
|
112 | 0 | selector = (String) endpoint.getProperties().get(JmsConstants.JMS_SELECTOR_PROPERTY); |
113 | |
} |
114 | 0 | String tempDurable = (String) endpoint.getProperties().get(JmsConstants.DURABLE_PROPERTY); |
115 | 0 | boolean durable = connector.isDurable(); |
116 | 0 | if (tempDurable != null) |
117 | |
{ |
118 | 0 | durable = Boolean.valueOf(tempDurable).booleanValue(); |
119 | |
} |
120 | |
|
121 | |
|
122 | 0 | String durableName = (String) endpoint.getProperties().get(JmsConstants.DURABLE_NAME_PROPERTY); |
123 | 0 | if (durableName == null && durable && dest instanceof Topic) |
124 | |
{ |
125 | 0 | durableName = "mule." + connector.getName() + "." + endpoint.getEndpointURI().getAddress(); |
126 | 0 | logger.debug("Jms Connector for this receiver is durable but no durable name has been specified. Defaulting to: " |
127 | |
+ durableName); |
128 | |
} |
129 | |
|
130 | |
|
131 | 0 | consumer = jmsSupport.createConsumer(session, dest, selector, connector.isNoLocal(), durableName, |
132 | |
topic, endpoint); |
133 | |
} |
134 | 0 | catch (JMSException e) |
135 | |
{ |
136 | 0 | throw new ConnectException(e, this); |
137 | 0 | } |
138 | 0 | } |
139 | |
|
140 | |
@Override |
141 | |
protected void doStart() throws MuleException |
142 | |
{ |
143 | |
try |
144 | |
{ |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | 0 | if (consumer == null) |
150 | |
{ |
151 | 0 | startOnConnect = true; |
152 | |
} |
153 | |
else |
154 | |
{ |
155 | 0 | startOnConnect = false; |
156 | 0 | this.consumer.setMessageListener(this); |
157 | |
} |
158 | |
} |
159 | 0 | catch (JMSException e) |
160 | |
{ |
161 | 0 | throw new StartException(e, this); |
162 | 0 | } |
163 | 0 | } |
164 | |
|
165 | |
@Override |
166 | |
protected void doStop() throws MuleException |
167 | |
{ |
168 | |
try |
169 | |
{ |
170 | 0 | if (consumer != null) |
171 | |
{ |
172 | 0 | consumer.setMessageListener(null); |
173 | |
} |
174 | |
} |
175 | 0 | catch (JMSException e) |
176 | |
{ |
177 | 0 | throw new StopException(e, this); |
178 | 0 | } |
179 | 0 | } |
180 | |
|
181 | |
@Override |
182 | |
public void doDisconnect() throws Exception |
183 | |
{ |
184 | 0 | closeConsumer(); |
185 | 0 | } |
186 | |
|
187 | |
protected void closeConsumer() |
188 | |
{ |
189 | 0 | connector.closeQuietly(consumer); |
190 | 0 | consumer = null; |
191 | 0 | connector.closeQuietly(session); |
192 | 0 | session = null; |
193 | 0 | } |
194 | |
|
195 | |
public void onMessage(Message message) |
196 | |
{ |
197 | |
try |
198 | |
{ |
199 | 0 | getWorkManager().scheduleWork(new MessageReceiverWorker(message, this)); |
200 | |
} |
201 | 0 | catch (Exception e) |
202 | |
{ |
203 | 0 | getConnector().getMuleContext().getExceptionListener().handleException(e); |
204 | 0 | } |
205 | 0 | } |
206 | |
|
207 | |
protected class MessageReceiverWorker implements Work |
208 | |
{ |
209 | |
Message message; |
210 | |
MessageReceiver receiver; |
211 | |
|
212 | |
public MessageReceiverWorker(Message message, MessageReceiver receiver) |
213 | 0 | { |
214 | 0 | this.message = message; |
215 | 0 | this.receiver = receiver; |
216 | 0 | } |
217 | |
|
218 | |
public void run() |
219 | |
{ |
220 | |
try |
221 | |
{ |
222 | 0 | TransactionTemplate<Void> tt = new TransactionTemplate<Void>( |
223 | |
endpoint.getTransactionConfig(), |
224 | |
connector.getMuleContext()); |
225 | |
|
226 | 0 | final String encoding = endpoint.getEncoding(); |
227 | |
|
228 | 0 | if (receiveMessagesInTransaction) |
229 | |
{ |
230 | 0 | TransactionCallback<Void> cb = new MessageTransactionCallback<Void>(message) |
231 | 0 | { |
232 | |
|
233 | |
public Void doInTransaction() throws Exception |
234 | |
{ |
235 | |
|
236 | 0 | Transaction tx = TransactionCoordination.getInstance().getTransaction(); |
237 | 0 | if (tx != null) |
238 | |
{ |
239 | 0 | tx.bindResource(connector.getConnection(), session); |
240 | |
} |
241 | 0 | if (tx instanceof JmsClientAcknowledgeTransaction) |
242 | |
{ |
243 | 0 | tx.bindResource(message, message); |
244 | |
} |
245 | |
|
246 | 0 | if (logger.isDebugEnabled()) |
247 | |
{ |
248 | 0 | logger.debug("Message received it is of type: " + |
249 | |
ClassUtils.getSimpleName(message.getClass())); |
250 | 0 | if (message.getJMSDestination() != null) |
251 | |
{ |
252 | 0 | logger.debug("Message received on " + message.getJMSDestination() + " (" |
253 | |
+ message.getJMSDestination().getClass().getName() + ")"); |
254 | |
} |
255 | |
else |
256 | |
{ |
257 | 0 | logger.debug("Message received on unknown destination"); |
258 | |
} |
259 | 0 | logger.debug("Message CorrelationId is: " + message.getJMSCorrelationID()); |
260 | 0 | logger.debug("Jms Message Id is: " + message.getJMSMessageID()); |
261 | |
} |
262 | |
|
263 | 0 | if (message.getJMSRedelivered()) |
264 | |
{ |
265 | 0 | if (logger.isDebugEnabled()) |
266 | |
{ |
267 | 0 | logger.debug("Message with correlationId: " |
268 | |
+ message.getJMSCorrelationID() |
269 | |
+ " is redelivered. handing off to Exception Handler"); |
270 | |
} |
271 | 0 | redeliveryHandler.handleRedelivery(message, receiver.getEndpoint(), receiver.getFlowConstruct()); |
272 | |
} |
273 | |
|
274 | 0 | MuleMessage messageToRoute = createMuleMessage(message, encoding); |
275 | 0 | routeMessage(messageToRoute); |
276 | 0 | return null; |
277 | |
} |
278 | |
}; |
279 | 0 | tt.execute(cb); |
280 | 0 | } |
281 | |
else |
282 | |
{ |
283 | 0 | MuleMessage messageToRoute = createMuleMessage(message, encoding); |
284 | 0 | routeMessage(messageToRoute); |
285 | |
} |
286 | |
} |
287 | 0 | catch (Exception e) |
288 | |
{ |
289 | 0 | getConnector().getMuleContext().getExceptionListener().handleException(e); |
290 | 0 | } |
291 | 0 | } |
292 | |
|
293 | |
public void release() |
294 | |
{ |
295 | |
|
296 | 0 | } |
297 | |
} |
298 | |
} |