1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.jms; |
12 | |
|
13 | |
import org.mule.api.MuleMessage; |
14 | |
import org.mule.api.context.notification.TransactionNotificationListener; |
15 | |
import org.mule.api.endpoint.InboundEndpoint; |
16 | |
import org.mule.api.transaction.Transaction; |
17 | |
import org.mule.api.transaction.TransactionConfig; |
18 | |
import org.mule.context.notification.TransactionNotification; |
19 | |
import org.mule.transaction.TransactionCoordination; |
20 | |
import org.mule.transport.AbstractMessageRequester; |
21 | |
import org.mule.transport.jms.filters.JmsSelectorFilter; |
22 | |
import org.mule.util.StringUtils; |
23 | |
|
24 | |
import javax.jms.Destination; |
25 | |
import javax.jms.Message; |
26 | |
import javax.jms.MessageConsumer; |
27 | |
import javax.jms.Session; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public class JmsMessageRequester extends AbstractMessageRequester |
36 | |
{ |
37 | |
|
38 | |
private JmsConnector connector; |
39 | |
|
40 | |
public JmsMessageRequester(InboundEndpoint endpoint) |
41 | |
{ |
42 | 0 | super(endpoint); |
43 | 0 | this.connector = (JmsConnector) endpoint.getConnector(); |
44 | 0 | } |
45 | |
|
46 | |
@Override |
47 | |
protected void doConnect() throws Exception |
48 | |
{ |
49 | |
|
50 | 0 | } |
51 | |
|
52 | |
@Override |
53 | |
protected void doDisconnect() throws Exception |
54 | |
{ |
55 | |
|
56 | 0 | } |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
@Override |
70 | |
protected MuleMessage doRequest(long timeout) throws Exception |
71 | |
{ |
72 | 0 | Session session = null; |
73 | 0 | MessageConsumer consumer = null; |
74 | 0 | boolean cleanupListenerRegistered = false; |
75 | |
|
76 | |
try |
77 | |
{ |
78 | 0 | final boolean topic = connector.getTopicResolver().isTopic(endpoint); |
79 | |
|
80 | 0 | JmsSupport support = connector.getJmsSupport(); |
81 | 0 | final TransactionConfig transactionConfig = endpoint.getTransactionConfig(); |
82 | 0 | final Transaction tx = TransactionCoordination.getInstance().getTransaction(); |
83 | 0 | boolean transacted = transactionConfig != null && transactionConfig.isTransacted(); |
84 | |
|
85 | 0 | session = connector.getSession(transacted, topic); |
86 | |
|
87 | 0 | if (transacted && !tx.isXA()) |
88 | |
{ |
89 | |
|
90 | 0 | final Session finalSession = session; |
91 | 0 | getConnector().getMuleContext().registerListener(new TransactionNotificationListener<TransactionNotification>() |
92 | 0 | { |
93 | |
public void onNotification(TransactionNotification txNotification) |
94 | |
{ |
95 | 0 | final int txAction = txNotification.getAction(); |
96 | 0 | final String txId = txNotification.getTransactionStringId(); |
97 | 0 | if ((txAction == TransactionNotification.TRANSACTION_COMMITTED || txAction == TransactionNotification.TRANSACTION_ROLLEDBACK) && |
98 | |
txId.equals(tx.getId())) { |
99 | 0 | connector.closeQuietly(finalSession); |
100 | |
} |
101 | 0 | } |
102 | |
}, tx.getId()); |
103 | |
|
104 | 0 | cleanupListenerRegistered = true; |
105 | |
} |
106 | |
|
107 | 0 | Destination dest = support.createDestination(session, endpoint); |
108 | |
|
109 | |
|
110 | 0 | String selector = null; |
111 | 0 | if (endpoint.getFilter() != null && endpoint.getFilter() instanceof JmsSelectorFilter) |
112 | |
{ |
113 | 0 | final String expressionTemplate = ((JmsSelectorFilter) endpoint.getFilter()).getExpression(); |
114 | 0 | if (StringUtils.isNotBlank(expressionTemplate)) |
115 | |
{ |
116 | 0 | selector = connector.getMuleContext().getExpressionManager().parse(expressionTemplate, null); |
117 | |
} |
118 | 0 | } |
119 | 0 | else if (endpoint.getProperties() != null) |
120 | |
{ |
121 | |
|
122 | |
|
123 | 0 | final String expressionTemplate = (String) endpoint.getProperty(JmsConstants.JMS_SELECTOR_PROPERTY); |
124 | 0 | if (StringUtils.isNotBlank(expressionTemplate)) |
125 | |
{ |
126 | 0 | selector = connector.getMuleContext().getExpressionManager().parse(expressionTemplate, null); |
127 | |
} |
128 | |
} |
129 | 0 | String tempDurable = (String) endpoint.getProperties().get(JmsConstants.DURABLE_PROPERTY); |
130 | 0 | boolean durable = connector.isDurable(); |
131 | 0 | if (tempDurable != null) |
132 | |
{ |
133 | 0 | durable = Boolean.valueOf(tempDurable); |
134 | |
} |
135 | |
|
136 | |
|
137 | 0 | String durableName = (String) endpoint.getProperties().get(JmsConstants.DURABLE_NAME_PROPERTY); |
138 | 0 | if (durableName == null && durable && topic) |
139 | |
{ |
140 | 0 | durableName = "mule." + connector.getName() + "." + endpoint.getEndpointURI().getAddress(); |
141 | 0 | if (logger.isDebugEnabled()) |
142 | |
{ |
143 | 0 | logger.debug("Jms Connector for this receiver is durable but no durable name has been specified. Defaulting to: " |
144 | |
+ durableName); |
145 | |
} |
146 | |
} |
147 | |
|
148 | |
|
149 | 0 | consumer = support.createConsumer(session, dest, selector, connector.isNoLocal(), durableName, |
150 | |
topic, endpoint); |
151 | |
|
152 | |
Message message; |
153 | |
|
154 | 0 | if (timeout == JmsMessageDispatcher.RECEIVE_NO_WAIT) |
155 | |
{ |
156 | 0 | message = consumer.receiveNoWait(); |
157 | |
} |
158 | 0 | else if (timeout == JmsMessageDispatcher.RECEIVE_WAIT_INDEFINITELY) |
159 | |
{ |
160 | 0 | message = consumer.receive(); |
161 | |
} |
162 | |
else |
163 | |
{ |
164 | 0 | message = consumer.receive(timeout); |
165 | |
} |
166 | |
|
167 | 0 | if (message == null) |
168 | |
{ |
169 | 0 | return null; |
170 | |
} |
171 | |
|
172 | 0 | message = connector.preProcessMessage(message, session); |
173 | 0 | return createMuleMessage(message, endpoint.getEncoding()); |
174 | |
} |
175 | |
finally |
176 | |
{ |
177 | 0 | if (!cleanupListenerRegistered) |
178 | |
{ |
179 | 0 | connector.closeQuietly(consumer); |
180 | 0 | connector.closeSessionIfNoTransactionActive(session); |
181 | |
} |
182 | |
} |
183 | |
} |
184 | |
|
185 | |
@Override |
186 | |
protected void doDispose() |
187 | |
{ |
188 | |
|
189 | 0 | } |
190 | |
|
191 | |
} |