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