View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
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  /**
35   * Spring {@link FactoryBean} which enables the configuration of
36   * {@link ThreadPoolExecutor} instances using Spring. Most of this code
37   * has been copied from the <code>ThreadPoolTaskExecutor</code> class
38   * available in Spring 2.0.
39   *
40   * @author The Apache Directory Project (mina-dev@directory.apache.org)
41   * @version $Rev$, $Date$
42   */
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      /**
57       * Set the ThreadPoolExecutor's core pool size. Default is 1.
58       */
59      public void setCorePoolSize(int corePoolSize) {
60          this.corePoolSize = corePoolSize;
61      }
62  
63      /**
64       * Set the ThreadPoolExecutor's maximum pool size. Default is
65       * <code>Integer.MAX_VALUE</code>.
66       */
67      public void setMaxPoolSize(int maxPoolSize) {
68          this.maxPoolSize = maxPoolSize;
69      }
70  
71      /**
72       * Set the ThreadPoolExecutor's keep alive seconds. Default is 60.
73       */
74      public void setKeepAliveSeconds(int keepAliveSeconds) {
75          this.keepAliveSeconds = keepAliveSeconds;
76      }
77  
78      /**
79       * Set the capacity for the ThreadPoolExecutor's BlockingQueue. Default is
80       * <code>Integer.MAX_VALUE</code>.
81       * <p>
82       * Any positive value will lead to a LinkedBlockingQueue instance; any other
83       * value will lead to a SynchronousQueue instance.
84       *
85       * @see LinkedBlockingQueue
86       * @see SynchronousQueue
87       */
88      public void setQueueCapacity(int queueCapacity) {
89          this.queueCapacity = queueCapacity;
90      }
91  
92      /**
93       * Set the ThreadFactory to use for the ThreadPoolExecutor's thread pool.
94       * Default is the ThreadPoolExecutor's default thread factory.
95       *
96       * @see Executors#defaultThreadFactory()
97       */
98      public void setThreadFactory(ThreadFactory threadFactory) {
99          this.threadFactory = (threadFactory != null ? threadFactory : Executors
100                 .defaultThreadFactory());
101     }
102 
103     /**
104      * Set the RejectedExecutionHandler to use for the ThreadPoolExecutor.
105      * Default is the ThreadPoolExecutor's default abort policy.
106      *
107      * @see ThreadPoolExecutor.AbortPolicy
108      */
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 }