1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.config.pool; |
8 | |
|
9 | |
import org.mule.api.MuleContext; |
10 | |
import org.mule.api.config.ThreadingProfile; |
11 | |
import org.mule.util.StringUtils; |
12 | |
import org.mule.util.concurrent.NamedThreadFactory; |
13 | |
import org.mule.util.concurrent.WaitPolicy; |
14 | |
|
15 | |
import edu.emory.mathcs.backport.java.util.concurrent.BlockingQueue; |
16 | |
import edu.emory.mathcs.backport.java.util.concurrent.LinkedBlockingDeque; |
17 | |
import edu.emory.mathcs.backport.java.util.concurrent.SynchronousQueue; |
18 | |
import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor; |
19 | |
import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; |
20 | |
import org.apache.commons.logging.Log; |
21 | |
import org.apache.commons.logging.LogFactory; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | 0 | public class DefaultThreadPoolFactory extends ThreadPoolFactory |
27 | |
{ |
28 | |
|
29 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
30 | |
|
31 | |
public ThreadPoolExecutor createPool(String name, ThreadingProfile tp) |
32 | |
{ |
33 | |
BlockingQueue buffer; |
34 | |
|
35 | 0 | if (tp.getMaxBufferSize() > 0 && tp.getMaxThreadsActive() > 1) |
36 | |
{ |
37 | 0 | buffer = new LinkedBlockingDeque(tp.getMaxBufferSize()); |
38 | |
} |
39 | |
else |
40 | |
{ |
41 | 0 | buffer = new SynchronousQueue(); |
42 | |
} |
43 | |
|
44 | 0 | ThreadPoolExecutor pool = internalCreatePool(name, tp, buffer); |
45 | 0 | configureThreadFactory(name, tp, pool); |
46 | |
|
47 | |
|
48 | 0 | if (tp.getRejectedExecutionHandler() != null) |
49 | |
{ |
50 | 0 | pool.setRejectedExecutionHandler(tp.getRejectedExecutionHandler()); |
51 | |
} |
52 | |
else |
53 | |
{ |
54 | 0 | switch (tp.getPoolExhaustedAction()) |
55 | |
{ |
56 | |
case ThreadingProfile.WHEN_EXHAUSTED_DISCARD_OLDEST : |
57 | 0 | pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy()); |
58 | 0 | break; |
59 | |
case ThreadingProfile.WHEN_EXHAUSTED_RUN : |
60 | 0 | pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); |
61 | 0 | break; |
62 | |
case ThreadingProfile.WHEN_EXHAUSTED_ABORT : |
63 | 0 | pool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy()); |
64 | 0 | break; |
65 | |
case ThreadingProfile.WHEN_EXHAUSTED_DISCARD : |
66 | 0 | pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy()); |
67 | 0 | break; |
68 | |
default : |
69 | |
|
70 | 0 | pool.setRejectedExecutionHandler(new WaitPolicy(tp.getThreadWaitTimeout(), TimeUnit.MILLISECONDS)); |
71 | |
break; |
72 | |
} |
73 | |
} |
74 | |
|
75 | 0 | return pool; |
76 | |
|
77 | |
} |
78 | |
|
79 | |
protected void configureThreadFactory(String name, ThreadingProfile tp, ThreadPoolExecutor pool) |
80 | |
{ |
81 | |
|
82 | 0 | if (tp.getThreadFactory() != null) |
83 | |
{ |
84 | 0 | pool.setThreadFactory(tp.getThreadFactory()); |
85 | |
} |
86 | |
else |
87 | |
{ |
88 | |
|
89 | 0 | if (StringUtils.isNotBlank(name)) |
90 | |
{ |
91 | |
|
92 | |
|
93 | 0 | pool.setThreadFactory(new NamedThreadFactory(name, MuleContext.class.getClassLoader())); |
94 | |
} |
95 | |
else |
96 | |
{ |
97 | |
|
98 | |
|
99 | |
} |
100 | |
} |
101 | 0 | } |
102 | |
|
103 | |
protected ThreadPoolExecutor internalCreatePool(String name, ThreadingProfile tp, BlockingQueue buffer) |
104 | |
{ |
105 | 0 | return new ThreadPoolExecutor(Math.min(tp.getMaxThreadsIdle(), tp.getMaxThreadsActive()), |
106 | |
tp.getMaxThreadsActive(), tp.getThreadTTL(), |
107 | |
TimeUnit.MILLISECONDS, buffer); |
108 | |
|
109 | |
} |
110 | |
} |