1 20 package org.apache.mina.integration.spring; 21 22 import org.springframework.beans.factory.FactoryBean; 23 import org.springframework.beans.factory.config.AbstractFactoryBean; 24 25 import java.util.concurrent.BlockingQueue ; 26 import java.util.concurrent.Executors ; 27 import java.util.concurrent.LinkedBlockingQueue ; 28 import java.util.concurrent.RejectedExecutionHandler ; 29 import java.util.concurrent.SynchronousQueue ; 30 import java.util.concurrent.ThreadFactory ; 31 import java.util.concurrent.ThreadPoolExecutor ; 32 import java.util.concurrent.TimeUnit ; 33 34 43 public class ThreadPoolExecutorFactoryBean extends AbstractFactoryBean { 44 private int corePoolSize = 1; 45 46 private int maxPoolSize = Integer.MAX_VALUE; 47 48 private int keepAliveSeconds = 60; 49 50 private int queueCapacity = Integer.MAX_VALUE; 51 52 private ThreadFactory threadFactory = Executors.defaultThreadFactory(); 53 54 private RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.AbortPolicy (); 55 56 59 public void setCorePoolSize(int corePoolSize) { 60 this.corePoolSize = corePoolSize; 61 } 62 63 67 public void setMaxPoolSize(int maxPoolSize) { 68 this.maxPoolSize = maxPoolSize; 69 } 70 71 74 public void setKeepAliveSeconds(int keepAliveSeconds) { 75 this.keepAliveSeconds = keepAliveSeconds; 76 } 77 78 88 public void setQueueCapacity(int queueCapacity) { 89 this.queueCapacity = queueCapacity; 90 } 91 92 98 public void setThreadFactory(ThreadFactory threadFactory) { 99 this.threadFactory = (threadFactory != null ? threadFactory : Executors 100 .defaultThreadFactory()); 101 } 102 103 109 public void setRejectedExecutionHandler( 110 RejectedExecutionHandler rejectedExecutionHandler) { 111 this.rejectedExecutionHandler = (rejectedExecutionHandler != null ? rejectedExecutionHandler 112 : new ThreadPoolExecutor.AbortPolicy ()); 113 } 114 115 protected Object createInstance() throws Exception { 116 BlockingQueue <Runnable > queue = null; 117 if (queueCapacity > 0) { 118 queue = new LinkedBlockingQueue <Runnable >(queueCapacity); 119 } else { 120 queue = new SynchronousQueue <Runnable >(); 121 } 122 return new ThreadPoolExecutor (corePoolSize, maxPoolSize, 123 keepAliveSeconds, TimeUnit.SECONDS, queue, threadFactory, 124 rejectedExecutionHandler); 125 } 126 127 protected void destroyInstance(Object o) throws Exception { 128 ThreadPoolExecutor executor = (ThreadPoolExecutor ) o; 129 executor.shutdown(); 130 } 131 132 public Class getObjectType() { 133 return ThreadPoolExecutor .class; 134 } 135 136 } 137 | Popular Tags |