comparison servlet/src/digilib/util/DigilibJobCenter.java @ 570:fd2ef7e46119

more cleanup, set version to 1.8.2
author robcast
date Tue, 21 Dec 2010 20:24:09 +0100
parents 686086d6e6d6
children 587c90bc5976
comparison
equal deleted inserted replaced
569:1f666c2b4578 570:fd2ef7e46119
10 import java.util.concurrent.Executors; 10 import java.util.concurrent.Executors;
11 import java.util.concurrent.Future; 11 import java.util.concurrent.Future;
12 import java.util.concurrent.ThreadPoolExecutor; 12 import java.util.concurrent.ThreadPoolExecutor;
13 13
14 import org.apache.log4j.Logger; 14 import org.apache.log4j.Logger;
15
16 import digilib.image.DocuImage;
17 15
18 /** Wrapper around ExecutionService. 16 /** Wrapper around ExecutionService.
19 * 17 *
20 * @author casties 18 * @author casties
21 * 19 *
30 /** max number of waiting threads */ 28 /** max number of waiting threads */
31 private int maxQueueLen = 50; 29 private int maxQueueLen = 50;
32 /** label for this job center */ 30 /** label for this job center */
33 private String label = ""; 31 private String label = "";
34 32
35 /** 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 36 * @param maxThreads
37 * @param label TODO 37 * @param maxQueueLen
38 * @param maxQueueLength 38 * @param prestart
39 * @param label
39 */ 40 */
40 public DigilibJobCenter(int maxThreads, int maxQueueLen, boolean prestart, String label) { 41 public DigilibJobCenter(int maxThreads, int maxQueueLen, boolean prestart, String label) {
41 super(); 42 super();
42 this.label = (label != null) ? label : ""; 43 this.label = (label != null) ? label : "";
43 this.maxThreads = maxThreads; 44 this.maxThreads = maxThreads;
57 */ 58 */
58 public Future<V> submit(Callable<V> job) { 59 public Future<V> submit(Callable<V> job) {
59 return executor.submit(job); 60 return executor.submit(job);
60 } 61 }
61 62
62 /** Returns if the service is not overloaded.
63 *
64 * @return
65 */
66 public boolean canRun() {
67 int jql = getWaitingJobs();
68 int jrl = getRunningJobs();
69 logger.debug(label+" canRun: waiting jobs="+jql+" running jobs="+jrl);
70 return (jql <= maxQueueLen);
71 }
72
73 /** Returns if the service is overloaded. 63 /** Returns if the service is overloaded.
74 * 64 *
75 * @return 65 * @return
76 */ 66 */
77 public boolean isBusy() { 67 public boolean isBusy() {
79 int jrl = getRunningJobs(); 69 int jrl = getRunningJobs();
80 logger.debug(label+" isBusy: waiting jobs="+jql+" running jobs="+jrl); 70 logger.debug(label+" isBusy: waiting jobs="+jql+" running jobs="+jrl);
81 return (jql > maxQueueLen); 71 return (jql > maxQueueLen);
82 } 72 }
83 73
74 /** Returns the number of currently running jobs.
75 * @return
76 */
84 public int getRunningJobs() { 77 public int getRunningJobs() {
85 return ((ThreadPoolExecutor)executor).getActiveCount(); 78 return ((ThreadPoolExecutor)executor).getActiveCount();
86 } 79 }
87 80
81 /** Returns the number of currently waiting jobs.
82 * @return
83 */
88 public int getWaitingJobs() { 84 public int getWaitingJobs() {
89 BlockingQueue<Runnable> jq = ((ThreadPoolExecutor)executor).getQueue(); 85 BlockingQueue<Runnable> jq = ((ThreadPoolExecutor)executor).getQueue();
90 int jql = jq.size(); 86 int jql = jq.size();
91 return jql; 87 return jql;
92 } 88 }
105 101
106 public int getMaxQueueLen() { 102 public int getMaxQueueLen() {
107 return maxQueueLen; 103 return maxQueueLen;
108 } 104 }
109 105
106 /** Shuts down the Executor.
107 * Tries to stop running threads and returns a list of waiting threads.
108 *
109 * @return
110 */
110 public List<Runnable> shutdownNow() { 111 public List<Runnable> shutdownNow() {
111 return executor.shutdownNow(); 112 return executor.shutdownNow();
112 } 113 }
113 114
114 } 115 }