KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > util > ThreadPoolExecutorFactoryBean


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.util;
18
19 import java.util.concurrent.ArrayBlockingQueue JavaDoc;
20 import java.util.concurrent.BlockingQueue JavaDoc;
21 import java.util.concurrent.ThreadPoolExecutor JavaDoc;
22 import java.util.concurrent.TimeUnit JavaDoc;
23
24 import org.alfresco.error.AlfrescoRuntimeException;
25 import org.springframework.beans.factory.FactoryBean;
26 import org.springframework.beans.factory.InitializingBean;
27
28 /**
29  * Factory for {@link java.util.concurrent.ThreadPoolExecutor} instances,
30  * which cannot easily be constructed using constructor injection.
31  * <p>
32  * This factory provides the a singleton instance of the pool.
33  *
34  * @author Derek Hulley
35  */

36 public class ThreadPoolExecutorFactoryBean implements FactoryBean, InitializingBean
37 {
38     private int corePoolSize;
39     private int maximumPoolSize;
40     private int keepAliveTime;
41     private BlockingQueue JavaDoc<Runnable JavaDoc> workQueue;
42     private ThreadPoolExecutor JavaDoc instance;
43     
44     /**
45      * Constructor setting default properties:
46      * <ul>
47      * <li>corePoolSize: 5</li>
48      * <li>maximumPoolSize: 20</li>
49      * <li>keepAliveTime: 60s</li>
50      * <li>workQueue: {@link ArrayBlockingQueue}</li>
51      * </ul>
52      */

53     public ThreadPoolExecutorFactoryBean()
54     {
55         corePoolSize = 5;
56         maximumPoolSize = 20;
57         keepAliveTime = 30;
58     }
59     
60     /**
61      * The number of threads to keep in the pool, even if idle.
62      *
63      * @param corePoolSize core thread count
64      */

65     public void setCorePoolSize(int corePoolSize)
66     {
67         this.corePoolSize = corePoolSize;
68     }
69
70     /**
71      * The maximum number of threads to keep in the pool
72      *
73      * @param maximumPoolSize the maximum number of threads in the pool
74      */

75     public void setMaximumPoolSize(int maximumPoolSize)
76     {
77         this.maximumPoolSize = maximumPoolSize;
78     }
79
80     /**
81      * The time (in seconds) to keep non-core idle threads in the pool
82      *
83      * @param keepAliveTime time to stay idle in seconds
84      */

85     public void setKeepAliveTime(int keepAliveTime)
86     {
87         this.keepAliveTime = keepAliveTime;
88     }
89
90     /**
91      * The optional queue instance to use
92      *
93      * @param workQueue optional queue implementation
94      */

95     public void setWorkQueue(BlockingQueue JavaDoc<Runnable JavaDoc> workQueue)
96     {
97         this.workQueue = workQueue;
98     }
99
100     public void afterPropertiesSet() throws Exception JavaDoc
101     {
102         if (workQueue == null)
103         {
104             workQueue = new ArrayBlockingQueue JavaDoc<Runnable JavaDoc>(corePoolSize);
105         }
106         // construct the instance
107
instance = new ThreadPoolExecutor JavaDoc(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, workQueue);
108     }
109
110     /**
111      * @return Returns true always.
112      */

113     public boolean isSingleton()
114     {
115         return true;
116     }
117
118     /**
119      * @return Returns the singleton {@link ThreadPoolExecutor instance}.
120      */

121     public Object JavaDoc getObject() throws Exception JavaDoc
122     {
123         if (instance == null)
124         {
125             throw new AlfrescoRuntimeException("The ThreadPoolExecutor instance has not been created");
126         }
127         return instance;
128     }
129
130     /**
131      * @see ThreadPoolExecutor
132      */

133     public Class JavaDoc getObjectType()
134     {
135         return ThreadPoolExecutor JavaDoc.class;
136     }
137 }
138
Popular Tags