1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
package org.mule.work; |
29 | |
|
30 | |
import org.mule.api.MuleContext; |
31 | |
import org.mule.api.MuleException; |
32 | |
import org.mule.api.config.MuleConfiguration; |
33 | |
import org.mule.api.config.ThreadingProfile; |
34 | |
import org.mule.api.context.MuleContextAware; |
35 | |
import org.mule.api.context.WorkManager; |
36 | |
import org.mule.api.work.WorkExecutor; |
37 | |
|
38 | |
import java.text.MessageFormat; |
39 | |
import java.util.List; |
40 | |
|
41 | |
import javax.resource.spi.XATerminator; |
42 | |
import javax.resource.spi.work.ExecutionContext; |
43 | |
import javax.resource.spi.work.Work; |
44 | |
import javax.resource.spi.work.WorkCompletedException; |
45 | |
import javax.resource.spi.work.WorkException; |
46 | |
import javax.resource.spi.work.WorkListener; |
47 | |
|
48 | |
import edu.emory.mathcs.backport.java.util.concurrent.Executor; |
49 | |
import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService; |
50 | |
import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; |
51 | |
|
52 | |
import org.apache.commons.logging.Log; |
53 | |
import org.apache.commons.logging.LogFactory; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public class MuleWorkManager implements WorkManager, MuleContextAware |
61 | |
{ |
62 | |
|
63 | |
|
64 | |
|
65 | 0 | protected static final Log logger = LogFactory.getLog(MuleWorkManager.class); |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
private static final long FORCEFUL_SHUTDOWN_TIMEOUT = 5000L; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
private final ThreadingProfile threadingProfile; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
private volatile ExecutorService workExecutorService; |
85 | |
private final String name; |
86 | |
private int gracefulShutdownTimeout; |
87 | |
private MuleContext muleContext; |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | 0 | private final WorkExecutor scheduleWorkExecutor = new ScheduleWorkExecutor(); |
93 | 0 | private final WorkExecutor startWorkExecutor = new StartWorkExecutor(); |
94 | 0 | private final WorkExecutor syncWorkExecutor = new SyncWorkExecutor(); |
95 | |
|
96 | |
public MuleWorkManager(ThreadingProfile profile, String name, int shutdownTimeout) |
97 | |
{ |
98 | 0 | super(); |
99 | |
|
100 | 0 | if (name == null) |
101 | |
{ |
102 | 0 | name = "WorkManager#" + hashCode(); |
103 | |
} |
104 | |
|
105 | 0 | this.threadingProfile = profile; |
106 | 0 | this.name = name; |
107 | 0 | gracefulShutdownTimeout = shutdownTimeout; |
108 | 0 | } |
109 | |
|
110 | |
public synchronized void start() throws MuleException |
111 | |
{ |
112 | 0 | gracefulShutdownTimeout = getMuleContext().getConfiguration().getShutdownTimeout(); |
113 | |
|
114 | 0 | if (workExecutorService == null) |
115 | |
{ |
116 | 0 | workExecutorService = threadingProfile.createPool(name); |
117 | |
} |
118 | 0 | } |
119 | |
|
120 | |
public synchronized void dispose() |
121 | |
{ |
122 | 0 | if (workExecutorService != null) |
123 | |
{ |
124 | |
|
125 | 0 | workExecutorService.shutdown(); |
126 | |
try |
127 | |
{ |
128 | |
|
129 | 0 | if (!workExecutorService.awaitTermination(gracefulShutdownTimeout, TimeUnit.MILLISECONDS)) |
130 | |
{ |
131 | |
|
132 | |
|
133 | 0 | List outstanding = workExecutorService.shutdownNow(); |
134 | |
|
135 | 0 | if (!workExecutorService.awaitTermination(FORCEFUL_SHUTDOWN_TIMEOUT, TimeUnit.MILLISECONDS)) |
136 | |
{ |
137 | 0 | logger.warn(MessageFormat.format( |
138 | |
"Pool {0} did not terminate in time; {1} work items were cancelled.", name, |
139 | |
outstanding.isEmpty() ? "No" : Integer.toString(outstanding.size()))); |
140 | |
} |
141 | |
else |
142 | |
{ |
143 | 0 | if (!outstanding.isEmpty()) |
144 | |
{ |
145 | 0 | logger.warn(MessageFormat.format( |
146 | |
"Pool {0} terminated; {1} work items were cancelled.", name, |
147 | |
Integer.toString(outstanding.size()))); |
148 | |
} |
149 | |
} |
150 | |
} |
151 | |
} |
152 | 0 | catch (InterruptedException ie) |
153 | |
{ |
154 | |
|
155 | 0 | workExecutorService.shutdownNow(); |
156 | |
|
157 | 0 | Thread.currentThread().interrupt(); |
158 | |
} |
159 | |
finally |
160 | |
{ |
161 | 0 | workExecutorService = null; |
162 | 0 | } |
163 | |
} |
164 | 0 | } |
165 | |
|
166 | |
|
167 | |
|
168 | |
public XATerminator getXATerminator() |
169 | |
{ |
170 | 0 | return null; |
171 | |
} |
172 | |
|
173 | |
public void doWork(Work work) throws WorkException |
174 | |
{ |
175 | 0 | executeWork(new WorkerContext(work), syncWorkExecutor); |
176 | 0 | } |
177 | |
|
178 | |
public void doWork(Work work, long startTimeout, ExecutionContext execContext, WorkListener workListener) |
179 | |
throws WorkException |
180 | |
{ |
181 | 0 | WorkerContext workWrapper = new WorkerContext(work, startTimeout, execContext, workListener); |
182 | 0 | workWrapper.setThreadPriority(Thread.currentThread().getPriority()); |
183 | 0 | executeWork(workWrapper, syncWorkExecutor); |
184 | 0 | } |
185 | |
|
186 | |
public long startWork(Work work) throws WorkException |
187 | |
{ |
188 | 0 | WorkerContext workWrapper = new WorkerContext(work); |
189 | 0 | workWrapper.setThreadPriority(Thread.currentThread().getPriority()); |
190 | 0 | executeWork(workWrapper, startWorkExecutor); |
191 | 0 | return System.currentTimeMillis() - workWrapper.getAcceptedTime(); |
192 | |
} |
193 | |
|
194 | |
public long startWork(Work work, |
195 | |
long startTimeout, |
196 | |
ExecutionContext execContext, |
197 | |
WorkListener workListener) throws WorkException |
198 | |
{ |
199 | 0 | WorkerContext workWrapper = new WorkerContext(work, startTimeout, execContext, workListener); |
200 | 0 | workWrapper.setThreadPriority(Thread.currentThread().getPriority()); |
201 | 0 | executeWork(workWrapper, startWorkExecutor); |
202 | 0 | return System.currentTimeMillis() - workWrapper.getAcceptedTime(); |
203 | |
} |
204 | |
|
205 | |
public void scheduleWork(Work work) throws WorkException |
206 | |
{ |
207 | 0 | WorkerContext workWrapper = new WorkerContext(work); |
208 | 0 | workWrapper.setThreadPriority(Thread.currentThread().getPriority()); |
209 | 0 | executeWork(workWrapper, scheduleWorkExecutor); |
210 | 0 | } |
211 | |
|
212 | |
public void scheduleWork(Work work, |
213 | |
long startTimeout, |
214 | |
ExecutionContext execContext, |
215 | |
WorkListener workListener) throws WorkException |
216 | |
{ |
217 | 0 | WorkerContext workWrapper = new WorkerContext(work, startTimeout, execContext, workListener); |
218 | 0 | workWrapper.setThreadPriority(Thread.currentThread().getPriority()); |
219 | 0 | executeWork(workWrapper, scheduleWorkExecutor); |
220 | 0 | } |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
public void execute(Runnable work) |
226 | |
{ |
227 | 0 | if (!isStarted()) |
228 | |
{ |
229 | 0 | throw new IllegalStateException("This MuleWorkManager '" + name + "' is stopped"); |
230 | |
} |
231 | 0 | workExecutorService.execute(work); |
232 | 0 | } |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
private void executeWork(WorkerContext work, WorkExecutor workExecutor) throws WorkException |
242 | |
{ |
243 | 0 | if (!isStarted()) |
244 | |
{ |
245 | 0 | throw new IllegalStateException("This MuleWorkManager '" + name + "' is stopped"); |
246 | |
} |
247 | |
|
248 | |
try |
249 | |
{ |
250 | 0 | work.workAccepted(this); |
251 | 0 | workExecutor.doExecute(work, workExecutorService); |
252 | 0 | WorkException exception = work.getWorkException(); |
253 | 0 | if (null != exception) |
254 | |
{ |
255 | 0 | throw exception; |
256 | |
} |
257 | |
} |
258 | 0 | catch (InterruptedException e) |
259 | |
{ |
260 | 0 | WorkCompletedException wcj = new WorkCompletedException("The execution has been interrupted for WorkManager: " + name, e); |
261 | 0 | wcj.setErrorCode(WorkException.INTERNAL); |
262 | 0 | throw wcj; |
263 | 0 | } |
264 | 0 | } |
265 | |
|
266 | |
public boolean isStarted() |
267 | |
{ |
268 | 0 | return (workExecutorService != null && !workExecutorService.isShutdown()); |
269 | |
} |
270 | |
|
271 | |
public MuleContext getMuleContext() |
272 | |
{ |
273 | 0 | return muleContext; |
274 | |
} |
275 | |
|
276 | |
public void setMuleContext(MuleContext muleContext) |
277 | |
{ |
278 | 0 | this.muleContext = muleContext; |
279 | 0 | if (this.threadingProfile != null && muleContext != null) |
280 | |
{ |
281 | 0 | threadingProfile.setMuleContext(muleContext); |
282 | |
} |
283 | 0 | } |
284 | |
} |