geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgenen...@apache.org
Subject svn commit: r578952 - in /geronimo/sandbox/AsyncHttpClient/src: main/java/org/apache/ahc/ main/java/org/apache/ahc/codec/ test/java/org/apache/ahc/
Date Mon, 24 Sep 2007 20:31:29 GMT
Author: jgenender
Date: Mon Sep 24 13:31:28 2007
New Revision: 578952

URL: http://svn.apache.org/viewvc?rev=578952&view=rev
Log:
Added timeout code back in

Modified:
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java
    geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/TimeoutTest.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=578952&r1=578951&r2=578952&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 Mon
Sep 24 13:31:28 2007
@@ -34,9 +34,13 @@
 import java.security.GeneralSecurityException;
 import java.util.concurrent.ExecutorService;
 
+/**
+ * Main class to use for sending asyncronous HTTP requests to servers.
+ * Only one or a few of these objects should be used in an application since
+ * it manages the threads and requests to multiple seperate servers/sockets.
+ */
 public class AsyncHttpClient {
 
-    public static final int DEFAULT_REQUEST_TIMEOUT = 30000;
     public static final int DEFAULT_CONNECTION_TIMEOUT = 30000;
     public static final String DEFAULT_SSL_PROTOCOL = "TLS";
 
@@ -44,26 +48,24 @@
 
     private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
     private SocketConnector connector;
-    private int requestTimeout = DEFAULT_REQUEST_TIMEOUT;
     private static ExecutorService threadPool;
-    private IoHandler handler = new HttpIoHandler();
+    private HttpIoHandler handler = new HttpIoHandler();
     private SSLFilter sslFilter;
 
     public AsyncHttpClient() {
-        this(DEFAULT_CONNECTION_TIMEOUT, DEFAULT_REQUEST_TIMEOUT, null);
+        this(DEFAULT_CONNECTION_TIMEOUT, null);
     }
 
     public AsyncHttpClient(ExecutorService executor) {
-        this(DEFAULT_CONNECTION_TIMEOUT, DEFAULT_REQUEST_TIMEOUT, executor);
+        this(DEFAULT_CONNECTION_TIMEOUT, executor);
     }
 
     public AsyncHttpClient(int connectionTimeout) {
-        this(connectionTimeout, DEFAULT_REQUEST_TIMEOUT, null);
+        this(connectionTimeout, null);
     }
 
-    public AsyncHttpClient(int connectionTimeout, int requestTimeout, ExecutorService executor)
{
+    public AsyncHttpClient(int connectionTimeout, ExecutorService executor) {
         this.connectionTimeout = connectionTimeout;
-        this.requestTimeout = requestTimeout;
 
         threadPool = executor;
 
@@ -97,9 +99,12 @@
     }
 
     public void destroyAll() {
+        handler.destroy();
+
         if (threadPool != null) {
             threadPool.shutdownNow();
         }
+
         if (connector != null){
             connector.setWorkerTimeout(0);
         }

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java?rev=578952&r1=578951&r2=578952&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
(original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
Mon Sep 24 13:31:28 2007
@@ -19,13 +19,15 @@
  */
 package org.apache.ahc.codec;
 
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+
 import org.apache.ahc.AsyncHttpClientCallback;
-import org.apache.mina.common.ConnectFuture;
 import org.apache.mina.common.IoHandlerAdapter;
 import org.apache.mina.common.IoSession;
 
-import java.util.concurrent.ConcurrentHashMap;
-
 public class HttpIoHandler extends IoHandlerAdapter {
     
     public static final String CURRENT_REQUEST = "CURRENT_REQUEST";
@@ -34,30 +36,27 @@
     public static final int DEFAULT_THREAD_POOL_SIZE = 10;
     public static final String CONNECTION_CLOSE = "close";
 
-//    private ScheduledExecutorService scheduler;
+    private ScheduledExecutorService scheduler;
     
     public HttpIoHandler() {
+        scheduler = Executors.newSingleThreadScheduledExecutor();
     }
 
+    public void destroy(){
+        scheduler.shutdownNow();
+    }
+    
     public void sessionOpened(IoSession ioSession) throws Exception {
     }
 
     public void messageReceived(IoSession ioSession, Object object) throws Exception {
-        //For each send, we should have a response
+
         HttpResponseMessage response = (HttpResponseMessage)object;
-        //Remove the sent message
-//        HttpRequestMessage request = sentQueue.poll();
-//        ScheduledFuture handle = request.getTimeoutHandle();
-//        if (handle != null) {
-//
-//            boolean canceled = handle.cancel(true);
-//            //See if it canceled
-//            if (!canceled) {
-//                //Couldn't cancel it and it ran, so too late :-(
-//                return;
-//            }
-//        }
+
         HttpRequestMessage request = (HttpRequestMessage)ioSession.getAttribute(CURRENT_REQUEST);
+
+        cancelTasks(request);
+
         AsyncHttpClientCallback callback = request.getCallback();
         callback.onResponse(response);
         ioSession.close();
@@ -66,10 +65,10 @@
     public void exceptionCaught(IoSession ioSession, Throwable throwable) throws Exception
{
         //Clean up if any in-proccess decoding was occurring
         ioSession.removeAttribute(CURRENT_RESPONSE);
-//        if (timeoutDelay > 0) {
-//        }
 
         HttpRequestMessage request = (HttpRequestMessage)ioSession.getAttribute(CURRENT_REQUEST);
+        cancelTasks(request);
+
         AsyncHttpClientCallback callback = request.getCallback();
         callback.onException(throwable);
         
@@ -80,24 +79,36 @@
     public void sessionClosed(IoSession ioSession) throws Exception {
         //Clean up if any in-proccess decoding was occurring
         ioSession.removeAttribute(CURRENT_RESPONSE);
-//        if (timeoutDelay > 0) {
-//            scheduler.shutdownNow();
-//            scheduler = null;
-//        }
         HttpRequestMessage request = (HttpRequestMessage)ioSession.getAttribute(CURRENT_REQUEST);
+        cancelTasks(request);
         AsyncHttpClientCallback callback = request.getCallback();
         callback.onClosed();
     }
 
     public void messageSent(IoSession ioSession, Object object) throws Exception {
         HttpRequestMessage msg = (HttpRequestMessage)object;
-//        if (timeoutDelay > 0) {
-//            TimeoutTask task = new TimeoutTask(ioSession);
-//            scheduler.schedule(task, timeoutDelay, TimeUnit.MILLISECONDS);
-//        }
-    }
 
-   
+        //Start the tmeout timer now
+        if (msg.getTimeOut() > 0) {
+            TimeoutTask task = new TimeoutTask(ioSession);
+            ScheduledFuture handle = scheduler.schedule(task, msg.getTimeOut(), TimeUnit.MILLISECONDS);
+            msg.setTimeoutHandle(handle);
+        }
+    }
+
+    private void cancelTasks(HttpRequestMessage request){
+        ScheduledFuture handle = request.getTimeoutHandle();
+        if (handle != null){
+            boolean canceled = handle.cancel(true);
+            //See if it canceled
+            if (!canceled) {
+                //Couldn't cancel it and it ran, so too late :-(
+                return;
+            }
+            request.setTimeoutHandle(null);
+        }
+    }
+    
     class TimeoutTask implements Runnable {
 
         private IoSession sess;

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java?rev=578952&r1=578951&r2=578952&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java
(original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java
Mon Sep 24 13:31:28 2007
@@ -29,6 +29,8 @@
 
 public class HttpRequestMessage extends HttpMessage {
 
+    public static final int DEFAULT_REQUEST_TIMEOUT = 30000;
+
     public static final String REQUEST_GET = "GET";
     public static final String REQUEST_POST = "POST";
     public static final String REQUEST_HEAD = "HEAD";
@@ -44,10 +46,19 @@
     private boolean followRedirects = true;
     private ScheduledFuture timeoutHandle;
     private AsyncHttpClientCallback callback;
+    private int timeOut = DEFAULT_REQUEST_TIMEOUT;
 
     public HttpRequestMessage(URL url, AsyncHttpClientCallback callback) {
         this.url = url;
         this.callback = callback;
+    }
+
+    public int getTimeOut() {
+        return timeOut;
+    }
+
+    public void setTimeOut(int timeOut) {
+        this.timeOut = timeOut;
     }
 
     protected ScheduledFuture getTimeoutHandle() {

Modified: geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/TimeoutTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/TimeoutTest.java?rev=578952&r1=578951&r2=578952&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/TimeoutTest.java (original)
+++ geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/TimeoutTest.java Mon Sep
24 13:31:28 2007
@@ -28,14 +28,15 @@
 
     public void testTimeout() throws Exception {
 
-        /**
         TestCallback callback = new TestCallback();
 
-        //Create a client with a one second timeout
-        AsyncHttpClient ahc = new AsyncHttpClient(30000, 1000, null);
+        AsyncHttpClient ahc = new AsyncHttpClient();
 
         HttpRequestMessage request = new HttpRequestMessage(new URL("http://localhost:8282/timeout.jsp"),
callback);
 
+        //Create a client with a one second timeout
+        request.setTimeOut(1000);
+
         ahc.sendRequest(request);
 
         //We are done...Thread would normally end...
@@ -46,7 +47,7 @@
         }
 
         assertTrue(callback.isTimeout());
-     **/
 
+        ahc.destroyAll();
     }
 }



Mime
View raw message