1 17 package org.alfresco.util; 18 19 import java.util.concurrent.ArrayBlockingQueue ; 20 import java.util.concurrent.BlockingQueue ; 21 import java.util.concurrent.ThreadPoolExecutor ; 22 import java.util.concurrent.TimeUnit ; 23 24 import org.alfresco.error.AlfrescoRuntimeException; 25 import org.springframework.beans.factory.FactoryBean; 26 import org.springframework.beans.factory.InitializingBean; 27 28 36 public class ThreadPoolExecutorFactoryBean implements FactoryBean, InitializingBean 37 { 38 private int corePoolSize; 39 private int maximumPoolSize; 40 private int keepAliveTime; 41 private BlockingQueue <Runnable > workQueue; 42 private ThreadPoolExecutor instance; 43 44 53 public ThreadPoolExecutorFactoryBean() 54 { 55 corePoolSize = 5; 56 maximumPoolSize = 20; 57 keepAliveTime = 30; 58 } 59 60 65 public void setCorePoolSize(int corePoolSize) 66 { 67 this.corePoolSize = corePoolSize; 68 } 69 70 75 public void setMaximumPoolSize(int maximumPoolSize) 76 { 77 this.maximumPoolSize = maximumPoolSize; 78 } 79 80 85 public void setKeepAliveTime(int keepAliveTime) 86 { 87 this.keepAliveTime = keepAliveTime; 88 } 89 90 95 public void setWorkQueue(BlockingQueue <Runnable > workQueue) 96 { 97 this.workQueue = workQueue; 98 } 99 100 public void afterPropertiesSet() throws Exception 101 { 102 if (workQueue == null) 103 { 104 workQueue = new ArrayBlockingQueue <Runnable >(corePoolSize); 105 } 106 instance = new ThreadPoolExecutor (corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, workQueue); 108 } 109 110 113 public boolean isSingleton() 114 { 115 return true; 116 } 117 118 121 public Object getObject() throws Exception 122 { 123 if (instance == null) 124 { 125 throw new AlfrescoRuntimeException("The ThreadPoolExecutor instance has not been created"); 126 } 127 return instance; 128 } 129 130 133 public Class getObjectType() 134 { 135 return ThreadPoolExecutor .class; 136 } 137 } 138 | Popular Tags |