View Javadoc

1   /*
2    * $Id: DefaultThreadPoolFactory.java 21939 2011-05-18 13:32:09Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.pool;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.config.ThreadingProfile;
15  import org.mule.util.StringUtils;
16  import org.mule.util.concurrent.NamedThreadFactory;
17  import org.mule.util.concurrent.WaitPolicy;
18  
19  import java.util.concurrent.BlockingQueue;
20  import java.util.concurrent.LinkedBlockingDeque;
21  import java.util.concurrent.SynchronousQueue;
22  import java.util.concurrent.ThreadPoolExecutor;
23  import java.util.concurrent.TimeUnit;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29  *
30  */
31  public class DefaultThreadPoolFactory extends ThreadPoolFactory
32  {
33      // deliberately shadow the superclass' static logger as to avoid log congestion on it
34      protected final Log logger = LogFactory.getLog(getClass());
35  
36      public ThreadPoolExecutor createPool(String name, ThreadingProfile tp)
37      {
38          BlockingQueue buffer;
39  
40          if (tp.getMaxBufferSize() > 0 && tp.getMaxThreadsActive() > 1)
41          {
42              buffer = new LinkedBlockingDeque(tp.getMaxBufferSize());
43          }
44          else
45          {
46              buffer = new SynchronousQueue();
47          }
48  
49          ThreadPoolExecutor pool = internalCreatePool(name, tp, buffer);
50          configureThreadFactory(name, tp, pool);
51  
52  
53          if (tp.getRejectedExecutionHandler() != null)
54          {
55              pool.setRejectedExecutionHandler(tp.getRejectedExecutionHandler());
56          }
57          else
58          {
59              switch (tp.getPoolExhaustedAction())
60              {
61                  case ThreadingProfile.WHEN_EXHAUSTED_DISCARD_OLDEST :
62                      pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
63                      break;
64                  case ThreadingProfile.WHEN_EXHAUSTED_RUN :
65                      pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
66                      break;
67                  case ThreadingProfile.WHEN_EXHAUSTED_ABORT :
68                      pool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
69                      break;
70                  case ThreadingProfile.WHEN_EXHAUSTED_DISCARD :
71                      pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
72                      break;
73                  default :
74                      // WHEN_EXHAUSTED_WAIT
75                      pool.setRejectedExecutionHandler(new WaitPolicy(tp.getThreadWaitTimeout(), TimeUnit.MILLISECONDS));
76                      break;
77              }
78          }
79  
80          return pool;
81  
82      }
83  
84      protected void configureThreadFactory(String name, ThreadingProfile tp, ThreadPoolExecutor pool)
85      {
86          // use a custom ThreadFactory if one has been configured
87          if (tp.getThreadFactory() != null)
88          {
89              pool.setThreadFactory(tp.getThreadFactory());
90          }
91          else
92          {
93              // ..else create a "NamedThreadFactory" if a proper name was passed in
94              if (StringUtils.isNotBlank(name))
95              {
96                  // Use MuleContext classloader so that other temporary classloaders
97                  // aren't used when things are started lazily or from elsewhere.
98                  pool.setThreadFactory(new NamedThreadFactory(name, MuleContext.class.getClassLoader()));
99              }
100             else
101             {
102                 // let ThreadPoolExecutor create a default ThreadFactory;
103                 // see Executors.defaultThreadFactory()
104             }
105         }
106     }
107 
108     protected ThreadPoolExecutor internalCreatePool(String name, ThreadingProfile tp, BlockingQueue buffer)
109     {
110         return new ThreadPoolExecutor(Math.min(tp.getMaxThreadsIdle(), tp.getMaxThreadsActive()),
111                                       tp.getMaxThreadsActive(), tp.getThreadTTL(),
112                                       TimeUnit.MILLISECONDS, buffer);
113 
114     }
115 }