geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rickmcgu...@apache.org
Subject svn commit: r603700 - /geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java
Date Wed, 12 Dec 2007 18:44:28 GMT
Author: rickmcguire
Date: Wed Dec 12 10:44:28 2007
New Revision: 603700

URL: http://svn.apache.org/viewvc?rev=603700&view=rev
Log:
GERONIMO-3686 AsyncHttpClient does not reuse connection even if connections are persistent

Patch submitted by Sangjin Lee. 


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=603700&r1=603699&r2=603700&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 Wed
Dec 12 10:44:28 2007
@@ -40,9 +40,11 @@
 import org.apache.mina.common.IoFutureListener;
 import org.apache.mina.common.IoSession;
 import org.apache.mina.common.RuntimeIOException;
+import org.apache.mina.common.ThreadModel;
 import org.apache.mina.common.support.DefaultConnectFuture;
 import org.apache.mina.filter.SSLFilter;
 import org.apache.mina.filter.codec.ProtocolCodecFilter;
+import org.apache.mina.filter.executor.ExecutorFilter;
 import org.apache.mina.transport.socket.nio.SocketConnector;
 import org.apache.mina.transport.socket.nio.SocketSessionConfig;
 
@@ -54,9 +56,9 @@
  */
 public class AsyncHttpClient {
 
-	/** The Constant DEFAULT_REUSE_CONNECTION. */
-	public static final boolean DEFAULT_REUSE_CONNECTION = false;
-	
+    /** The Constant DEFAULT_REUSE_CONNECTION. */
+    public static final boolean DEFAULT_REUSE_CONNECTION = false;
+    
     /** The Constant DEFAULT_CONNECTION_TIMEOUT. */
     public static final int DEFAULT_CONNECTION_TIMEOUT = 30000;
 
@@ -96,9 +98,13 @@
     /** The connector. */
     private final SocketConnector connector;
 
-    /** The thread pool. */
+    /** 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;
     
+    /** The (optional) thread pool for the events and callbacks. */
+    private volatile ExecutorService eventThreadPool;
+    
     /** The HttpIoHandler handler. */
     private final HttpIoHandler handler;
 
@@ -138,7 +144,7 @@
      * @return true if it reuses connections
      */
     public boolean isReuseConnection() {
-    	return reuseConnection;
+        return reuseConnection;
     }
     
     /**
@@ -147,7 +153,7 @@
      * @param reuseConnection the new value
      */
     public void setReuseConnection(boolean reuseConnection) {
-    	this.reuseConnection = reuseConnection;
+        this.reuseConnection = reuseConnection;
     }
     
     /**
@@ -295,6 +301,15 @@
     }
     
     /**
+     * Sets the (optional) thread pool for the event processing and callback
+     * invocation.  It is the caller's responsibility to properly dispose of
+     * the thread pool.
+     */
+    public void setEventThreadPool(ExecutorService threadPool) {
+        eventThreadPool = threadPool;
+    }
+    
+    /**
      * Instantiates a new AsyncHttpClient.  It will use a single threaded model and is good
for
      * use in one-off connections.
      */
@@ -345,6 +360,10 @@
         else
             connector = new SocketConnector(Runtime.getRuntime().availableProcessors(), threadPool);
 
+        // disable the default thread model per recommendation from the mina folks
+        // http://mina.apache.org/configuring-thread-model.html
+        connector.getDefaultConfig().setThreadModel(ThreadModel.MANUAL);
+        
         applyConnectionTimeout();
         connector.setWorkerTimeout(1);
 
@@ -481,6 +500,7 @@
     class FutureListener implements IoFutureListener {
         static final String SSL_FILTER = "SSL";
         static final String PROTOCOL_FILTER = "protocolFilter";
+        static final String EVENT_THREAD_POOL_FILTER = "eventThreadPoolFilter";
 
         /** The request. */
         final HttpRequestMessage request;
@@ -501,7 +521,6 @@
          * @param future the {@link org.apache.mina.common.IoFuture} representing the <code>ConnectFuture</code>.
          * @see org.apache.mina.common.IoFutureListener#operationComplete(org.apache.mina.common.IoFuture)
          */
-        @SuppressWarnings({"UnusedDeclaration"})
         public void operationComplete(IoFuture future) {
             ConnectFuture connFuture = (ConnectFuture) future;
             if (connFuture.isConnected()) {
@@ -515,6 +534,13 @@
                     sess.getFilterChain().addLast(PROTOCOL_FILTER, new ProtocolCodecFilter(
                             new HttpProtocolCodecFactory()));
                 }
+                // (optional) add the executor filter for the event thread pool 
+                // (if it's not there already like in a reused session)
+                if (eventThreadPool != null && 
+                        !sess.getFilterChain().contains(EVENT_THREAD_POOL_FILTER)) {
+                    sess.getFilterChain().addLast(EVENT_THREAD_POOL_FILTER, 
+                            new ExecutorFilter(eventThreadPool));
+                }
                 
                 sess.setAttribute(HttpIoHandler.CURRENT_REQUEST, request);
 
@@ -534,7 +560,7 @@
                 sess.write(request);
             } else {
                 try {
-                    IoSession sess = future.getSession();
+                    future.getSession();
                     request.getCallback().onException(new AsyncHttpClientException("Connection
failed."));
                 } catch (RuntimeIOException e) {
                     //Set the callback exception



Mime
View raw message