1 10 11 27 28 package org.mule.impl.work; 29 30 import edu.emory.mathcs.backport.java.util.concurrent.BlockingQueue; 31 import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor; 32 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; 33 34 import org.mule.config.ThreadingProfile; 35 import org.mule.util.concurrent.WaitPolicy; 36 37 43 public class WorkExecutorPoolImpl implements WorkExecutorPool 44 { 45 46 49 private ThreadPoolExecutor pooledExecutor; 50 51 private ThreadingProfile profile; 52 53 private String name; 54 55 private static final long SHUTDOWN_TIMEOUT = 5000L; 56 57 65 public WorkExecutorPoolImpl(ThreadingProfile profile, String name) 66 { 67 this.profile = profile; 68 this.name = name; 69 pooledExecutor = profile.createPool(name); 70 } 71 72 79 public WorkExecutorPoolImpl(BlockingQueue queue, int maxSize) 80 { 81 pooledExecutor = new ThreadPoolExecutor(0, maxSize, 60L, TimeUnit.SECONDS, queue); 82 pooledExecutor.setCorePoolSize(maxSize); 83 pooledExecutor.setRejectedExecutionHandler(new WaitPolicy( 84 ThreadingProfile.DEFAULT_THREAD_WAIT_TIMEOUT, TimeUnit.MILLISECONDS)); 85 } 86 87 94 public void execute(Runnable work) 95 { 96 pooledExecutor.execute(work); 97 } 98 99 102 public int getPoolSize() 103 { 104 return pooledExecutor.getPoolSize(); 105 } 106 107 110 public int getMaximumPoolSize() 111 { 112 return pooledExecutor.getMaximumPoolSize(); 113 } 114 115 120 public void setMaximumPoolSize(int maxSize) 121 { 122 pooledExecutor.setMaximumPoolSize(maxSize); 123 } 124 125 public WorkExecutorPool start() 126 { 127 throw new IllegalStateException ("This pooled executor is already started"); 128 } 129 130 135 public WorkExecutorPool stop() 136 { 137 pooledExecutor.shutdownNow(); 138 try 139 { 140 pooledExecutor.awaitTermination(SHUTDOWN_TIMEOUT, TimeUnit.MILLISECONDS); 141 } 142 catch (InterruptedException e) 143 { 144 } 146 return new NullWorkExecutorPool(profile, name); 147 } 148 149 } 150 | Popular Tags |