View Javadoc

1   /*
2    * $Id: ImmutableThreadingProfile.java 10489 2008-01-23 17:53:38Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.config;
12  
13  import org.mule.api.config.ThreadingProfile;
14  import org.mule.api.context.WorkManager;
15  import org.mule.util.StringUtils;
16  import org.mule.util.concurrent.NamedThreadFactory;
17  import org.mule.util.concurrent.WaitPolicy;
18  import org.mule.work.MuleWorkManager;
19  
20  import edu.emory.mathcs.backport.java.util.concurrent.BlockingQueue;
21  import edu.emory.mathcs.backport.java.util.concurrent.LinkedBlockingDeque;
22  import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionHandler;
23  import edu.emory.mathcs.backport.java.util.concurrent.SynchronousQueue;
24  import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory;
25  import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor;
26  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
27  
28  
29  public class ImmutableThreadingProfile implements ThreadingProfile
30  {
31  
32      private int maxThreadsActive;
33      private int maxThreadsIdle;
34      private int maxBufferSize;
35      private long threadTTL;
36      private long threadWaitTimeout;
37      private int poolExhaustedAction;
38      private boolean doThreading;
39  
40      private WorkManagerFactory workManagerFactory = new DefaultWorkManagerFactory();
41      private RejectedExecutionHandler rejectedExecutionHandler;
42      private ThreadFactory threadFactory;
43  
44      public ImmutableThreadingProfile(int maxThreadsActive,
45                              int maxThreadsIdle,
46                              int maxBufferSize,
47                              long threadTTL,
48                              long threadWaitTimeout,
49                              int poolExhaustedAction,
50                              boolean doThreading,
51                              RejectedExecutionHandler rejectedExecutionHandler,
52                              ThreadFactory threadFactory)
53      {
54          this.maxThreadsActive = maxThreadsActive;
55          this.maxThreadsIdle = maxThreadsIdle;
56          this.maxBufferSize = maxBufferSize;
57          this.threadTTL = threadTTL;
58          this.threadWaitTimeout = threadWaitTimeout;
59          this.poolExhaustedAction = poolExhaustedAction;
60          this.doThreading = doThreading;
61          this.rejectedExecutionHandler = rejectedExecutionHandler;
62          this.threadFactory = threadFactory;
63      }
64  
65      public ImmutableThreadingProfile(ThreadingProfile tp)
66      {
67          this(tp.getMaxThreadsActive(),
68                  tp.getMaxThreadsIdle(),
69                  tp.getMaxBufferSize(),
70                  tp.getThreadTTL(),
71                  tp.getThreadWaitTimeout(),
72                  tp.getPoolExhaustedAction(),
73                  tp.isDoThreading(),
74                  tp.getRejectedExecutionHandler(),
75                  tp.getThreadFactory());
76      }
77  
78      public int getMaxThreadsActive()
79      {
80          return maxThreadsActive;
81      }
82  
83      public int getMaxThreadsIdle()
84      {
85          return maxThreadsIdle;
86      }
87  
88      public long getThreadTTL()
89      {
90          return threadTTL;
91      }
92  
93      public long getThreadWaitTimeout()
94      {
95          return threadWaitTimeout;
96      }
97  
98      public int getPoolExhaustedAction()
99      {
100         return poolExhaustedAction;
101     }
102 
103     public RejectedExecutionHandler getRejectedExecutionHandler()
104     {
105         return rejectedExecutionHandler;
106     }
107 
108     public ThreadFactory getThreadFactory()
109     {
110         return threadFactory;
111     }
112 
113     public void setMaxThreadsActive(int maxThreadsActive)
114     {
115         throw new UnsupportedOperationException(getClass().getName());
116     }
117 
118     public void setMaxThreadsIdle(int maxThreadsIdle)
119     {
120         throw new UnsupportedOperationException(getClass().getName());
121     }
122 
123     public void setThreadTTL(long threadTTL)
124     {
125         throw new UnsupportedOperationException(getClass().getName());
126     }
127 
128     public void setThreadWaitTimeout(long threadWaitTimeout)
129     {
130         throw new UnsupportedOperationException(getClass().getName());
131     }
132 
133     public void setPoolExhaustedAction(int poolExhaustPolicy)
134     {
135         throw new UnsupportedOperationException(getClass().getName());
136     }
137 
138     public void setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler)
139     {
140         throw new UnsupportedOperationException(getClass().getName());
141     }
142 
143     public void setThreadFactory(ThreadFactory threadFactory)
144     {
145         throw new UnsupportedOperationException(getClass().getName());
146     }
147 
148     public int getMaxBufferSize()
149     {
150         return maxBufferSize;
151     }
152 
153     public void setMaxBufferSize(int maxBufferSize)
154     {
155         throw new UnsupportedOperationException(getClass().getName());
156     }
157 
158     public WorkManagerFactory getWorkManagerFactory()
159     {
160         return workManagerFactory;
161     }
162 
163     public void setWorkManagerFactory(WorkManagerFactory workManagerFactory)
164     {
165         throw new UnsupportedOperationException(getClass().getName());
166     }
167 
168     public WorkManager createWorkManager(String name)
169     {
170         return workManagerFactory.createWorkManager(this, name);
171     }
172 
173     public ThreadPoolExecutor createPool()
174     {
175         return createPool(null);
176     }
177 
178     public ThreadPoolExecutor createPool(String name)
179     {
180         return createPool(name, this);
181     }
182 
183     public boolean isDoThreading()
184     {
185         return doThreading;
186     }
187 
188     public void setDoThreading(boolean doThreading)
189     {
190         throw new UnsupportedOperationException(getClass().getName());
191     }
192 
193     public String toString()
194     {
195         return "ThreadingProfile{" + "maxThreadsActive=" + maxThreadsActive + ", maxThreadsIdle="
196                         + maxThreadsIdle + ", maxBufferSize=" + maxBufferSize + ", threadTTL=" + threadTTL
197                         + ", poolExhaustedAction=" + poolExhaustedAction + ", threadWaitTimeout="
198                         + threadWaitTimeout + ", doThreading=" + doThreading + ", workManagerFactory="
199                         + workManagerFactory + ", rejectedExecutionHandler=" + rejectedExecutionHandler
200                         + ", threadFactory=" + threadFactory + "}";
201     }
202 
203     public static class DefaultWorkManagerFactory implements WorkManagerFactory
204     {
205 
206         public WorkManager createWorkManager(ThreadingProfile profile, String name)
207         {
208             return new MuleWorkManager(profile, name);
209         }
210 
211     }
212 
213     // this should be a separate factory, really
214     public static ThreadPoolExecutor createPool(String name, ThreadingProfile tp)
215     {
216 
217         BlockingQueue buffer;
218 
219         if (tp.getMaxBufferSize() > 0 && tp.getMaxThreadsActive() > 1)
220         {
221             buffer = new LinkedBlockingDeque(tp.getMaxBufferSize());
222         }
223         else
224         {
225             buffer = new SynchronousQueue();
226         }
227 
228         ThreadPoolExecutor pool =
229                 new ThreadPoolExecutor(Math.max(tp.getMaxThreadsIdle(), tp.getMaxThreadsActive()),
230                         tp.getMaxThreadsActive(), tp.getThreadTTL(),
231                         TimeUnit.MILLISECONDS, buffer);
232 
233         // use a custom ThreadFactory if one has been configured
234         if (tp.getThreadFactory() != null)
235         {
236             pool.setThreadFactory(tp.getThreadFactory());
237         }
238         else
239         {
240             // ..else create a "NamedThreadFactory" if a proper name was passed in
241             if (StringUtils.isNotBlank(name))
242             {
243                 pool.setThreadFactory(new NamedThreadFactory(name)); 
244             }
245             else
246             {
247                 // let ThreadPoolExecutor create a default ThreadFactory;
248                 // see Executors.defaultThreadFactory()
249             }
250         }
251 
252         if (tp.getRejectedExecutionHandler() != null)
253         {
254             pool.setRejectedExecutionHandler(tp.getRejectedExecutionHandler());
255         }
256         else
257         {
258             switch (tp.getPoolExhaustedAction())
259             {
260                 case WHEN_EXHAUSTED_DISCARD_OLDEST :
261                     pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
262                     break;
263                 case WHEN_EXHAUSTED_RUN :
264                     pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
265                     break;
266                 case WHEN_EXHAUSTED_ABORT :
267                     pool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
268                     break;
269                 case WHEN_EXHAUSTED_DISCARD :
270                     pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
271                     break;
272                 default :
273                     // WHEN_EXHAUSTED_WAIT
274                     pool.setRejectedExecutionHandler(new WaitPolicy(tp.getThreadWaitTimeout(), TimeUnit.MILLISECONDS));
275                     break;
276             }
277         }
278 
279         return pool;
280     }
281 
282 }