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