1 18 package org.apache.activemq.thread; 19 20 import java.util.concurrent.Executor ; 21 import java.util.concurrent.ExecutorService ; 22 import java.util.concurrent.SynchronousQueue ; 23 import java.util.concurrent.ThreadFactory ; 24 import java.util.concurrent.ThreadPoolExecutor ; 25 import java.util.concurrent.TimeUnit ; 26 27 39 public class TaskRunnerFactory { 40 41 private ExecutorService executor; 42 private int maxIterationsPerRun; 43 private String 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 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( "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 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 createDefaultExecutor() { 82 ThreadPoolExecutor rc = new ThreadPoolExecutor (1, Integer.MAX_VALUE, 10, TimeUnit.SECONDS, new SynchronousQueue (), new ThreadFactory () { 83 public Thread newThread(Runnable runnable) { 84 Thread thread = new Thread (runnable, name); 85 thread.setDaemon(daemon); 86 thread.setPriority(priority); 87 return thread; 88 } 89 }); 90 return rc; 92 } 93 94 } 95 | Popular Tags |