Coverage Report - org.mule.config.ImmutableThreadingProfile
 
Classes in this File Line Coverage Branch Coverage Complexity
ImmutableThreadingProfile
54%
32/59
33%
5/15
1.643
ImmutableThreadingProfile$DefaultWorkManagerFactory
100%
2/2
N/A
1.643
 
 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  2
     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  2
     {
 54  2
         this.maxThreadsActive = maxThreadsActive;
 55  2
         this.maxThreadsIdle = maxThreadsIdle;
 56  2
         this.maxBufferSize = maxBufferSize;
 57  2
         this.threadTTL = threadTTL;
 58  2
         this.threadWaitTimeout = threadWaitTimeout;
 59  2
         this.poolExhaustedAction = poolExhaustedAction;
 60  2
         this.doThreading = doThreading;
 61  2
         this.rejectedExecutionHandler = rejectedExecutionHandler;
 62  2
         this.threadFactory = threadFactory;
 63  2
     }
 64  
 
 65  
     public ImmutableThreadingProfile(ThreadingProfile tp)
 66  
     {
 67  0
         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  0
     }
 77  
 
 78  
     public int getMaxThreadsActive()
 79  
     {
 80  2332
         return maxThreadsActive;
 81  
     }
 82  
 
 83  
     public int getMaxThreadsIdle()
 84  
     {
 85  1166
         return maxThreadsIdle;
 86  
     }
 87  
 
 88  
     public long getThreadTTL()
 89  
     {
 90  1166
         return threadTTL;
 91  
     }
 92  
 
 93  
     public long getThreadWaitTimeout()
 94  
     {
 95  0
         return threadWaitTimeout;
 96  
     }
 97  
 
 98  
     public int getPoolExhaustedAction()
 99  
     {
 100  1166
         return poolExhaustedAction;
 101  
     }
 102  
 
 103  
     public RejectedExecutionHandler getRejectedExecutionHandler()
 104  
     {
 105  1166
         return rejectedExecutionHandler;
 106  
     }
 107  
 
 108  
     public ThreadFactory getThreadFactory()
 109  
     {
 110  1166
         return threadFactory;
 111  
     }
 112  
 
 113  
     public void setMaxThreadsActive(int maxThreadsActive)
 114  
     {
 115  0
         throw new UnsupportedOperationException(getClass().getName());
 116  
     }
 117  
 
 118  
     public void setMaxThreadsIdle(int maxThreadsIdle)
 119  
     {
 120  0
         throw new UnsupportedOperationException(getClass().getName());
 121  
     }
 122  
 
 123  
     public void setThreadTTL(long threadTTL)
 124  
     {
 125  0
         throw new UnsupportedOperationException(getClass().getName());
 126  
     }
 127  
 
 128  
     public void setThreadWaitTimeout(long threadWaitTimeout)
 129  
     {
 130  0
         throw new UnsupportedOperationException(getClass().getName());
 131  
     }
 132  
 
 133  
     public void setPoolExhaustedAction(int poolExhaustPolicy)
 134  
     {
 135  0
         throw new UnsupportedOperationException(getClass().getName());
 136  
     }
 137  
 
 138  
     public void setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler)
 139  
     {
 140  0
         throw new UnsupportedOperationException(getClass().getName());
 141  
     }
 142  
 
 143  
     public void setThreadFactory(ThreadFactory threadFactory)
 144  
     {
 145  0
         throw new UnsupportedOperationException(getClass().getName());
 146  
     }
 147  
 
 148  
     public int getMaxBufferSize()
 149  
     {
 150  1166
         return maxBufferSize;
 151  
     }
 152  
 
 153  
     public void setMaxBufferSize(int maxBufferSize)
 154  
     {
 155  0
         throw new UnsupportedOperationException(getClass().getName());
 156  
     }
 157  
 
 158  
     public WorkManagerFactory getWorkManagerFactory()
 159  
     {
 160  0
         return workManagerFactory;
 161  
     }
 162  
 
 163  
     public void setWorkManagerFactory(WorkManagerFactory workManagerFactory)
 164  
     {
 165  0
         throw new UnsupportedOperationException(getClass().getName());
 166  
     }
 167  
 
 168  
     public WorkManager createWorkManager(String name)
 169  
     {
 170  0
         return workManagerFactory.createWorkManager(this, name);
 171  
     }
 172  
 
 173  
     public ThreadPoolExecutor createPool()
 174  
     {
 175  0
         return createPool(null);
 176  
     }
 177  
 
 178  
     public ThreadPoolExecutor createPool(String name)
 179  
     {
 180  1166
         return createPool(name, this);
 181  
     }
 182  
 
 183  
     public boolean isDoThreading()
 184  
     {
 185  2
         return doThreading;
 186  
     }
 187  
 
 188  
     public void setDoThreading(boolean doThreading)
 189  
     {
 190  0
         throw new UnsupportedOperationException(getClass().getName());
 191  
     }
 192  
 
 193  
     public String toString()
 194  
     {
 195  0
         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  5732
     public static class DefaultWorkManagerFactory implements WorkManagerFactory
 204  
     {
 205  
 
 206  
         public WorkManager createWorkManager(ThreadingProfile profile, String name)
 207  
         {
 208  1334
             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  2124
         if (tp.getMaxBufferSize() > 0 && tp.getMaxThreadsActive() > 1)
 220  
         {
 221  0
             buffer = new LinkedBlockingDeque(tp.getMaxBufferSize());
 222  
         }
 223  
         else
 224  
         {
 225  2124
             buffer = new SynchronousQueue();
 226  
         }
 227  
 
 228  2124
         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  2124
         if (tp.getThreadFactory() != null)
 235  
         {
 236  0
             pool.setThreadFactory(tp.getThreadFactory());
 237  
         }
 238  
         else
 239  
         {
 240  
             // ..else create a "NamedThreadFactory" if a proper name was passed in
 241  2124
             if (StringUtils.isNotBlank(name))
 242  
             {
 243  2124
                 pool.setThreadFactory(new NamedThreadFactory(name)); 
 244  
             }
 245  
             else
 246  
             {
 247  
                 // let ThreadPoolExecutor create a default ThreadFactory;
 248  
                 // see Executors.defaultThreadFactory()
 249  
             }
 250  
         }
 251  
 
 252  2124
         if (tp.getRejectedExecutionHandler() != null)
 253  
         {
 254  0
             pool.setRejectedExecutionHandler(tp.getRejectedExecutionHandler());
 255  
         }
 256  
         else
 257  
         {
 258  2124
             switch (tp.getPoolExhaustedAction())
 259  
             {
 260  
                 case WHEN_EXHAUSTED_DISCARD_OLDEST :
 261  0
                     pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
 262  0
                     break;
 263  
                 case WHEN_EXHAUSTED_RUN :
 264  2124
                     pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
 265  2124
                     break;
 266  
                 case WHEN_EXHAUSTED_ABORT :
 267  0
                     pool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
 268  0
                     break;
 269  
                 case WHEN_EXHAUSTED_DISCARD :
 270  0
                     pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
 271  0
                     break;
 272  
                 default :
 273  
                     // WHEN_EXHAUSTED_WAIT
 274  0
                     pool.setRejectedExecutionHandler(new WaitPolicy(tp.getThreadWaitTimeout(), TimeUnit.MILLISECONDS));
 275  
                     break;
 276  
             }
 277  
         }
 278  
 
 279  2124
         return pool;
 280  
     }
 281  
 
 282  
 }