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