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