diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/servlet/src/digilib/util/DigilibJobCenter.java	Thu Dec 16 21:19:11 2010 +0100
@@ -0,0 +1,110 @@
+/** Wrapper around ExecutionService.
+ * 
+ */
+package digilib.util;
+
+import java.util.List;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.ThreadPoolExecutor;
+
+import org.apache.log4j.Logger;
+
+import digilib.image.DocuImage;
+
+/** Wrapper around ExecutionService.
+ * 
+ * @author casties
+ *
+ */
+public class DigilibJobCenter<V> {
+    /** general logger for this class */
+    private static Logger logger = Logger.getLogger("digilib.jobcenter");
+    /** ExecutorService */
+    private ExecutorService executor;
+    /** max number of running threads */
+    private int maxThreads = 1;
+    /** max number of waiting threads */
+    private int maxQueueLen = 50;
+    
+    /**
+     * @param maxThreads
+     * @param maxQueueLength
+     */
+    public DigilibJobCenter(int maxThreads, int maxQueueLen, boolean prestart) {
+        super();
+        this.maxThreads = maxThreads;
+        this.maxQueueLen = maxQueueLen;
+        executor = Executors.newFixedThreadPool(maxThreads);
+        if (prestart) {
+            // prestart threads so Tomcat's leak protection doesn't complain
+            int st = ((ThreadPoolExecutor)executor).prestartAllCoreThreads();
+            logger.debug("prestarting threads: "+st);
+        }
+    }
+    
+    /** Submit job to execute
+     * 
+     * @param job
+     * @return Future to control the job
+     */
+    public Future<V> submit(Callable<V> job) {
+        return executor.submit(job);
+    }
+
+    /** Returns if the service is not overloaded.
+     *  
+     * @return
+     */
+    public boolean canRun() {
+        int jql = getWaitingJobs();
+        int jrl = getRunningJobs();
+        logger.debug("canRun: waiting jobs="+jql+" running jobs="+jrl);
+        return (jql <= maxQueueLen);
+    }
+    
+    /** Returns if the service is overloaded.
+     *  
+     * @return
+     */
+    public boolean isBusy() {
+        int jql = getWaitingJobs();
+        int jrl = getRunningJobs();
+        logger.debug("isBusy: waiting jobs="+jql+" running jobs="+jrl);
+        return (jql > maxQueueLen);
+    }
+    
+    public int getRunningJobs() {
+        return ((ThreadPoolExecutor)executor).getActiveCount();
+    }
+    
+    public int getWaitingJobs() {
+        BlockingQueue<Runnable> jq = ((ThreadPoolExecutor)executor).getQueue();
+        int jql = jq.size();
+        return jql;
+    }
+
+    public void setMaxThreads(int maxThreads) {
+        this.maxThreads = maxThreads;
+    }
+
+    public int getMaxThreads() {
+        return maxThreads;
+    }
+
+    public void setMaxQueueLen(int maxQueueLen) {
+        this.maxQueueLen = maxQueueLen;
+    }
+
+    public int getMaxQueueLen() {
+        return maxQueueLen;
+    }
+
+    public List<Runnable> shutdownNow() {
+        return executor.shutdownNow();
+    }
+
+}