Return-Path: Delivered-To: apmail-geronimo-scm-archive@www.apache.org Received: (qmail 29869 invoked from network); 6 Sep 2007 19:59:09 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 6 Sep 2007 19:59:09 -0000 Received: (qmail 4484 invoked by uid 500); 6 Sep 2007 19:59:03 -0000 Delivered-To: apmail-geronimo-scm-archive@geronimo.apache.org Received: (qmail 4444 invoked by uid 500); 6 Sep 2007 19:59:03 -0000 Mailing-List: contact scm-help@geronimo.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: dev@geronimo.apache.org List-Id: Delivered-To: mailing list scm@geronimo.apache.org Received: (qmail 4433 invoked by uid 99); 6 Sep 2007 19:59:03 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 06 Sep 2007 12:59:03 -0700 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 06 Sep 2007 20:00:27 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 2CD3B1A9838; Thu, 6 Sep 2007 12:58:45 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: scm@geronimo.apache.org From: jgenender@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20070906195845.2CD3B1A9838@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 @@ org.apache.mina mina-core - 1.1.1 + 1.1.2 org.apache.mina mina-filter-ssl - 1.1.1 + 1.1.2 org.slf4j 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 sendQueue = Collections.synchronizedList(new LinkedList()); + 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 sentQueue = Collections.synchronizedList(new LinkedList()); 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 {