comparison servlet/src/digilib/util/DigilibJobCenter.java @ 557:0885f5ca5b24 digilibPDF

more refactoring and rearranging pdf and image generation works now
author robcast
date Thu, 16 Dec 2010 21:19:11 +0100
parents servlet/src/digilib/servlet/DigilibJobCenter.java@e7c29b587829
children 686086d6e6d6
comparison
equal deleted inserted replaced
556:5cc180bb0a5c 557:0885f5ca5b24
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 import digilib.image.DocuImage;
17
18 /** Wrapper around ExecutionService.
19 *
20 * @author casties
21 *
22 */
23 public class DigilibJobCenter<V> {
24 /** general logger for this class */
25 private static Logger logger = Logger.getLogger("digilib.jobcenter");
26 /** ExecutorService */
27 private ExecutorService executor;
28 /** max number of running threads */
29 private int maxThreads = 1;
30 /** max number of waiting threads */
31 private int maxQueueLen = 50;
32
33 /**
34 * @param maxThreads
35 * @param maxQueueLength
36 */
37 public DigilibJobCenter(int maxThreads, int maxQueueLen, boolean prestart) {
38 super();
39 this.maxThreads = maxThreads;
40 this.maxQueueLen = maxQueueLen;
41 executor = Executors.newFixedThreadPool(maxThreads);
42 if (prestart) {
43 // prestart threads so Tomcat's leak protection doesn't complain
44 int st = ((ThreadPoolExecutor)executor).prestartAllCoreThreads();
45 logger.debug("prestarting threads: "+st);
46 }
47 }
48
49 /** Submit job to execute
50 *
51 * @param job
52 * @return Future to control the job
53 */
54 public Future<V> submit(Callable<V> job) {
55 return executor.submit(job);
56 }
57
58 /** Returns if the service is not overloaded.
59 *
60 * @return
61 */
62 public boolean canRun() {
63 int jql = getWaitingJobs();
64 int jrl = getRunningJobs();
65 logger.debug("canRun: waiting jobs="+jql+" running jobs="+jrl);
66 return (jql <= maxQueueLen);
67 }
68
69 /** Returns if the service is overloaded.
70 *
71 * @return
72 */
73 public boolean isBusy() {
74 int jql = getWaitingJobs();
75 int jrl = getRunningJobs();
76 logger.debug("isBusy: waiting jobs="+jql+" running jobs="+jrl);
77 return (jql > maxQueueLen);
78 }
79
80 public int getRunningJobs() {
81 return ((ThreadPoolExecutor)executor).getActiveCount();
82 }
83
84 public int getWaitingJobs() {
85 BlockingQueue<Runnable> jq = ((ThreadPoolExecutor)executor).getQueue();
86 int jql = jq.size();
87 return jql;
88 }
89
90 public void setMaxThreads(int maxThreads) {
91 this.maxThreads = maxThreads;
92 }
93
94 public int getMaxThreads() {
95 return maxThreads;
96 }
97
98 public void setMaxQueueLen(int maxQueueLen) {
99 this.maxQueueLen = maxQueueLen;
100 }
101
102 public int getMaxQueueLen() {
103 return maxQueueLen;
104 }
105
106 public List<Runnable> shutdownNow() {
107 return executor.shutdownNow();
108 }
109
110 }