KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > work > pool > WorkExecutorPoolImpl


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.connector.work.pool;
19
20 import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor;
21 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
22 import edu.emory.mathcs.backport.java.util.concurrent.LinkedBlockingQueue;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 /**
28  * Based class for WorkExecutorPool. Sub-classes define the synchronization
29  * policy (should the call block until the end of the work; or when it starts
30  * et cetera).
31  *
32  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
33  */

34 public class WorkExecutorPoolImpl implements WorkExecutorPool {
35
36     /**
37      * A timed out pooled executor.
38      */

39     private ThreadPoolExecutor pooledExecutor;
40     private static Log log = LogFactory.getLog(WorkExecutorPoolImpl.class);
41
42     /**
43      * Creates a pool with the specified minimum and maximum sizes. The Channel
44      * used to enqueue the submitted Work instances is queueless synchronous
45      * one.
46      *
47      * @param maxSize Maximum size of the work executor pool.
48      */

49     public WorkExecutorPoolImpl(int maxSize) {
50         pooledExecutor = new ThreadPoolExecutor(1, maxSize, 1, TimeUnit.MINUTES, new LinkedBlockingQueue());
51         /*
52
53         FIXME: How to do this with concurrent.util ?
54         pooledExecutor.waitWhenBlocked();
55         */

56     }
57     
58     /**
59      * Execute the specified Work.
60      *
61      * @param work Work to be executed.
62      */

63     public void execute(Runnable JavaDoc work) {
64         if(pooledExecutor.getPoolSize() == pooledExecutor.getMaximumPoolSize()) {
65             log.warn("Maximum Pool size has been exceeded. Current Pool Size = "+pooledExecutor.getMaximumPoolSize());
66         }
67
68         pooledExecutor.execute(work);
69     }
70
71     /**
72      * Gets the size of this pool.
73      */

74     public int getPoolSize() {
75         return pooledExecutor.getPoolSize();
76     }
77
78     /**
79      * Gets the maximum size of this pool.
80      */

81     public int getMaximumPoolSize() {
82         return pooledExecutor.getMaximumPoolSize();
83     }
84
85     /**
86      * Sets the maximum size of this pool.
87      * @param maxSize New maximum size of this pool.
88      */

89     public void setMaximumPoolSize(int maxSize) {
90         pooledExecutor.setMaximumPoolSize(maxSize);
91     }
92
93     public WorkExecutorPool start() {
94         throw new IllegalStateException JavaDoc("This pooled executor is already started");
95     }
96
97     /**
98      * Stops this pool. Prior to stop this pool, all the enqueued Work instances
99      * are processed. This is an orderly shutdown.
100      */

101     public WorkExecutorPool stop() {
102         int maxSize = getMaximumPoolSize();
103         pooledExecutor.shutdown();
104         return new NullWorkExecutorPool(maxSize);
105     }
106
107 }
108
Popular Tags