KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > thread > TaskRunnerFactory


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

18 package org.apache.activemq.thread;
19
20 import java.util.concurrent.Executor JavaDoc;
21 import java.util.concurrent.ExecutorService JavaDoc;
22 import java.util.concurrent.SynchronousQueue JavaDoc;
23 import java.util.concurrent.ThreadFactory JavaDoc;
24 import java.util.concurrent.ThreadPoolExecutor JavaDoc;
25 import java.util.concurrent.TimeUnit JavaDoc;
26
27 /**
28  * Manages the thread pool for long running tasks.
29  *
30  * Long running tasks are not always active but when they are active, they may
31  * need a few iterations of processing for them to become idle. The manager
32  * ensures that each task is processes but that no one task overtakes the
33  * system.
34  *
35  * This is kina like cooperative multitasking.
36  *
37  * @version $Revision: 1.5 $
38  */

39 public class TaskRunnerFactory {
40
41     private ExecutorService JavaDoc executor;
42     private int maxIterationsPerRun;
43     private String JavaDoc name;
44     private int priority;
45     private boolean daemon;
46
47     public TaskRunnerFactory() {
48         this("ActiveMQ Task", Thread.NORM_PRIORITY, true, 1000);
49     }
50
51     public TaskRunnerFactory(String JavaDoc name, int priority, boolean daemon, int maxIterationsPerRun) {
52         
53         this.name = name;
54         this.priority = priority;
55         this.daemon = daemon;
56         this.maxIterationsPerRun = maxIterationsPerRun;
57         
58         // If your OS/JVM combination has a good thread model, you may want to avoid
59
// using a thread pool to run tasks and use a DedicatedTaskRunner instead.
60
if( "true".equals(System.getProperty("org.apache.activemq.UseDedicatedTaskRunner")) ) {
61             executor = null;
62         } else {
63             executor = createDefaultExecutor();
64         }
65     }
66
67     public void shutdown() {
68         if (executor != null) {
69             executor.shutdownNow();
70         }
71     }
72     
73     public TaskRunner createTaskRunner(Task task, String JavaDoc name) {
74         if( executor!=null ) {
75             return new PooledTaskRunner(executor, task, maxIterationsPerRun);
76         } else {
77             return new DedicatedTaskRunner(task, name, priority, daemon);
78         }
79     }
80     
81     protected ExecutorService JavaDoc createDefaultExecutor() {
82         ThreadPoolExecutor JavaDoc rc = new ThreadPoolExecutor JavaDoc(1, Integer.MAX_VALUE, 10, TimeUnit.SECONDS, new SynchronousQueue JavaDoc(), new ThreadFactory JavaDoc() {
83             public Thread JavaDoc newThread(Runnable JavaDoc runnable) {
84                 Thread JavaDoc thread = new Thread JavaDoc(runnable, name);
85                 thread.setDaemon(daemon);
86                 thread.setPriority(priority);
87                 return thread;
88             }
89         });
90         //rc.allowCoreThreadTimeOut(true);
91
return rc;
92     }
93
94 }
95
Popular Tags