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