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