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 EDU.oswego.cs.dl.util.concurrent.Channel; 21 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor; 22 import org.apache.geronimo.connector.work.WorkerContext; 23 24 /** 25 * PooledExecutor enforcing a timed out "blocked execution policy". The works 26 * submitted to this pooled executor MUST be a WorkWrapper. 27 * 28 * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $ 29 */ 30 public class TimedOutPooledExecutor extends PooledExecutor { 31 32 /** 33 * Creates a pooled executor. The Channel used to enqueue the submitted 34 * Work instance is a queueless synchronous one. 35 */ 36 public TimedOutPooledExecutor() { 37 setBlockedExecutionHandler(new TimedOutSpinHandler()); 38 } 39 40 /** 41 * Creates a pooled executor, which uses the provided Channel as its 42 * queueing mechanism. 43 * 44 * @param aChannel Channel to be used to enqueue the submitted Work 45 * intances. 46 */ 47 public TimedOutPooledExecutor(Channel aChannel) { 48 super(aChannel); 49 setBlockedExecutionHandler(new TimedOutSpinHandler()); 50 } 51 52 /** 53 * Executes the provided task, which MUST be an instance of WorkWrapper. 54 * 55 * @throws IllegalArgumentException Indicates that the provided task is not 56 * a WorkWrapper instance. 57 */ 58 public void execute(Runnable aTask) throws InterruptedException { 59 if (!(aTask instanceof WorkerContext)) { 60 throw new IllegalArgumentException("Please submit a WorkWrapper."); 61 } 62 super.execute(aTask); 63 } 64 65 /** 66 * This class implements a time out policy when a work is blocked: it offers 67 * the task to the pool until the work has timed out. 68 * 69 * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $ 70 */ 71 private class TimedOutSpinHandler 72 implements PooledExecutor.BlockedExecutionHandler { 73 74 /* (non-Javadoc) 75 * @see EDU.oswego.cs.dl.util.concurrent.PooledExecutor.BlockedExecutionHandler#blockedAction(java.lang.Runnable) 76 */ 77 public boolean blockedAction(Runnable arg0) throws InterruptedException { 78 WorkerContext work = (WorkerContext) arg0; 79 if (!handOff_.offer(arg0, work.getStartTimeout())) { 80 // double check. 81 if (work.isTimedOut()) { 82 return false; 83 } 84 return true; 85 } 86 return true; 87 } 88 } 89 }