1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.config; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.config.ThreadingProfile; |
15 | |
import org.mule.api.context.MuleContextAware; |
16 | |
import org.mule.api.context.WorkManager; |
17 | |
import org.mule.config.pool.ThreadPoolFactory; |
18 | |
|
19 | |
import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService; |
20 | |
import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionHandler; |
21 | |
import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class ChainedThreadingProfile implements ThreadingProfile |
40 | |
{ |
41 | |
|
42 | |
private Integer maxThreadsActive; |
43 | |
private Integer maxThreadsIdle; |
44 | |
private Integer maxBufferSize; |
45 | |
private Long threadTTL; |
46 | |
private Long threadWaitTimeout; |
47 | |
private Integer poolExhaustedAction; |
48 | |
private Boolean doThreading; |
49 | |
|
50 | 0 | private ThreadPoolFactory poolFactory = ThreadPoolFactory.newInstance(); |
51 | 0 | private WorkManagerFactory workManagerFactory = new ImmutableThreadingProfile.DefaultWorkManagerFactory(); |
52 | |
private RejectedExecutionHandler rejectedExecutionHandler; |
53 | |
private ThreadFactory threadFactory; |
54 | |
|
55 | |
private ThreadingProfile delegate; |
56 | |
|
57 | |
private MuleContext muleContext; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public ChainedThreadingProfile() |
64 | |
{ |
65 | |
|
66 | 0 | this(DEFAULT_THREADING_PROFILE); |
67 | 0 | } |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public ChainedThreadingProfile(ThreadingProfile delegate) |
75 | |
{ |
76 | 0 | this(delegate, true); |
77 | 0 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public ChainedThreadingProfile(ThreadingProfile delegate, boolean dynamic) |
88 | 0 | { |
89 | 0 | if (!dynamic) |
90 | |
{ |
91 | |
|
92 | 0 | delegate = new ImmutableThreadingProfile(delegate); |
93 | |
} |
94 | 0 | this.delegate = delegate; |
95 | 0 | } |
96 | |
|
97 | |
public int getMaxThreadsActive() |
98 | |
{ |
99 | 0 | return null != maxThreadsActive ? maxThreadsActive : delegate.getMaxThreadsActive(); |
100 | |
} |
101 | |
|
102 | |
public int getMaxThreadsIdle() |
103 | |
{ |
104 | 0 | return null != maxThreadsIdle ? maxThreadsIdle : delegate.getMaxThreadsIdle(); |
105 | |
} |
106 | |
|
107 | |
public long getThreadTTL() |
108 | |
{ |
109 | 0 | return null != threadTTL ? threadTTL : delegate.getThreadTTL(); |
110 | |
} |
111 | |
|
112 | |
public long getThreadWaitTimeout() |
113 | |
{ |
114 | 0 | return null != threadWaitTimeout ? threadWaitTimeout : delegate.getThreadWaitTimeout(); |
115 | |
} |
116 | |
|
117 | |
public int getPoolExhaustedAction() |
118 | |
{ |
119 | 0 | return null != poolExhaustedAction ? poolExhaustedAction : delegate.getPoolExhaustedAction(); |
120 | |
} |
121 | |
|
122 | |
public RejectedExecutionHandler getRejectedExecutionHandler() |
123 | |
{ |
124 | 0 | return rejectedExecutionHandler; |
125 | |
} |
126 | |
|
127 | |
public ThreadFactory getThreadFactory() |
128 | |
{ |
129 | 0 | return threadFactory; |
130 | |
} |
131 | |
|
132 | |
public void setMaxThreadsActive(int maxThreadsActive) |
133 | |
{ |
134 | 0 | this.maxThreadsActive = maxThreadsActive; |
135 | 0 | } |
136 | |
|
137 | |
public void setMaxThreadsIdle(int maxThreadsIdle) |
138 | |
{ |
139 | 0 | this.maxThreadsIdle = maxThreadsIdle; |
140 | 0 | } |
141 | |
|
142 | |
public void setThreadTTL(long threadTTL) |
143 | |
{ |
144 | 0 | this.threadTTL = threadTTL; |
145 | 0 | } |
146 | |
|
147 | |
public void setThreadWaitTimeout(long threadWaitTimeout) |
148 | |
{ |
149 | 0 | this.threadWaitTimeout = threadWaitTimeout; |
150 | 0 | } |
151 | |
|
152 | |
public void setPoolExhaustedAction(int poolExhaustPolicy) |
153 | |
{ |
154 | 0 | this.poolExhaustedAction = poolExhaustPolicy; |
155 | 0 | } |
156 | |
|
157 | |
public void setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler) |
158 | |
{ |
159 | 0 | this.rejectedExecutionHandler = rejectedExecutionHandler; |
160 | 0 | } |
161 | |
|
162 | |
public void setThreadFactory(ThreadFactory threadFactory) |
163 | |
{ |
164 | 0 | this.threadFactory = threadFactory; |
165 | 0 | } |
166 | |
|
167 | |
public int getMaxBufferSize() |
168 | |
{ |
169 | 0 | return null != maxBufferSize ? maxBufferSize : delegate.getMaxBufferSize(); |
170 | |
} |
171 | |
|
172 | |
public void setMaxBufferSize(int maxBufferSize) |
173 | |
{ |
174 | 0 | this.maxBufferSize = maxBufferSize; |
175 | 0 | } |
176 | |
|
177 | |
public WorkManagerFactory getWorkManagerFactory() |
178 | |
{ |
179 | 0 | return workManagerFactory; |
180 | |
} |
181 | |
|
182 | |
public void setWorkManagerFactory(WorkManagerFactory workManagerFactory) |
183 | |
{ |
184 | 0 | this.workManagerFactory = workManagerFactory; |
185 | 0 | } |
186 | |
|
187 | |
public WorkManager createWorkManager(String name, int shutdownTimeout) |
188 | |
{ |
189 | |
|
190 | 0 | return workManagerFactory.createWorkManager(new ImmutableThreadingProfile(this), name, shutdownTimeout); |
191 | |
} |
192 | |
|
193 | |
public ExecutorService createPool() |
194 | |
{ |
195 | 0 | return createPool(null); |
196 | |
} |
197 | |
|
198 | |
public ExecutorService createPool(String name) |
199 | |
{ |
200 | |
|
201 | 0 | return poolFactory.createPool(name, new ImmutableThreadingProfile(this)); |
202 | |
} |
203 | |
|
204 | |
public boolean isDoThreading() |
205 | |
{ |
206 | 0 | return null != doThreading ? doThreading : delegate.isDoThreading(); |
207 | |
} |
208 | |
|
209 | |
public void setDoThreading(boolean doThreading) |
210 | |
{ |
211 | 0 | this.doThreading = doThreading; |
212 | 0 | } |
213 | |
|
214 | |
public ThreadPoolFactory getPoolFactory() |
215 | |
{ |
216 | 0 | return poolFactory; |
217 | |
} |
218 | |
|
219 | |
public MuleContext getMuleContext() |
220 | |
{ |
221 | 0 | return muleContext; |
222 | |
} |
223 | |
|
224 | |
public void setMuleContext(MuleContext muleContext) |
225 | |
{ |
226 | 0 | this.muleContext = muleContext; |
227 | |
|
228 | |
|
229 | 0 | if (this.workManagerFactory instanceof MuleContextAware) |
230 | |
{ |
231 | 0 | ((MuleContextAware) workManagerFactory).setMuleContext(muleContext); |
232 | |
} |
233 | |
|
234 | 0 | poolFactory.setMuleContext(muleContext); |
235 | 0 | } |
236 | |
|
237 | |
public String toString() |
238 | |
{ |
239 | 0 | return "ThreadingProfile{" + "maxThreadsActive=" + maxThreadsActive + ", maxThreadsIdle=" |
240 | |
+ maxThreadsIdle + ", maxBufferSize=" + maxBufferSize + ", threadTTL=" + threadTTL |
241 | |
+ ", poolExhaustedAction=" + poolExhaustedAction + ", threadWaitTimeout=" |
242 | |
+ threadWaitTimeout + ", doThreading=" + doThreading + ", workManagerFactory=" |
243 | |
+ workManagerFactory + ", rejectedExecutionHandler=" + rejectedExecutionHandler |
244 | |
+ ", threadFactory=" + threadFactory + "}"; |
245 | |
} |
246 | |
|
247 | |
} |