1 /** 2 * 3 * Copyright 2003-2004 The Apache Software Foundation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * 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 javax.resource.spi.work.WorkCompletedException; 21 import javax.resource.spi.work.WorkException; 22 23 import EDU.oswego.cs.dl.util.concurrent.Channel; 24 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor; 25 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue; 26 import org.apache.geronimo.connector.work.WorkerContext; 27 28 /** 29 * Based class for WorkExecutorPool. Sub-classes define the synchronization 30 * policy (should the call block until the end of the work; or when it starts 31 * et cetera). 32 * 33 * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $ 34 */ 35 public class WorkExecutorPoolImpl implements WorkExecutorPool { 36 37 /** 38 * A timed out pooled executor. 39 */ 40 private PooledExecutor pooledExecutor; 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 PooledExecutor(new LinkedQueue(), maxSize); 51 pooledExecutor.setMinimumPoolSize(maxSize); 52 pooledExecutor.waitWhenBlocked(); 53 } 54 55 /** 56 * Creates a pool with the specified minimum and maximum sizes and using the 57 * specified Channel to enqueue the submitted Work instances. 58 * 59 * @param channel Queue to be used as the queueing facility of this pool. 60 * @param maxSize Maximum size of the work executor pool. 61 */ 62 public WorkExecutorPoolImpl( 63 Channel channel, 64 int maxSize) { 65 pooledExecutor = new PooledExecutor(channel, maxSize); 66 pooledExecutor.setMinimumPoolSize(maxSize); 67 pooledExecutor.waitWhenBlocked(); 68 } 69 70 /** 71 * Execute the specified Work. 72 * 73 * @param work Work to be executed. 74 * 75 * @exception InterruptedException Indicates that the Work execution has been 76 * unsuccessful. 77 */ 78 public void execute(Runnable work) throws InterruptedException { 79 pooledExecutor.execute(work); 80 } 81 82 /** 83 * Gets the size of this pool. 84 */ 85 public int getPoolSize() { 86 return pooledExecutor.getPoolSize(); 87 } 88 89 /** 90 * Gets the maximum size of this pool. 91 */ 92 public int getMaximumPoolSize() { 93 return pooledExecutor.getMaximumPoolSize(); 94 } 95 96 /** 97 * Sets the maximum size of this pool. 98 * @param maxSize New maximum size of this pool. 99 */ 100 public void setMaximumPoolSize(int maxSize) { 101 pooledExecutor.setMaximumPoolSize(maxSize); 102 } 103 104 public WorkExecutorPool start() { 105 throw new IllegalStateException("This pooled executor is already started"); 106 } 107 108 /** 109 * Stops this pool. Prior to stop this pool, all the enqueued Work instances 110 * are processed. This is an orderly shutdown. 111 */ 112 public WorkExecutorPool stop() { 113 int maxSize = getMaximumPoolSize(); 114 pooledExecutor.shutdownAfterProcessingCurrentlyQueuedTasks(); 115 return new NullWorkExecutorPool(maxSize); 116 } 117 118 }