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