1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.jms; |
12 | |
|
13 | |
import org.mule.umo.UMOComponent; |
14 | |
import org.mule.umo.UMOException; |
15 | |
import org.mule.umo.UMOTransaction; |
16 | |
import org.mule.umo.TransactionException; |
17 | |
import org.mule.umo.endpoint.UMOEndpoint; |
18 | |
import org.mule.umo.lifecycle.InitialisationException; |
19 | |
import org.mule.umo.lifecycle.LifecycleException; |
20 | |
import org.mule.umo.provider.UMOConnector; |
21 | |
import org.mule.util.ClassUtils; |
22 | |
import org.mule.providers.AbstractReceiverWorker; |
23 | |
import org.mule.providers.AbstractMessageReceiver; |
24 | |
import org.mule.providers.ConnectException; |
25 | |
import org.mule.providers.jms.filters.JmsSelectorFilter; |
26 | |
|
27 | |
import java.util.ArrayList; |
28 | |
import java.util.List; |
29 | |
|
30 | |
import javax.jms.Message; |
31 | |
import javax.jms.JMSException; |
32 | |
import javax.jms.Destination; |
33 | |
import javax.jms.Topic; |
34 | |
import javax.jms.MessageConsumer; |
35 | |
import javax.jms.Session; |
36 | |
import javax.jms.MessageListener; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class JmsMessageReceiver extends AbstractMessageReceiver implements MessageListener |
43 | |
{ |
44 | |
|
45 | |
protected JmsConnector connector; |
46 | |
protected RedeliveryHandler redeliveryHandler; |
47 | |
protected MessageConsumer consumer; |
48 | |
protected Session session; |
49 | 0 | protected boolean startOnConnect = false; |
50 | |
|
51 | |
public JmsMessageReceiver(UMOConnector connector, UMOComponent component, UMOEndpoint endpoint) |
52 | |
throws InitialisationException |
53 | |
{ |
54 | 0 | super(connector, component, endpoint); |
55 | 0 | this.connector = (JmsConnector) connector; |
56 | |
|
57 | |
try |
58 | |
{ |
59 | 0 | redeliveryHandler = this.connector.createRedeliveryHandler(); |
60 | 0 | redeliveryHandler.setConnector(this.connector); |
61 | |
} |
62 | 0 | catch (Exception e) |
63 | |
{ |
64 | 0 | throw new InitialisationException(e, this); |
65 | 0 | } |
66 | 0 | } |
67 | |
|
68 | |
protected void doConnect() throws Exception |
69 | |
{ |
70 | 0 | createConsumer(); |
71 | 0 | if (startOnConnect) |
72 | |
{ |
73 | 0 | doStart(); |
74 | |
} |
75 | 0 | } |
76 | |
|
77 | |
protected void doDisconnect() throws Exception |
78 | |
{ |
79 | 0 | closeConsumer(); |
80 | 0 | } |
81 | |
|
82 | |
public void onMessage(Message message) |
83 | |
{ |
84 | |
try |
85 | |
{ |
86 | 0 | getWorkManager().scheduleWork(new JmsWorker(message, this)); |
87 | |
} |
88 | 0 | catch (Exception e) |
89 | |
{ |
90 | 0 | handleException(e); |
91 | 0 | } |
92 | 0 | } |
93 | |
|
94 | |
protected class JmsWorker extends AbstractReceiverWorker |
95 | |
{ |
96 | |
public JmsWorker(Message message, AbstractMessageReceiver receiver) |
97 | 0 | { |
98 | 0 | super(new ArrayList(1), receiver); |
99 | 0 | messages.add(message); |
100 | 0 | } |
101 | |
|
102 | |
public JmsWorker(List messages, AbstractMessageReceiver receiver) |
103 | 0 | { |
104 | 0 | super(messages, receiver); |
105 | 0 | } |
106 | |
|
107 | |
|
108 | |
protected Object preProcessMessage(Object message) throws Exception |
109 | |
{ |
110 | 0 | Message m = (Message) message; |
111 | |
|
112 | 0 | if (logger.isDebugEnabled()) |
113 | |
{ |
114 | 0 | logger.debug("Message received it is of type: " + |
115 | |
ClassUtils.getSimpleName(message.getClass())); |
116 | 0 | if (m.getJMSDestination() != null) |
117 | |
{ |
118 | 0 | logger.debug("Message received on " + m.getJMSDestination() + " (" |
119 | |
+ m.getJMSDestination().getClass().getName() + ")"); |
120 | |
} |
121 | |
else |
122 | |
{ |
123 | 0 | logger.debug("Message received on unknown destination"); |
124 | |
} |
125 | 0 | logger.debug("Message CorrelationId is: " + m.getJMSCorrelationID()); |
126 | 0 | logger.debug("Jms Message Id is: " + m.getJMSMessageID()); |
127 | |
} |
128 | |
|
129 | 0 | if (m.getJMSRedelivered() && redeliveryHandler != null) |
130 | |
{ |
131 | 0 | if (logger.isDebugEnabled()) |
132 | |
{ |
133 | 0 | logger.debug("Message with correlationId: " + m.getJMSCorrelationID() |
134 | |
+ " has redelivered flag set, handing off to Exception Handler"); |
135 | |
} |
136 | 0 | redeliveryHandler.handleRedelivery(m); |
137 | |
} |
138 | 0 | return m; |
139 | |
|
140 | |
} |
141 | |
|
142 | |
protected void bindTransaction(UMOTransaction tx) throws TransactionException |
143 | |
{ |
144 | 0 | if(tx instanceof JmsTransaction) |
145 | |
{ |
146 | 0 | tx.bindResource(connector.getConnection(), session); |
147 | |
} |
148 | 0 | else if(tx instanceof JmsClientAcknowledgeTransaction) |
149 | |
{ |
150 | |
|
151 | |
|
152 | 0 | tx.bindResource(connector.getConnection(), session); |
153 | 0 | ((JmsClientAcknowledgeTransaction)tx).setMessage((Message)messages.get(0)); |
154 | |
} |
155 | 0 | } |
156 | |
} |
157 | |
|
158 | |
protected void doStart() throws UMOException |
159 | |
{ |
160 | |
try |
161 | |
{ |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | 0 | if (consumer == null) |
169 | |
{ |
170 | 0 | startOnConnect = true; |
171 | |
} |
172 | |
else |
173 | |
{ |
174 | 0 | startOnConnect = false; |
175 | 0 | consumer.setMessageListener(this); |
176 | |
} |
177 | |
} |
178 | 0 | catch (JMSException e) |
179 | |
{ |
180 | 0 | throw new LifecycleException(e, this); |
181 | 0 | } |
182 | 0 | } |
183 | |
|
184 | |
protected void doStop() throws UMOException |
185 | |
{ |
186 | |
try |
187 | |
{ |
188 | 0 | if (consumer != null) |
189 | |
{ |
190 | 0 | consumer.setMessageListener(null); |
191 | |
} |
192 | |
} |
193 | 0 | catch (JMSException e) |
194 | |
{ |
195 | 0 | throw new LifecycleException(e, this); |
196 | 0 | } |
197 | 0 | } |
198 | |
|
199 | |
protected void doDispose() |
200 | |
{ |
201 | |
|
202 | 0 | } |
203 | |
|
204 | |
protected void closeConsumer() |
205 | |
{ |
206 | 0 | connector.closeQuietly(consumer); |
207 | 0 | consumer = null; |
208 | 0 | connector.closeQuietly(session); |
209 | 0 | session = null; |
210 | 0 | } |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
protected void createConsumer() throws Exception |
218 | |
{ |
219 | |
try |
220 | |
{ |
221 | 0 | JmsSupport jmsSupport = this.connector.getJmsSupport(); |
222 | |
|
223 | 0 | if (session == null) |
224 | |
{ |
225 | 0 | session = this.connector.getSession(endpoint); |
226 | |
} |
227 | |
|
228 | 0 | boolean topic = connector.getTopicResolver().isTopic(endpoint, true); |
229 | |
|
230 | |
|
231 | 0 | Destination dest = jmsSupport.createDestination(session, endpoint.getEndpointURI().getAddress(), |
232 | |
topic); |
233 | |
|
234 | |
|
235 | 0 | String selector = null; |
236 | 0 | if (endpoint.getFilter() != null && endpoint.getFilter() instanceof JmsSelectorFilter) |
237 | |
{ |
238 | 0 | selector = ((JmsSelectorFilter) endpoint.getFilter()).getExpression(); |
239 | |
} |
240 | 0 | else if (endpoint.getProperties() != null) |
241 | |
{ |
242 | |
|
243 | |
|
244 | 0 | selector = (String) endpoint.getProperties().get(JmsConstants.JMS_SELECTOR_PROPERTY); |
245 | |
} |
246 | 0 | String tempDurable = (String) endpoint.getProperties().get(JmsConstants.DURABLE_PROPERTY); |
247 | 0 | boolean durable = connector.isDurable(); |
248 | 0 | if (tempDurable != null) |
249 | |
{ |
250 | 0 | durable = Boolean.valueOf(tempDurable).booleanValue(); |
251 | |
} |
252 | |
|
253 | |
|
254 | 0 | String durableName = (String) endpoint.getProperties().get(JmsConstants.DURABLE_NAME_PROPERTY); |
255 | 0 | if (durableName == null && durable && dest instanceof Topic) |
256 | |
{ |
257 | 0 | durableName = "mule." + connector.getName() + "." + endpoint.getEndpointURI().getAddress(); |
258 | 0 | logger.debug("Jms Connector for this receiver is durable but no durable name has been specified. Defaulting to: " |
259 | |
+ durableName); |
260 | |
} |
261 | |
|
262 | |
|
263 | 0 | consumer = jmsSupport.createConsumer(session, dest, selector, connector.isNoLocal(), durableName, |
264 | |
topic); |
265 | |
} |
266 | 0 | catch (JMSException e) |
267 | |
{ |
268 | 0 | throw new ConnectException(e, this); |
269 | 0 | } |
270 | 0 | } |
271 | |
|
272 | |
} |