You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by ja...@apache.org on 2005/06/23 12:59:38 UTC

svn commit: r193131 - in /webservices/axis/trunk/java/modules/core/src/org/apache/axis/util/threadpool: ./ AxisWorker.java ThreadPool.java ThreadWorker.java

Author: jaliya
Date: Thu Jun 23 03:59:35 2005
New Revision: 193131

URL: http://svn.apache.org/viewcvs?rev=193131&view=rev
Log:
Added a thread pool for Axis2

Added:
    webservices/axis/trunk/java/modules/core/src/org/apache/axis/util/threadpool/
    webservices/axis/trunk/java/modules/core/src/org/apache/axis/util/threadpool/AxisWorker.java
    webservices/axis/trunk/java/modules/core/src/org/apache/axis/util/threadpool/ThreadPool.java
    webservices/axis/trunk/java/modules/core/src/org/apache/axis/util/threadpool/ThreadWorker.java

Added: webservices/axis/trunk/java/modules/core/src/org/apache/axis/util/threadpool/AxisWorker.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/core/src/org/apache/axis/util/threadpool/AxisWorker.java?rev=193131&view=auto
==============================================================================
--- webservices/axis/trunk/java/modules/core/src/org/apache/axis/util/threadpool/AxisWorker.java (added)
+++ webservices/axis/trunk/java/modules/core/src/org/apache/axis/util/threadpool/AxisWorker.java Thu Jun 23 03:59:35 2005
@@ -0,0 +1,10 @@
+package org.apache.axis.util.threadpool;
+
+/**
+ * Worker interface for Axis Workers.
+ * When a worker is handed over to the thread pool the method <code>doWork()</code>
+ * is called by the thread pool.
+ */
+public interface AxisWorker {
+    void doWork();
+}

Added: webservices/axis/trunk/java/modules/core/src/org/apache/axis/util/threadpool/ThreadPool.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/core/src/org/apache/axis/util/threadpool/ThreadPool.java?rev=193131&view=auto
==============================================================================
--- webservices/axis/trunk/java/modules/core/src/org/apache/axis/util/threadpool/ThreadPool.java (added)
+++ webservices/axis/trunk/java/modules/core/src/org/apache/axis/util/threadpool/ThreadPool.java Thu Jun 23 03:59:35 2005
@@ -0,0 +1,103 @@
+package org.apache.axis.util.threadpool;
+
+import sun.misc.Queue;
+
+import java.util.*;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.axis.engine.AxisFault;
+
+
+/**
+ * This the thread pool for axis2. This class will be used a singleton
+ * across axis2 engine. <code>ThreadPool</code> is accepts <code>AxisWorkers</code> which has
+ * doWork method on them and execute this method, using one of the threads
+ * in the thread pool.
+ */
+
+public class ThreadPool {
+
+    protected static Log log = LogFactory.getLog(ThreadPool.class.getName());
+
+    private static int MAX_THREAD_COUNT = 10;
+    protected static long SLEEP_INTERVAL = 1000;
+    private static List threads;
+    private static List tasks;
+    private static boolean shoutDown;
+    private static ThreadPool instance;
+
+    private ThreadPool() {
+        threads = new ArrayList();
+        tasks = new ArrayList();
+    }
+
+
+    public static ThreadPool getInstance() {
+        if(log.isDebugEnabled())
+        log.debug("ThreadPool Created");
+
+        if (instance != null) {
+            return instance;
+        } else {
+            instance = new ThreadPool();
+            for (int i = 0; i < MAX_THREAD_COUNT; i++) {
+                ThreadWorker threadWorker = new ThreadWorker();
+                threadWorker.setPool(instance);
+                threads.add(threadWorker);
+                threadWorker.start();
+            }
+            return instance;
+        }
+    }
+
+    public void addWorker(AxisWorker worker) throws AxisFault {
+        if (shoutDown)
+            throw new AxisFault("Thread Pool is Shutting Down");
+        tasks.add(worker);
+    }
+
+    public synchronized AxisWorker getWorker() {
+        if (!tasks.isEmpty()) {
+            AxisWorker worker = (AxisWorker) tasks.get(0);
+            tasks.remove(worker);
+            return worker;
+        } else
+            return null;
+    }
+
+    /**
+     * This is the recommended shutdown method for the thread pool
+     * This will wait till all the workers that are already handed over to the
+     * thread pool get executed.
+     *
+     * @throws AxisFault
+     */
+    public void safeShutDown() throws AxisFault {
+        synchronized (this) {
+            shoutDown = true;
+        }
+        while (!tasks.isEmpty()) {
+            try {
+                Thread.sleep(SLEEP_INTERVAL);
+            } catch (InterruptedException e) {
+                throw new AxisFault("Error while safeShutDown", e);
+            }
+        }
+        forceShutDown();
+
+    }
+
+    /**
+     * A forceful shutdown mechanism for thread pool.
+     */
+    public void forceShutDown() {
+        if(log.isDebugEnabled())
+        log.debug("forceShutDown called. Thread workers will be stopped");
+        Iterator ite = threads.iterator();
+        while (ite.hasNext()) {
+            ThreadWorker worker = (ThreadWorker) ite.next();
+            worker.setStop(true);
+        }
+    }
+}

Added: webservices/axis/trunk/java/modules/core/src/org/apache/axis/util/threadpool/ThreadWorker.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/core/src/org/apache/axis/util/threadpool/ThreadWorker.java?rev=193131&view=auto
==============================================================================
--- webservices/axis/trunk/java/modules/core/src/org/apache/axis/util/threadpool/ThreadWorker.java (added)
+++ webservices/axis/trunk/java/modules/core/src/org/apache/axis/util/threadpool/ThreadWorker.java Thu Jun 23 03:59:35 2005
@@ -0,0 +1,41 @@
+package org.apache.axis.util.threadpool;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * This is the thread worker for the Axis2's thread pool
+ * This will pick a worker from the thread pool and executes its
+ * <code>doWork()</code> method of the particular worker.
+ */
+public class ThreadWorker extends Thread {
+    protected static Log log = LogFactory.getLog(ThreadWorker.class.getName());
+    private boolean stop;
+    private ThreadPool pool;
+
+    public void setPool(ThreadPool pool) {
+        this.pool = pool;
+    }
+
+    public boolean isStop() {
+        return stop;
+    }
+
+    public void setStop(boolean stop) {
+        this.stop = stop;
+    }
+
+    public void run() {
+        while (!stop) {
+            AxisWorker axisWorker = null;
+            try {
+                axisWorker = pool.getWorker();
+                if (axisWorker != null)
+                    axisWorker.doWork();
+                sleep(ThreadPool.SLEEP_INTERVAL);
+            } catch (InterruptedException e) {
+                log.error(e);
+            }
+        }
+    }
+}