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