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 private ThreadPoolFactory poolFactory = ThreadPoolFactory.newInstance();
51 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 this(DEFAULT_THREADING_PROFILE);
67 }
68
69
70
71
72
73
74 public ChainedThreadingProfile(ThreadingProfile delegate)
75 {
76 this(delegate, true);
77 }
78
79
80
81
82
83
84
85
86
87 public ChainedThreadingProfile(ThreadingProfile delegate, boolean dynamic)
88 {
89 if (!dynamic)
90 {
91
92 delegate = new ImmutableThreadingProfile(delegate);
93 }
94 this.delegate = delegate;
95 }
96
97 public int getMaxThreadsActive()
98 {
99 return null != maxThreadsActive ? maxThreadsActive : delegate.getMaxThreadsActive();
100 }
101
102 public int getMaxThreadsIdle()
103 {
104 return null != maxThreadsIdle ? maxThreadsIdle : delegate.getMaxThreadsIdle();
105 }
106
107 public long getThreadTTL()
108 {
109 return null != threadTTL ? threadTTL : delegate.getThreadTTL();
110 }
111
112 public long getThreadWaitTimeout()
113 {
114 return null != threadWaitTimeout ? threadWaitTimeout : delegate.getThreadWaitTimeout();
115 }
116
117 public int getPoolExhaustedAction()
118 {
119 return null != poolExhaustedAction ? poolExhaustedAction : delegate.getPoolExhaustedAction();
120 }
121
122 public RejectedExecutionHandler getRejectedExecutionHandler()
123 {
124 return rejectedExecutionHandler;
125 }
126
127 public ThreadFactory getThreadFactory()
128 {
129 return threadFactory;
130 }
131
132 public void setMaxThreadsActive(int maxThreadsActive)
133 {
134 this.maxThreadsActive = maxThreadsActive;
135 }
136
137 public void setMaxThreadsIdle(int maxThreadsIdle)
138 {
139 this.maxThreadsIdle = maxThreadsIdle;
140 }
141
142 public void setThreadTTL(long threadTTL)
143 {
144 this.threadTTL = threadTTL;
145 }
146
147 public void setThreadWaitTimeout(long threadWaitTimeout)
148 {
149 this.threadWaitTimeout = threadWaitTimeout;
150 }
151
152 public void setPoolExhaustedAction(int poolExhaustPolicy)
153 {
154 this.poolExhaustedAction = poolExhaustPolicy;
155 }
156
157 public void setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler)
158 {
159 this.rejectedExecutionHandler = rejectedExecutionHandler;
160 }
161
162 public void setThreadFactory(ThreadFactory threadFactory)
163 {
164 this.threadFactory = threadFactory;
165 }
166
167 public int getMaxBufferSize()
168 {
169 return null != maxBufferSize ? maxBufferSize : delegate.getMaxBufferSize();
170 }
171
172 public void setMaxBufferSize(int maxBufferSize)
173 {
174 this.maxBufferSize = maxBufferSize;
175 }
176
177 public WorkManagerFactory getWorkManagerFactory()
178 {
179 return workManagerFactory;
180 }
181
182 public void setWorkManagerFactory(WorkManagerFactory workManagerFactory)
183 {
184 this.workManagerFactory = workManagerFactory;
185 }
186
187 public WorkManager createWorkManager(String name, int shutdownTimeout)
188 {
189
190 return workManagerFactory.createWorkManager(new ImmutableThreadingProfile(this), name, shutdownTimeout);
191 }
192
193 public ExecutorService createPool()
194 {
195 return createPool(null);
196 }
197
198 public ExecutorService createPool(String name)
199 {
200
201 return poolFactory.createPool(name, new ImmutableThreadingProfile(this));
202 }
203
204 public boolean isDoThreading()
205 {
206 return null != doThreading ? doThreading : delegate.isDoThreading();
207 }
208
209 public void setDoThreading(boolean doThreading)
210 {
211 this.doThreading = doThreading;
212 }
213
214 public ThreadPoolFactory getPoolFactory()
215 {
216 return poolFactory;
217 }
218
219 public MuleContext getMuleContext()
220 {
221 return muleContext;
222 }
223
224 public void setMuleContext(MuleContext muleContext)
225 {
226 this.muleContext = muleContext;
227
228
229 if (this.workManagerFactory instanceof MuleContextAware)
230 {
231 ((MuleContextAware) workManagerFactory).setMuleContext(muleContext);
232 }
233
234 poolFactory.setMuleContext(muleContext);
235 }
236
237 public String toString()
238 {
239 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 }