geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgenen...@apache.org
Subject svn commit: r573352 - in /geronimo/sandbox/AsyncHttpClient: pom.xml src/main/java/org/apache/ahc/AsyncHttpClient.java src/main/java/org/apache/ahc/codec/HttpIoHandler.java
Date Thu, 06 Sep 2007 19:58:44 GMT
Author: jgenender
Date: Thu Sep  6 12:58:44 2007
New Revision: 573352

URL: http://svn.apache.org/viewvc?rev=573352&view=rev
Log:
Use queues to allow the connect to be asynchronous as well

Modified:
    geronimo/sandbox/AsyncHttpClient/pom.xml
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java

Modified: geronimo/sandbox/AsyncHttpClient/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/pom.xml?rev=573352&r1=573351&r2=573352&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/pom.xml (original)
+++ geronimo/sandbox/AsyncHttpClient/pom.xml Thu Sep  6 12:58:44 2007
@@ -38,12 +38,12 @@
         <dependency>
             <groupId>org.apache.mina</groupId>
             <artifactId>mina-core</artifactId>
-            <version>1.1.1</version>
+            <version>1.1.2</version>
         </dependency>
         <dependency>
             <groupId>org.apache.mina</groupId>
             <artifactId>mina-filter-ssl</artifactId>
-            <version>1.1.1</version>
+            <version>1.1.2</version>
         </dependency>
         <dependency>
             <groupId>org.slf4j</groupId>

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=573352&r1=573351&r2=573352&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 Thu
Sep  6 12:58:44 2007
@@ -19,23 +19,24 @@
  */
 package org.apache.ahc;
 
-import org.apache.mina.transport.socket.nio.SocketConnector;
-import org.apache.mina.common.IoConnectorConfig;
+import org.apache.ahc.codec.HttpIoHandler;
+import org.apache.ahc.codec.HttpProtocolCodecFactory;
+import org.apache.ahc.codec.HttpRequestMessage;
+import org.apache.ahc.ssl.TrustManagerFactoryImpl;
 import org.apache.mina.common.ConnectFuture;
+import org.apache.mina.common.IoConnectorConfig;
 import org.apache.mina.common.IoSession;
 import org.apache.mina.filter.SSLFilter;
 import org.apache.mina.filter.codec.ProtocolCodecFilter;
-import org.apache.ahc.ssl.TrustManagerFactoryImpl;
-import org.apache.ahc.codec.HttpRequestMessage;
-import org.apache.ahc.codec.HttpProtocolCodecFactory;
-import org.apache.ahc.codec.HttpIoHandler;
-import org.apache.ahc.util.AsyncHttpClientException;
+import org.apache.mina.transport.socket.nio.SocketConnector;
 
 import javax.net.ssl.SSLContext;
-import java.net.URL;
 import java.net.InetSocketAddress;
+import java.net.URL;
 import java.security.GeneralSecurityException;
-import java.io.IOException;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
 
 public class AsyncHttpClient {
 
@@ -50,9 +51,11 @@
     private IoSession session;
     private AsyncHttpClientCallback callback;
     private int requestTimeout = DEFAULT_REQUEST_TIMEOUT;
+    private List<HttpRequestMessage> sendQueue = Collections.synchronizedList(new LinkedList<HttpRequestMessage>());
+    private ConnectionListener connectionListener = new ConnectionListener();
 
     public AsyncHttpClient(URL url, AsyncHttpClientCallback callback) {
-        this(url, callback,DEFAULT_CONNECTION_TIMEOUT, DEFAULT_REQUEST_TIMEOUT);
+        this(url, callback, DEFAULT_CONNECTION_TIMEOUT, DEFAULT_REQUEST_TIMEOUT);
     }
 
     public AsyncHttpClient(URL url, AsyncHttpClientCallback callback, int connectionTimeout)
{
@@ -67,10 +70,11 @@
     }
 
     public void connect() throws Exception {
+        sendQueue.clear();
         SocketConnector connector = new SocketConnector();
         IoConnectorConfig cfg = connector.getDefaultConfig();
 
-        cfg.setConnectTimeout(connectionTimeout/1000);
+        cfg.setConnectTimeout(connectionTimeout / 1000);
 
         String scheme = url.getProtocol();
         int port = url.getPort();
@@ -86,14 +90,9 @@
             port = 80;
         }
 
-        cfg.getFilterChain().addLast("protocolFilter",new ProtocolCodecFilter( new HttpProtocolCodecFactory(url)));
+        cfg.getFilterChain().addLast("protocolFilter", new ProtocolCodecFilter(new HttpProtocolCodecFactory(url)));
 
-        ConnectFuture future = connector.connect(new InetSocketAddress(url.getHost(), port),
new HttpIoHandler(callback, requestTimeout));
-        future.join();
-        if (!future.isConnected()) {
-            throw new IOException("Cannot connect to " + url.toString());
-        }
-        session = future.getSession();
+        ConnectFuture future = connector.connect(new InetSocketAddress(url.getHost(), port),
new HttpIoHandler(connectionListener, callback, requestTimeout));
     }
 
     public void disconnect() {
@@ -103,12 +102,9 @@
         session = null;
     }
 
-    public void sendRequest(HttpRequestMessage message){
-        if (session == null || !session.isConnected()){
-            callback.onException(new AsyncHttpClientException("Objectdoes not have a valid
connection to a server."));
-            return;
-        }
-        session.write(message);
+    public void sendRequest(HttpRequestMessage message) {
+        sendQueue.add(message);
+        processSendQueue();
     }
 
     public int getConnectionTimeout() {
@@ -127,10 +123,27 @@
         this.sslProtocol = sslProtocol;
     }
 
+    private synchronized void processSendQueue() {
+        if (session != null && session.isConnected()) {
+            while (sendQueue.size() > 0) {
+                //Process messages in FIFO
+                HttpRequestMessage message = sendQueue.remove(0);
+                session.write(message);
+            }
+        }
+    }
+
     private SSLContext createClientSSLContext() throws GeneralSecurityException {
         SSLContext context = SSLContext.getInstance(sslProtocol);
         context.init(null, TrustManagerFactoryImpl.X509_MANAGERS, null);
         return context;
+    }
+
+    public class ConnectionListener {
+        public void onConnected(IoSession sess) {
+            session = sess;
+            processSendQueue();
+        }
     }
 
 }

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=573352&r1=573351&r2=573352&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
Thu Sep  6 12:58:44 2007
@@ -22,6 +22,7 @@
 import org.apache.mina.common.IoHandlerAdapter;
 import org.apache.mina.common.IoSession;
 import org.apache.ahc.AsyncHttpClientCallback;
+import org.apache.ahc.AsyncHttpClient;
 
 import java.util.LinkedList;
 import java.util.Collections;
@@ -40,8 +41,10 @@
     private List<HttpRequestMessage> sentQueue = Collections.synchronizedList(new LinkedList<HttpRequestMessage>());
     private ScheduledThreadPoolExecutor scheduler = null;
     private int timeoutDelay;
+    private AsyncHttpClient.ConnectionListener connectionListener = null;
 
-    public HttpIoHandler(AsyncHttpClientCallback callback, int timeoutDelay) {
+    public HttpIoHandler(AsyncHttpClient.ConnectionListener connectionListener, AsyncHttpClientCallback
callback, int timeoutDelay) {
+        this.connectionListener = connectionListener;
         this.callback = callback;
         this.timeoutDelay = timeoutDelay;
     }
@@ -54,6 +57,7 @@
             //to prevent memory leaks
             scheduler.scheduleWithFixedDelay(new SchedulePurger(), 5000, 5000, TimeUnit.MILLISECONDS);
         }
+        connectionListener.onConnected(ioSession);
     }
 
     public void messageReceived(IoSession ioSession, Object object) throws Exception {



Mime
View raw message