comparison common/src/main/java/digilib/util/DigilibJobCenter.java @ 903:7779b37d1d05

refactored into maven modules per servlet type. can build servlet-api 2.3 and 3.0 via profile now!
author robcast
date Tue, 26 Apr 2011 20:24:31 +0200
parents servlet/src/main/java/digilib/util/DigilibJobCenter.java@ba1eb2d821a2
children
comparison
equal deleted inserted replaced
902:89ba3ffcf552 903:7779b37d1d05
1 /** Wrapper around ExecutionService.
2 *
3 */
4 package digilib.util;
5
6 import java.util.List;
7 import java.util.concurrent.BlockingQueue;
8 import java.util.concurrent.Callable;
9 import java.util.concurrent.ExecutorService;
10 import java.util.concurrent.Executors;
11 import java.util.concurrent.Future;
12 import java.util.concurrent.ThreadPoolExecutor;
13
14 import org.apache.log4j.Logger;
15
16 /** Wrapper around ExecutionService.
17 *
18 * @author casties
19 *
20 */
21 public class DigilibJobCenter<V> {
22 /** general logger for this class */
23 private static Logger logger = Logger.getLogger("digilib.jobcenter");
24 /** ExecutorService */
25 private ExecutorService executor;
26 /** max number of running threads */
27 private int maxThreads = 1;
28 /** max number of waiting threads */
29 private int maxQueueLen = 50;
30 /** label for this job center */
31 private String label = "";
32
33 /** Create a DigilibJobcenter with the given number of threads and queue length.
34 * If prestart=true it starts the threads in the thread pool.
35 *
36 * @param maxThreads
37 * @param maxQueueLen
38 * @param prestart
39 * @param label
40 */
41 public DigilibJobCenter(int maxThreads, int maxQueueLen, boolean prestart, String label) {
42 super();
43 this.label = (label != null) ? label : "";
44 this.maxThreads = maxThreads;
45 this.maxQueueLen = maxQueueLen;
46 executor = Executors.newFixedThreadPool(maxThreads);
47 if (prestart) {
48 // prestart threads so Tomcat's leak protection doesn't complain
49 int st = ((ThreadPoolExecutor)executor).prestartAllCoreThreads();
50 logger.debug(label+" prestarting threads: "+st);
51 }
52 }
53
54 /** Submit Callable job that returns a Value to execute.
55 *
56 * @param job
57 * @return Future to control the job
58 */
59 public Future<V> submit(Callable<V> job) {
60 return executor.submit(job);
61 }
62
63 /** Submit Runnable job to execute.
64 *
65 * @param job
66 * @return Future to control the job
67 */
68 public Future<?> submit(Runnable job) {
69 return executor.submit(job);
70 }
71
72 /** Returns if the service is overloaded.
73 *
74 * @return
75 */
76 public boolean isBusy() {
77 int jql = getWaitingJobs();
78 int jrl = getRunningJobs();
79 logger.debug(label+" isBusy: waiting jobs="+jql+" running jobs="+jrl);
80 return (jql > maxQueueLen);
81 }
82
83 /** Returns the number of currently running jobs.
84 * @return
85 */
86 public int getRunningJobs() {
87 return ((ThreadPoolExecutor)executor).getActiveCount();
88 }
89
90 /** Returns the number of currently waiting jobs.
91 * @return
92 */
93 public int getWaitingJobs() {
94 BlockingQueue<Runnable> jq = ((ThreadPoolExecutor)executor).getQueue();
95 int jql = jq.size();
96 return jql;
97 }
98
99 public void setMaxThreads(int maxThreads) {
100 this.maxThreads = maxThreads;
101 }
102
103 public int getMaxThreads() {
104 return maxThreads;
105 }
106
107 public void setMaxQueueLen(int maxQueueLen) {
108 this.maxQueueLen = maxQueueLen;
109 }
110
111 public int getMaxQueueLen() {
112 return maxQueueLen;
113 }
114
115 /** Shuts down the Executor.
116 * Tries to stop running threads and returns a list of waiting threads.
117 *
118 * @return
119 */
120 public List<Runnable> shutdownNow() {
121 return executor.shutdownNow();
122 }
123
124 }