001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    package org.activemq.util;
019    
020    import EDU.oswego.cs.dl.util.concurrent.Executor;
021    import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.activemq.service.Service;
025    
026    import javax.jms.JMSException;
027    
028    /**
029     * A helper class for working with  {@link Executor} objects
030     *
031     * @version $Revision: 1.1.1.1 $
032     */
033    public class ExecutorHelper {
034        private static final Log log = LogFactory.getLog(ExecutorHelper.class);
035    
036        /**
037         * A helper method to stop the execution of an executor
038         *
039         * @param executor the executor or null if one is not created yet
040         */
041        public static void stopExecutor(Executor executor) throws InterruptedException, JMSException {
042            if (executor instanceof Service) {
043                Service service = (Service) executor;
044                service.stop();
045            }
046            else if (executor instanceof PooledExecutor) {
047                PooledExecutor pe = (PooledExecutor) executor;
048                pe.shutdownAfterProcessingCurrentlyQueuedTasks();
049                //pe.shutdownNow();
050                // Wait up to 5 seconds of the threads to shutdown..
051                pe.awaitTerminationAfterShutdown(1000*5);
052            }
053            else if (executor != null) {
054                log.warn("Don't know how to cleanly close down the given executor: " + executor
055                        + ". Consider deriving from this class to implement the Service interface to shut down cleanly");
056            }
057        }
058    
059    }