1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport; |
12 | |
|
13 | |
import org.mule.api.MuleException; |
14 | |
import org.mule.api.MuleMessage; |
15 | |
import org.mule.api.config.ThreadingProfile; |
16 | |
import org.mule.api.construct.FlowConstruct; |
17 | |
import org.mule.api.endpoint.InboundEndpoint; |
18 | |
import org.mule.api.lifecycle.CreateException; |
19 | |
import org.mule.api.transaction.TransactionCallback; |
20 | |
import org.mule.api.transport.Connector; |
21 | |
import org.mule.transaction.TransactionTemplate; |
22 | |
|
23 | |
import java.util.List; |
24 | |
|
25 | |
import javax.resource.spi.work.Work; |
26 | |
|
27 | |
import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public abstract class TransactedPollingMessageReceiver extends AbstractPollingMessageReceiver |
35 | |
{ |
36 | |
|
37 | 0 | private boolean receiveMessagesInTransaction = true; |
38 | |
|
39 | |
|
40 | 0 | private boolean useMultipleReceivers = true; |
41 | |
|
42 | |
public TransactedPollingMessageReceiver(Connector connector, |
43 | |
FlowConstruct flowConstruct, |
44 | |
final InboundEndpoint endpoint) throws CreateException |
45 | |
{ |
46 | 0 | super(connector, flowConstruct, endpoint); |
47 | 0 | this.setReceiveMessagesInTransaction(endpoint.getTransactionConfig().isTransacted()); |
48 | 0 | } |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
@Deprecated |
56 | |
public TransactedPollingMessageReceiver(Connector connector, |
57 | |
FlowConstruct flowConstruct, |
58 | |
final InboundEndpoint endpoint, |
59 | |
long frequency) throws CreateException |
60 | |
{ |
61 | 0 | this(connector, flowConstruct, endpoint); |
62 | 0 | this.setFrequency(frequency); |
63 | 0 | } |
64 | |
|
65 | |
public boolean isReceiveMessagesInTransaction() |
66 | |
{ |
67 | 0 | return receiveMessagesInTransaction; |
68 | |
} |
69 | |
|
70 | |
public void setReceiveMessagesInTransaction(boolean useTx) |
71 | |
{ |
72 | 0 | receiveMessagesInTransaction = useTx; |
73 | 0 | } |
74 | |
|
75 | |
public boolean isUseMultipleTransactedReceivers() |
76 | |
{ |
77 | 0 | return useMultipleReceivers; |
78 | |
} |
79 | |
|
80 | |
public void setUseMultipleTransactedReceivers(boolean useMultiple) |
81 | |
{ |
82 | 0 | useMultipleReceivers = useMultiple; |
83 | 0 | } |
84 | |
|
85 | |
@Override |
86 | |
public void doStart() throws MuleException |
87 | |
{ |
88 | |
|
89 | 0 | this.setUseMultipleTransactedReceivers(connector.isCreateMultipleTransactedReceivers()); |
90 | |
|
91 | 0 | ThreadingProfile tp = connector.getReceiverThreadingProfile(); |
92 | 0 | int numReceiversToStart = 1; |
93 | |
|
94 | 0 | if (this.isReceiveMessagesInTransaction() && this.isUseMultipleTransactedReceivers() |
95 | |
&& tp.isDoThreading()) |
96 | |
{ |
97 | 0 | numReceiversToStart = connector.getNumberOfConcurrentTransactedReceivers(); |
98 | |
} |
99 | |
|
100 | 0 | for (int i = 0; i < numReceiversToStart; i++) |
101 | |
{ |
102 | 0 | super.doStart(); |
103 | |
} |
104 | 0 | } |
105 | |
|
106 | |
@Override |
107 | |
public void poll() throws Exception |
108 | |
{ |
109 | 0 | TransactionTemplate<Object> tt = new TransactionTemplate<Object>(endpoint.getTransactionConfig(), connector.getMuleContext()); |
110 | |
|
111 | 0 | if (this.isReceiveMessagesInTransaction()) |
112 | |
{ |
113 | |
|
114 | |
|
115 | |
|
116 | 0 | TransactionCallback<Object> cb = new TransactionCallback<Object>() |
117 | 0 | { |
118 | |
public Object doInTransaction() throws Exception |
119 | |
{ |
120 | |
|
121 | 0 | List messages = getMessages(); |
122 | 0 | if (messages != null && messages.size() > 0) |
123 | |
{ |
124 | 0 | for (Object message : messages) |
125 | |
{ |
126 | 0 | processMessage(message); |
127 | |
} |
128 | |
} |
129 | 0 | return null; |
130 | |
} |
131 | |
}; |
132 | 0 | tt.execute(cb); |
133 | 0 | } |
134 | |
else |
135 | |
{ |
136 | |
|
137 | 0 | List messages = getMessages(); |
138 | 0 | if (messages != null && messages.size() > 0) |
139 | |
{ |
140 | 0 | final CountDownLatch countdown = new CountDownLatch(messages.size()); |
141 | 0 | for (Object message : messages) |
142 | |
{ |
143 | |
try |
144 | |
{ |
145 | 0 | this.getWorkManager().scheduleWork( |
146 | |
new MessageProcessorWorker(tt, countdown, message)); |
147 | |
} |
148 | 0 | catch (Exception e) |
149 | |
{ |
150 | 0 | countdown.countDown(); |
151 | 0 | throw e; |
152 | 0 | } |
153 | |
} |
154 | 0 | countdown.await(); |
155 | |
} |
156 | |
} |
157 | 0 | } |
158 | |
|
159 | |
protected class MessageProcessorWorker implements Work, TransactionCallback |
160 | |
{ |
161 | |
private final TransactionTemplate tt; |
162 | |
private final Object message; |
163 | |
private final CountDownLatch latch; |
164 | |
|
165 | |
public MessageProcessorWorker(TransactionTemplate tt, CountDownLatch latch, Object message) |
166 | 0 | { |
167 | 0 | this.tt = tt; |
168 | 0 | this.message = message; |
169 | 0 | this.latch = latch; |
170 | 0 | } |
171 | |
|
172 | |
public void release() |
173 | |
{ |
174 | |
|
175 | 0 | } |
176 | |
|
177 | |
public void run() |
178 | |
{ |
179 | |
try |
180 | |
{ |
181 | 0 | tt.execute(this); |
182 | |
} |
183 | 0 | catch (Exception e) |
184 | |
{ |
185 | 0 | connector.getMuleContext().getExceptionListener().handleException(e); |
186 | |
} |
187 | |
finally |
188 | |
{ |
189 | 0 | latch.countDown(); |
190 | 0 | } |
191 | 0 | } |
192 | |
|
193 | |
public Object doInTransaction() throws Exception |
194 | |
{ |
195 | 0 | processMessage(message); |
196 | 0 | return null; |
197 | |
} |
198 | |
|
199 | |
} |
200 | |
|
201 | |
protected abstract List<MuleMessage> getMessages() throws Exception; |
202 | |
|
203 | |
protected abstract void processMessage(Object message) throws Exception; |
204 | |
|
205 | |
} |