You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ri...@apache.org on 2007/12/18 19:54:56 UTC

svn commit: r605292 - /geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java

Author: rickmcguire
Date: Tue Dec 18 10:54:41 2007
New Revision: 605292

URL: http://svn.apache.org/viewvc?rev=605292&view=rev
Log:
GERONIMO-3707 use Executor rather than ExecutorService for thread pools that are passed into AsyncHttpClient


Modified:
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java?rev=605292&r1=605291&r2=605292&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java (original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java Tue Dec 18 10:54:41 2007
@@ -22,7 +22,7 @@
 import java.net.InetSocketAddress;
 import java.security.GeneralSecurityException;
 import java.util.concurrent.BlockingQueue;
-import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executor;
 import java.util.concurrent.ScheduledExecutorService;
 
 import javax.net.ssl.SSLContext;
@@ -106,10 +106,10 @@
 
     /** The thread pool for I/O processing, and the events and callbacks if 
      * the optional event thread pool is not provided. */
-    private final ExecutorService threadPool;
+    private Executor threadPool;
     
     /** The (optional) thread pool for the events and callbacks. */
-    private volatile ExecutorService eventThreadPool;
+    private volatile Executor eventThreadPool;
     
     /** The HttpIoHandler handler. */
     private final HttpIoHandler handler;
@@ -140,6 +140,9 @@
 
     /** The TCP No Delay Socket Parameter. */
     private boolean tcpNoDelay = DEFAULT_TCP_NO_DELAY;
+    
+    /** flag to make this as having been disposed of */
+    private boolean destroyed = false; 
 
     /**
      * Returns if it reuses established connections for more requests.
@@ -308,7 +311,7 @@
      * invocation.  It is the caller's responsibility to properly dispose of
      * the thread pool.
      */
-    public void setEventThreadPool(ExecutorService threadPool) {
+    public void setEventThreadPool(Executor threadPool) {
         eventThreadPool = threadPool;
     }
     
@@ -342,13 +345,13 @@
     }
 
     /**
-     * Instantiates a new AsyncHttpClient.  This will take a thread pool (ExecutorService) to use
+     * Instantiates a new AsyncHttpClient.  This will take a thread pool (Executor) to use
      * for processing connections.
      *
      * @param executor  the executor
      * @param scheduler the scheduler to use to track timeouts
      */
-    public AsyncHttpClient(ExecutorService executor, ScheduledExecutorService scheduler) {
+    public AsyncHttpClient(Executor executor, ScheduledExecutorService scheduler) {
         this(DEFAULT_CONNECTION_TIMEOUT, executor, scheduler);
     }
 
@@ -363,13 +366,13 @@
     }
 
     /**
-     * Instantiates a new AsyncHttpClient.  Allows you to specify a connection timeout and an ExecutorService.
+     * Instantiates a new AsyncHttpClient.  Allows you to specify a connection timeout and an Executor.
      *
      * @param connectionTimeout the connection timeout in milliseconds.
      * @param executor          the ExceutorService to use to process connections.
      * @param scheduler         the scheduler to use to track timeouts
      */
-    public AsyncHttpClient(int connectionTimeout, ExecutorService executor, ScheduledExecutorService scheduler) {
+    public AsyncHttpClient(int connectionTimeout, Executor executor, ScheduledExecutorService scheduler) {
         this.connectionTimeout = connectionTimeout;
 
         threadPool = executor;
@@ -428,7 +431,7 @@
      */
     public ResponseFuture sendRequest(HttpRequestMessage message, 
             BlockingQueue<ResponseFuture> queue) {
-        if (threadPool != null && threadPool.isShutdown()) {
+        if (destroyed) {
             throw new IllegalStateException("AsyncHttpClient has been destroyed and cannot be reused.");
         }
         
@@ -516,19 +519,21 @@
     }
 
     /**
-     * Shuts down the AsyncHttpClient object and shuts any associated thread pools.  Should always be called when the application is
+     * Shuts down the AsyncHttpClient object and releases references to associated thread pools.  
+     * Should always be called when the application is  
      * done using the object or a hang can occur.
      */
     public void destroyAll() {
         handler.destroy();
 
-        if (threadPool != null) {
-            threadPool.shutdownNow();
-        }
-
         if (connector != null) {
             connector.setWorkerTimeout(0);
         }
+        // release the thread pool references 
+        threadPool = null; 
+        eventThreadPool = null; 
+        // and mark this as no longer usable. 
+        destroyed = true; 
     }
 
     /**