Author: mheath
Date: Tue Feb  5 20:20:03 2008
New Revision: 618877

URL: http://svn.apache.org/viewvc?rev=618877&view=rev
Log:
ASYNCWEB-2 Renamed SessionCache to ConnectionPool.

Added:
    
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/ConnectionPool.java
      - copied, changed from r618827, 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/SessionCache.java
Removed:
    
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/SessionCache.java
Modified:
    mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/AsyncHttpClient.java
    
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
    
mina/asyncweb/trunk/client/src/test/java/org/apache/ahc/ConnectionReuseTest.java

Modified: 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/AsyncHttpClient.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/AsyncHttpClient.java?rev=618877&r1=618876&r2=618877&view=diff
==============================================================================
--- 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/AsyncHttpClient.java 
(original)
+++ 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/AsyncHttpClient.java 
Tue Feb  5 20:20:03 2008
@@ -34,7 +34,7 @@
 import org.apache.ahc.codec.HttpProtocolCodecFactory;
 import org.apache.ahc.codec.HttpRequestMessage;
 import org.apache.ahc.codec.ResponseFuture;
-import org.apache.ahc.codec.SessionCache;
+import org.apache.ahc.codec.ConnectionPool;
 import org.apache.ahc.proxy.ProxyFilter;
 import org.apache.ahc.ssl.TrustManagerFactoryImpl;
 import org.apache.ahc.util.AsyncHttpClientException;
@@ -118,8 +118,8 @@
     /** The HttpIoHandler handler. */
     private final HttpIoHandler handler;
 
-    /** The cache for session reuse */
-    private SessionCache sessionCache; 
+    /** The connection pool for session reuse */
+    private ConnectionPool connectionPool; 
 
     /** The Reuse Address Socket Parameter. */
     private boolean reuseAddress = DEFAULT_REUSE_ADDRESS;
@@ -383,27 +383,27 @@
 
     
     /**
-     * Set the session cache that should be used for 
-     * connection reuse.
+     * Set the connection pool that should be used for 
+     * session reuse.
      * 
-     * @param cache  The new session cache.  If null, this will disable
-     *               future connection reuse.
+     * @param connectionPool  The new session connection pool.  If null, this 
will disable
+     *               future connection pooling.
      */
-    public void setSessionCache(SessionCache cache) {
-        sessionCache = cache; 
+    public void setConnectionPool(ConnectionPool connectionPool) {
+        this.connectionPool = connectionPool; 
         // our I/O Handler instance needs to be fitted with the same 
-        // cache
-        handler.setSessionCache(cache); 
+        // connection pool
+        handler.setConnectionPool(connectionPool); 
     }
     
     /**
-     * Retrieve the session cache used for storing 
+     * Retrieve the connection pool used for storing 
      * connections for reuse. 
      * 
-     * @return The current session cache for the client. 
+     * @return The current connection pool
      */
-    public SessionCache getSessionCache() {
-        return sessionCache; 
+    public ConnectionPool getConnectionPool() {
+        return connectionPool; 
     }
     
     /**
@@ -457,20 +457,20 @@
             message.setResponseFuture(new ResponseFuture(message, queue));
         }
         
-        // *IF* connection reuse is enabled, we should see if we have a cached 
+        // *IF* connection reuse is enabled, we should see if we have a pooled 
         // connection first; if not, always open a new one
         ConnectFuture future = null;
         if (!message.isProxyEnabled()) {
-            SessionCache cache = getSessionCache(); 
-            if (cache != null) {
-            future = getCachedConnection(message);
-        } else {
-            // add the Connection close header explicitly
-            message.setHeader(HttpDecoder.CONNECTION, HttpDecoder.CLOSE);
-        }
+               ConnectionPool connectionPool = getConnectionPool(); 
+               if (connectionPool != null) {
+                       future = getPooledConnection(message);
+               } else {
+                       // add the Connection close header explicitly
+                       message.setHeader(HttpDecoder.CONNECTION, 
HttpDecoder.CLOSE);
+               }
         }
         
-        // if no cached connection is found or keep-alive is disabled, force a
+        // if no pooled connection is found or keep-alive is disabled, force a
         // new connection
         if (future == null) {
             // set the connect start time
@@ -527,16 +527,16 @@
     }
     
     /**
-     * Attempt to get a connection from the session cache.
+     * Attempt to get a connection from the connection pool.
      * 
      * @param message The message we're sending.
      * 
-     * @return A cached connection.  This returns null if there's
+     * @return A pooled connection.  This returns null if there's
      *         no available connection for the target location.
      */
-    private ConnectFuture getCachedConnection(HttpRequestMessage message) {
-        IoSession cached = sessionCache.getActiveSession(message);
-        if (cached == null) {
+    private ConnectFuture getPooledConnection(HttpRequestMessage message) {
+        IoSession pooledSession = connectionPool.getActiveSession(message);
+        if (pooledSession == null) {
             return null;
         }
 
@@ -545,7 +545,7 @@
         notifyMonitoringListeners(MonitoringEvent.CONNECTION_REUSED, message); 
         // create a containing future object and set the session right away
         ConnectFuture future = new DefaultConnectFuture();
-        future.setSession(cached);
+        future.setSession(pooledSession);
         return future;
     }
 

Copied: 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/ConnectionPool.java
 (from r618827, 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/SessionCache.java)
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/ConnectionPool.java?p2=mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/ConnectionPool.java&p1=mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/SessionCache.java&r1=618827&r2=618877&rev=618877&view=diff
==============================================================================
--- 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/SessionCache.java 
(original)
+++ 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/ConnectionPool.java
 Tue Feb  5 20:20:03 2008
@@ -28,14 +28,17 @@
 import org.apache.mina.common.IoSession;
 
 /**
- * Class that provides access to cached sessions.  IoSessions are cached using
- * the host and the port.  This class is thread safe.
+ * Facilitates support for connection pooling by storing IoSession instances
+ * that have keepAlive enabled.  Connection pooling is based on the
+ * IoSession's remote host and port.
+ * 
+ * <p>This class is thread safe.
  */
-public final class SessionCache {
-    private final ConcurrentMap<String,Queue<IoSession>> cachedSessions = 
+public final class ConnectionPool {
+    private final ConcurrentMap<String,Queue<IoSession>> pooledSessions = 
             new ConcurrentHashMap<String,Queue<IoSession>>();
     
-    public SessionCache() {}
+    public ConnectionPool() {}
 
     /**
      * Returns an IoSession that is connected and considered usable.  Note that
@@ -52,35 +55,35 @@
             throw new IllegalArgumentException("null request was passed in");
         }
         
-        Queue<IoSession> queue = cachedSessions.get(getKey(msg));
+        Queue<IoSession> queue = pooledSessions.get(getKey(msg));
         if (queue == null) {
             return null;
         }
         
-        IoSession cached = null;
-        while ((cached = queue.poll()) != null) {
+        IoSession pooled = null;
+        while ((pooled = queue.poll()) != null) {
                // see if the session is usable
-            if (cached.isConnected() && !cached.isClosing()) {
-                return cached;
+            if (pooled.isConnected() && !pooled.isClosing()) {
+                return pooled;
             }
         }
         return null;
     }
     
     /**
-     * Caches the given session using its remote host and port information.
+     * Places the given session in the pool of available connections based on 
the session's remote host and port.
      * 
-     * @param session IoSession to cache
+     * @param session IoSession to pool
      * @throws IllegalArgumentException if a null session was passed in.
      */
-    void cacheSession(IoSession session) {
+    void poolConnection(IoSession session) {
         if (session == null) {
             throw new IllegalArgumentException("null session was passed in");
         }
         
         String key = getKey((InetSocketAddress)session.getRemoteAddress());
         Queue<IoSession> newQueue = new ConcurrentLinkedQueue<IoSession>();
-        Queue<IoSession> queue = cachedSessions.putIfAbsent(key, newQueue);
+        Queue<IoSession> queue = pooledSessions.putIfAbsent(key, newQueue);
         if (queue == null) {
             // the value was previously empty
             queue = newQueue;
@@ -90,9 +93,9 @@
     }
     
     /**
-     * Removes the given session from the cache if it is in the cache.
+     * Removes the given session from the pool if it is in the pool.
      * 
-     * @param session IoSession to remove from the cache
+     * @param session IoSession to remove from the pool
      * @throws IllegalArgumentException if a null session was passed in
      */
     void removeSession(IoSession session) {
@@ -101,7 +104,7 @@
         }
         
         String key = getKey((InetSocketAddress)session.getRemoteAddress());
-        Queue<IoSession> queue = cachedSessions.get(key);
+        Queue<IoSession> queue = pooledSessions.get(key);
         if (queue != null) {
             queue.remove(session);
         }

Modified: 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpIoHandler.java?rev=618877&r1=618876&r2=618877&view=diff
==============================================================================
--- 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
 (original)
+++ 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
 Tue Feb  5 20:20:03 2008
@@ -57,29 +57,29 @@
     public static final String CONNECTION_CLOSE = "close";
 
     /**
-     * The session cache used for reusable connections 
+     * The connection pool used for reusable IoSessions 
      */
-    private SessionCache sessionCache; 
+    private ConnectionPool connectionPool; 
 
     /**
-     * Set the session cache that should be used for 
+     * Set the connection pool that should be used for 
      * connection reuse.
      * 
-     * @param cache  The new session cache.  If null, this will disable
-     *               future connection reuse.
+     * @param connectionPool  The new connection pool.  If null, this will 
disable
+     *               future IoSession instance reuse.
      */
-    public void setSessionCache(SessionCache cache) {
-        sessionCache = cache; 
+    public void setConnectionPool(ConnectionPool connectionPool) {
+        this.connectionPool = connectionPool; 
     }
     
     /**
-     * Retrieve the session cache used for storing 
-     * connections for reuse. 
+     * Retrieve the connection pool used for storing 
+     * IoSession instances for reuse. 
      * 
-     * @return The current session cache for the client. 
+     * @return The current connection pool for the client. 
      */
-    public SessionCache getSessionCache() {
-        return sessionCache; 
+    public ConnectionPool getConnectionPool() {
+        return connectionPool; 
     }
 
     /**
@@ -126,13 +126,7 @@
             //Send the redirect
             client.sendRequest(request);
 
-            // if we've been provided with a cache, put this session into 
-            // the cache. 
-            SessionCache cache = getSessionCache(); 
-            if (cache != null) {
-            // cache the session before we return
-                cache.cacheSession(ioSession);
-            }
+            poolSession(ioSession);
             return;
         }
 
@@ -155,13 +149,7 @@
             //Authenticate
             int authCount = request.getAuthCount() + 1;
             if (authCount <= 3) {
-                // if we've been provided with a cache, put this session into 
-                // the cache. 
-                SessionCache cache = getSessionCache(); 
-                if (cache != null) {
-                    // cache the session before we return
-                    cache.cacheSession(ioSession);
-                }
+                poolSession(ioSession);
                 
                 request.setAuthCount(authCount);
                 client.sendRequest(request);
@@ -176,16 +164,21 @@
         ResponseFuture result = request.getResponseFuture();
         result.set(response);
 
-        // if we've been provided with a cache, put this session into 
-        // the cache. 
-        SessionCache cache = getSessionCache(); 
-        if (cache != null) {
-        // cache the session before we return
-            cache.cacheSession(ioSession);
-        }
+        poolSession(ioSession);
     }
 
     /**
+     * Place the IoSession instance in the pool if the pool is available.
+     * @param session
+     */
+    private void poolSession(IoSession session) {
+        ConnectionPool pool = getConnectionPool();
+        if (pool != null) {
+            pool.poolConnection(session);
+        }
+    }
+    
+    /**
      * Handler for receiving a notification that an Exception occurred in the 
communication with the server
      *
      * @param ioSession the [EMAIL PROTECTED] 
org.apache.mina.common.IoSession} representing the connection to the server.
@@ -219,13 +212,11 @@
     public void sessionClosed(IoSession ioSession) throws Exception {
         //Clean up if any in-proccess decoding was occurring
         ioSession.removeAttribute(CURRENT_RESPONSE);
-        
-        // if we've been provided with a cache, remove this session from 
-        // the cache. 
-        SessionCache cache = getSessionCache(); 
-        if (cache != null) {
-            // cache the session before we return
-            cache.removeSession(ioSession);
+
+        // Remove ioSession from connection pool if the conneciton pool is 
available
+        ConnectionPool connectionPool = getConnectionPool(); 
+        if (connectionPool != null) {
+            connectionPool.removeSession(ioSession);
         }
         HttpRequestMessage request = (HttpRequestMessage) 
ioSession.getAttribute(CURRENT_REQUEST);
         AsyncHttpClient client = (AsyncHttpClient) ioSession.getAttachment();

Modified: 
mina/asyncweb/trunk/client/src/test/java/org/apache/ahc/ConnectionReuseTest.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/client/src/test/java/org/apache/ahc/ConnectionReuseTest.java?rev=618877&r1=618876&r2=618877&view=diff
==============================================================================
--- 
mina/asyncweb/trunk/client/src/test/java/org/apache/ahc/ConnectionReuseTest.java
 (original)
+++ 
mina/asyncweb/trunk/client/src/test/java/org/apache/ahc/ConnectionReuseTest.java
 Tue Feb  5 20:20:03 2008
@@ -25,13 +25,13 @@
 
 import org.apache.ahc.codec.HttpRequestMessage;
 import org.apache.ahc.codec.HttpResponseMessage;
-import org.apache.ahc.codec.SessionCache;        
+import org.apache.ahc.codec.ConnectionPool;        
 
 public class ConnectionReuseTest extends AbstractTest {
     // variable that keeps count of session close's
     private final AtomicInteger closeCount = new AtomicInteger(0);
     
-    private final SessionCache cache = new SessionCache(); 
+    private final ConnectionPool connectionPool = new ConnectionPool(); 
     
     // It is important that this test case contains these methods in this 
order.
     // It is because to test connection reuse we need to keep the embedded 
@@ -111,7 +111,7 @@
         request.setParameter("TEST2", "Test Two");
         AsyncHttpClient ahc = new AsyncHttpClient();
         if (reuseConnection) {
-            ahc.setSessionCache(cache);  
+            ahc.setConnectionPool(connectionPool);  
         }
         ahc.setTcpNoDelay(true);
         return ahc.sendRequest(request);


Reply via email to