001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  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    package org.apache.activemq.thread;
018    
019    import java.util.concurrent.Executor;
020    import java.util.concurrent.ExecutorService;
021    import java.util.concurrent.SynchronousQueue;
022    import java.util.concurrent.ThreadFactory;
023    import java.util.concurrent.ThreadPoolExecutor;
024    import java.util.concurrent.TimeUnit;
025    
026    /**
027     * Manages the thread pool for long running tasks. Long running tasks are not
028     * always active but when they are active, they may need a few iterations of
029     * processing for them to become idle. The manager ensures that each task is
030     * processes but that no one task overtakes the system. This is kina like
031     * cooperative multitasking.
032     * 
033     * @version $Revision: 1.5 $
034     */
035    public class TaskRunnerFactory implements Executor {
036    
037        private ExecutorService executor;
038        private int maxIterationsPerRun;
039        private String name;
040        private int priority;
041        private boolean daemon;
042    
043        public TaskRunnerFactory() {
044            this("ActiveMQ Task", Thread.NORM_PRIORITY, true, 1000);
045        }
046        
047        private TaskRunnerFactory(String name, int priority, boolean daemon, int maxIterationsPerRun) {
048            this(name,priority,daemon,maxIterationsPerRun,false);
049        }
050    
051    
052        public TaskRunnerFactory(String name, int priority, boolean daemon, int maxIterationsPerRun, boolean dedicatedTaskRunner) {
053    
054            this.name = name;
055            this.priority = priority;
056            this.daemon = daemon;
057            this.maxIterationsPerRun = maxIterationsPerRun;
058    
059            // If your OS/JVM combination has a good thread model, you may want to
060            // avoid
061            // using a thread pool to run tasks and use a DedicatedTaskRunner
062            // instead.
063            if (dedicatedTaskRunner || "true".equalsIgnoreCase(System.getProperty("org.apache.activemq.UseDedicatedTaskRunner"))) {
064                executor = null;
065            } else {
066                executor = createDefaultExecutor();
067            }
068        }
069    
070        public void shutdown() {
071            if (executor != null) {
072                executor.shutdownNow();
073            }
074        }
075    
076        public TaskRunner createTaskRunner(Task task, String name) {
077            if (executor != null) {
078                return new PooledTaskRunner(executor, task, maxIterationsPerRun);
079            } else {
080                return new DedicatedTaskRunner(task, name, priority, daemon);
081            }
082        }
083    
084        public void execute(Runnable runnable) {
085            execute(runnable, "ActiveMQ Task");
086        }
087        
088        public void execute(Runnable runnable, String name) {
089            if (executor != null) {
090                executor.execute(runnable);
091            } else {
092                new Thread(runnable, name).start();
093            }
094        }
095    
096        protected ExecutorService createDefaultExecutor() {
097            ThreadPoolExecutor rc = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 10, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactory() {
098                public Thread newThread(Runnable runnable) {
099                    Thread thread = new Thread(runnable, name);
100                    thread.setDaemon(daemon);
101                    thread.setPriority(priority);
102                    return thread;
103                }
104            });
105            // rc.allowCoreThreadTimeOut(true);
106            return rc;
107        }
108    
109    }