1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.config; |
12 | |
|
13 | |
import org.mule.impl.work.MuleWorkManager; |
14 | |
import org.mule.umo.manager.UMOWorkManager; |
15 | |
import org.mule.util.MapUtils; |
16 | |
import org.mule.util.concurrent.NamedThreadFactory; |
17 | |
import org.mule.util.concurrent.WaitPolicy; |
18 | |
|
19 | |
import java.util.Map; |
20 | |
|
21 | |
import edu.emory.mathcs.backport.java.util.concurrent.BlockingQueue; |
22 | |
import edu.emory.mathcs.backport.java.util.concurrent.LinkedBlockingDeque; |
23 | |
import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionHandler; |
24 | |
import edu.emory.mathcs.backport.java.util.concurrent.SynchronousQueue; |
25 | |
import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory; |
26 | |
import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor; |
27 | |
import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; |
28 | |
|
29 | |
import org.apache.commons.collections.map.CaseInsensitiveMap; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class ThreadingProfile |
38 | |
{ |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public static final int DEFAULT_MAX_THREADS_ACTIVE = 16; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public static final int DEFAULT_MAX_THREADS_IDLE = 1; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public static final int DEFAULT_MAX_BUFFER_SIZE = 0; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public static final long DEFAULT_MAX_THREAD_TTL = 60000; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public static final long DEFAULT_THREAD_WAIT_TIMEOUT = 30000L; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public static final boolean DEFAULT_DO_THREADING = true; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public static final int WHEN_EXHAUSTED_WAIT = 0; |
74 | |
public static final int WHEN_EXHAUSTED_DISCARD = 1; |
75 | |
public static final int WHEN_EXHAUSTED_DISCARD_OLDEST = 2; |
76 | |
public static final int WHEN_EXHAUSTED_ABORT = 3; |
77 | |
public static final int WHEN_EXHAUSTED_RUN = 4; |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public static final int DEFAULT_POOL_EXHAUST_ACTION = WHEN_EXHAUSTED_RUN; |
83 | |
|
84 | |
|
85 | 2 | private static final Map POOL_EXHAUSTED_ACTIONS = new CaseInsensitiveMap() |
86 | |
{ |
87 | |
private static final long serialVersionUID = 1L; |
88 | |
|
89 | |
|
90 | 2 | { |
91 | 2 | Integer value = new Integer(WHEN_EXHAUSTED_WAIT); |
92 | 2 | this.put("WHEN_EXHAUSTED_WAIT", value); |
93 | 2 | this.put("WAIT", value); |
94 | |
|
95 | 2 | value = new Integer(WHEN_EXHAUSTED_DISCARD); |
96 | 2 | this.put("WHEN_EXHAUSTED_DISCARD", value); |
97 | 2 | this.put("DISCARD", value); |
98 | |
|
99 | 2 | value = new Integer(WHEN_EXHAUSTED_DISCARD_OLDEST); |
100 | 2 | this.put("WHEN_EXHAUSTED_DISCARD_OLDEST", value); |
101 | 2 | this.put("DISCARD_OLDEST", value); |
102 | |
|
103 | 2 | value = new Integer(WHEN_EXHAUSTED_ABORT); |
104 | 2 | this.put("WHEN_EXHAUSTED_ABORT", value); |
105 | 2 | this.put("ABORT", value); |
106 | |
|
107 | 2 | value = new Integer(WHEN_EXHAUSTED_RUN); |
108 | 2 | this.put("WHEN_EXHAUSTED_RUN", value); |
109 | 2 | this.put("RUN", value); |
110 | 2 | } |
111 | |
}; |
112 | |
|
113 | 5156 | private int maxThreadsActive = DEFAULT_MAX_THREADS_ACTIVE; |
114 | 5156 | private int maxThreadsIdle = DEFAULT_MAX_THREADS_IDLE; |
115 | 5156 | private int maxBufferSize = DEFAULT_MAX_BUFFER_SIZE; |
116 | 5156 | private long threadTTL = DEFAULT_MAX_THREAD_TTL; |
117 | 5156 | private long threadWaitTimeout = DEFAULT_THREAD_WAIT_TIMEOUT; |
118 | 5156 | private int poolExhaustPolicy = DEFAULT_POOL_EXHAUST_ACTION; |
119 | 5156 | private boolean doThreading = DEFAULT_DO_THREADING; |
120 | |
|
121 | 5156 | private WorkManagerFactory workManagerFactory = new DefaultWorkManagerFactory(); |
122 | |
|
123 | |
private RejectedExecutionHandler rejectedExecutionHandler; |
124 | |
|
125 | |
private ThreadFactory threadFactory; |
126 | |
|
127 | |
public ThreadingProfile() |
128 | |
{ |
129 | 2296 | super(); |
130 | 2296 | } |
131 | |
|
132 | |
public ThreadingProfile(int maxThreadsActive, |
133 | |
int maxThreadsIdle, |
134 | |
long threadTTL, |
135 | |
int poolExhaustPolicy, |
136 | |
RejectedExecutionHandler rejectedExecutionHandler, |
137 | |
ThreadFactory threadFactory) |
138 | 0 | { |
139 | 0 | this.maxThreadsActive = maxThreadsActive; |
140 | 0 | this.maxThreadsIdle = maxThreadsIdle; |
141 | 0 | this.threadTTL = threadTTL; |
142 | 0 | this.poolExhaustPolicy = poolExhaustPolicy; |
143 | 0 | this.rejectedExecutionHandler = rejectedExecutionHandler; |
144 | 0 | this.threadFactory = threadFactory; |
145 | 0 | } |
146 | |
|
147 | |
public ThreadingProfile(ThreadingProfile tp) |
148 | 2860 | { |
149 | 2860 | this.maxThreadsActive = tp.getMaxThreadsActive(); |
150 | 2860 | this.maxThreadsIdle = tp.getMaxThreadsIdle(); |
151 | 2860 | this.maxBufferSize = tp.getMaxBufferSize(); |
152 | 2860 | this.threadTTL = tp.getThreadTTL(); |
153 | 2860 | this.threadWaitTimeout = tp.getThreadWaitTimeout(); |
154 | 2860 | this.poolExhaustPolicy = tp.getPoolExhaustedAction(); |
155 | 2860 | this.doThreading = tp.isDoThreading(); |
156 | 2860 | this.rejectedExecutionHandler = tp.getRejectedExecutionHandler(); |
157 | 2860 | this.threadFactory = tp.getThreadFactory(); |
158 | 2860 | this.workManagerFactory = tp.getWorkManagerFactory(); |
159 | 2860 | } |
160 | |
|
161 | |
public int getMaxThreadsActive() |
162 | |
{ |
163 | 2904 | return maxThreadsActive; |
164 | |
} |
165 | |
|
166 | |
public int getMaxThreadsIdle() |
167 | |
{ |
168 | 2860 | return maxThreadsIdle; |
169 | |
} |
170 | |
|
171 | |
public long getThreadTTL() |
172 | |
{ |
173 | 2882 | return threadTTL; |
174 | |
} |
175 | |
|
176 | |
public long getThreadWaitTimeout() |
177 | |
{ |
178 | 2860 | return threadWaitTimeout; |
179 | |
} |
180 | |
|
181 | |
public int getPoolExhaustedAction() |
182 | |
{ |
183 | 2860 | return poolExhaustPolicy; |
184 | |
} |
185 | |
|
186 | |
public RejectedExecutionHandler getRejectedExecutionHandler() |
187 | |
{ |
188 | 2860 | return rejectedExecutionHandler; |
189 | |
} |
190 | |
|
191 | |
public ThreadFactory getThreadFactory() |
192 | |
{ |
193 | 2860 | return threadFactory; |
194 | |
} |
195 | |
|
196 | |
public void setMaxThreadsActive(int maxThreadsActive) |
197 | |
{ |
198 | 0 | this.maxThreadsActive = maxThreadsActive; |
199 | 0 | } |
200 | |
|
201 | |
public void setMaxThreadsIdle(int maxThreadsIdle) |
202 | |
{ |
203 | 0 | this.maxThreadsIdle = maxThreadsIdle; |
204 | 0 | } |
205 | |
|
206 | |
public void setThreadTTL(long threadTTL) |
207 | |
{ |
208 | 0 | this.threadTTL = threadTTL; |
209 | 0 | } |
210 | |
|
211 | |
public void setThreadWaitTimeout(long threadWaitTimeout) |
212 | |
{ |
213 | 0 | this.threadWaitTimeout = threadWaitTimeout; |
214 | 0 | } |
215 | |
|
216 | |
public void setPoolExhaustedAction(int poolExhaustPolicy) |
217 | |
{ |
218 | 0 | this.poolExhaustPolicy = poolExhaustPolicy; |
219 | 0 | } |
220 | |
|
221 | |
public void setPoolExhaustedActionString(String poolExhaustPolicy) |
222 | |
{ |
223 | 0 | this.poolExhaustPolicy = MapUtils.getIntValue(POOL_EXHAUSTED_ACTIONS, poolExhaustPolicy, |
224 | |
DEFAULT_POOL_EXHAUST_ACTION); |
225 | 0 | } |
226 | |
|
227 | |
public void setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler) |
228 | |
{ |
229 | 0 | this.rejectedExecutionHandler = rejectedExecutionHandler; |
230 | 0 | } |
231 | |
|
232 | |
public void setThreadFactory(ThreadFactory threadFactory) |
233 | |
{ |
234 | 0 | this.threadFactory = threadFactory; |
235 | 0 | } |
236 | |
|
237 | |
public int getMaxBufferSize() |
238 | |
{ |
239 | 2860 | return maxBufferSize; |
240 | |
} |
241 | |
|
242 | |
public void setMaxBufferSize(int maxBufferSize) |
243 | |
{ |
244 | 0 | this.maxBufferSize = maxBufferSize; |
245 | 0 | } |
246 | |
|
247 | |
public WorkManagerFactory getWorkManagerFactory() |
248 | |
{ |
249 | 2860 | return workManagerFactory; |
250 | |
} |
251 | |
|
252 | |
public void setWorkManagerFactory(WorkManagerFactory workManagerFactory) |
253 | |
{ |
254 | 0 | this.workManagerFactory = workManagerFactory; |
255 | 0 | } |
256 | |
|
257 | |
public UMOWorkManager createWorkManager(String name) |
258 | |
{ |
259 | 60 | return workManagerFactory.createWorkManager(this, name); |
260 | |
} |
261 | |
|
262 | |
public ThreadPoolExecutor createPool() |
263 | |
{ |
264 | 0 | return createPool(null); |
265 | |
} |
266 | |
|
267 | |
public ThreadPoolExecutor createPool(String name) |
268 | |
{ |
269 | |
BlockingQueue buffer; |
270 | |
|
271 | 98 | if (maxBufferSize > 0 && maxThreadsActive > 1) |
272 | |
{ |
273 | 0 | buffer = new LinkedBlockingDeque(maxBufferSize); |
274 | |
} |
275 | |
else |
276 | |
{ |
277 | 98 | buffer = new SynchronousQueue(); |
278 | |
} |
279 | |
|
280 | 98 | if (maxThreadsIdle > maxThreadsActive) |
281 | |
{ |
282 | 0 | maxThreadsIdle = maxThreadsActive; |
283 | |
} |
284 | |
|
285 | 98 | ThreadPoolExecutor pool = new ThreadPoolExecutor(maxThreadsIdle, maxThreadsActive, threadTTL, |
286 | |
TimeUnit.MILLISECONDS, buffer); |
287 | |
|
288 | |
|
289 | 98 | if (threadFactory != null) |
290 | |
{ |
291 | 0 | pool.setThreadFactory(threadFactory); |
292 | |
} |
293 | |
else |
294 | |
{ |
295 | |
|
296 | 98 | if (name != null) |
297 | |
{ |
298 | 98 | pool.setThreadFactory(new NamedThreadFactory(name)); |
299 | |
} |
300 | |
else |
301 | |
{ |
302 | |
|
303 | |
|
304 | |
} |
305 | |
} |
306 | |
|
307 | 98 | if (rejectedExecutionHandler != null) |
308 | |
{ |
309 | 0 | pool.setRejectedExecutionHandler(rejectedExecutionHandler); |
310 | |
} |
311 | |
else |
312 | |
{ |
313 | 98 | switch (poolExhaustPolicy) |
314 | |
{ |
315 | |
case WHEN_EXHAUSTED_DISCARD_OLDEST : |
316 | 0 | pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy()); |
317 | 0 | break; |
318 | |
case WHEN_EXHAUSTED_RUN : |
319 | 98 | pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); |
320 | 98 | break; |
321 | |
case WHEN_EXHAUSTED_ABORT : |
322 | 0 | pool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy()); |
323 | 0 | break; |
324 | |
case WHEN_EXHAUSTED_DISCARD : |
325 | 0 | pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy()); |
326 | 0 | break; |
327 | |
default : |
328 | |
|
329 | 0 | pool.setRejectedExecutionHandler(new WaitPolicy(threadWaitTimeout, TimeUnit.MILLISECONDS)); |
330 | |
break; |
331 | |
} |
332 | |
} |
333 | |
|
334 | 98 | return pool; |
335 | |
} |
336 | |
|
337 | |
public boolean isDoThreading() |
338 | |
{ |
339 | 2864 | return doThreading; |
340 | |
} |
341 | |
|
342 | |
public void setDoThreading(boolean doThreading) |
343 | |
{ |
344 | 844 | this.doThreading = doThreading; |
345 | 844 | } |
346 | |
|
347 | |
public String toString() |
348 | |
{ |
349 | 44 | return "ThreadingProfile{" + "maxThreadsActive=" + maxThreadsActive + ", maxThreadsIdle=" |
350 | |
+ maxThreadsIdle + ", maxBufferSize=" + maxBufferSize + ", threadTTL=" + threadTTL |
351 | |
+ ", poolExhaustPolicy=" + poolExhaustPolicy + ", threadWaitTimeout=" |
352 | |
+ threadWaitTimeout + ", doThreading=" + doThreading + ", workManagerFactory=" |
353 | |
+ workManagerFactory + ", rejectedExecutionHandler=" + rejectedExecutionHandler |
354 | |
+ ", threadFactory=" + threadFactory + "}"; |
355 | |
} |
356 | |
|
357 | |
public static interface WorkManagerFactory |
358 | |
{ |
359 | |
UMOWorkManager createWorkManager(ThreadingProfile profile, String name); |
360 | |
} |
361 | |
|
362 | 10312 | private class DefaultWorkManagerFactory implements WorkManagerFactory |
363 | |
{ |
364 | |
public UMOWorkManager createWorkManager(ThreadingProfile profile, String name) |
365 | |
{ |
366 | 60 | return new MuleWorkManager(profile, name); |
367 | |
} |
368 | |
} |
369 | |
|
370 | |
} |