hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1164406 - in /httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol: HttpAsyncClientExchangeHandlerImpl.java HttpAsyncRequestExecutor.java
Date Fri, 02 Sep 2011 08:48:55 GMT
Author: olegk
Date: Fri Sep  2 08:48:55 2011
New Revision: 1164406

URL: http://svn.apache.org/viewvc?rev=1164406&view=rev
Log:
Complete rewrite of core async HTTP protocol handlers (async request executor)

Added:
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java
  (with props)
Modified:
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandlerImpl.java

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandlerImpl.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandlerImpl.java?rev=1164406&r1=1164405&r2=1164406&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandlerImpl.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandlerImpl.java
Fri Sep  2 08:48:55 2011
@@ -34,6 +34,7 @@ import org.apache.http.HttpException;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
+import org.apache.http.concurrent.BasicFuture;
 import org.apache.http.nio.ContentDecoder;
 import org.apache.http.nio.ContentEncoder;
 import org.apache.http.nio.IOControl;
@@ -46,6 +47,7 @@ import org.apache.http.protocol.HttpProc
 
 class HttpAsyncClientExchangeHandlerImpl<T> implements HttpAsyncClientExchangeHandler<T>
{
 
+    private final BasicFuture<T> future;
     private final HttpAsyncRequestProducer requestProducer;
     private final HttpAsyncResponseConsumer<T> responseConsumer;
     private final HttpContext localContext;
@@ -55,6 +57,7 @@ class HttpAsyncClientExchangeHandlerImpl
     private final HttpParams params;
 
     public HttpAsyncClientExchangeHandlerImpl(
+            final BasicFuture<T> future,
             final HttpAsyncRequestProducer requestProducer,
             final HttpAsyncResponseConsumer<T> responseConsumer,
             final HttpContext localContext,
@@ -63,6 +66,9 @@ class HttpAsyncClientExchangeHandlerImpl
             final ConnectionReuseStrategy reuseStrategy,
             final HttpParams params) {
         super();
+        if (future == null) {
+            throw new IllegalArgumentException("Request future may not be null");
+        }
         if (requestProducer == null) {
             throw new IllegalArgumentException("Request producer may not be null");
         }
@@ -84,6 +90,7 @@ class HttpAsyncClientExchangeHandlerImpl
         if (params == null) {
             throw new IllegalArgumentException("HTTP parameters may not be null");
         }
+        this.future = future;
         this.requestProducer = requestProducer;
         this.responseConsumer = responseConsumer;
         this.localContext = localContext;
@@ -93,9 +100,22 @@ class HttpAsyncClientExchangeHandlerImpl
         this.params = params;
     }
 
+    private void releaseResources() {
+        try {
+            this.responseConsumer.close();
+        } catch (IOException ex) {
+        }
+        try {
+            this.requestProducer.close();
+        } catch (IOException ex) {
+        }
+    }
+
     public void close() throws IOException {
-        this.responseConsumer.close();
-        this.requestProducer.close();
+        releaseResources();
+        if (!this.future.isDone()) {
+            this.future.cancel(true);
+        }
     }
 
     public HttpHost getTarget() {
@@ -143,16 +163,41 @@ class HttpAsyncClientExchangeHandlerImpl
         this.responseConsumer.consumeContent(decoder, ioctrl);
     }
 
-    public void responseCompleted(final HttpContext context) {
-        this.responseConsumer.responseCompleted(context);
-    }
-
     public void failed(final Exception ex) {
-        this.responseConsumer.failed(ex);
+        try {
+            this.responseConsumer.failed(ex);
+        } finally {
+            try {
+                this.future.failed(ex);
+            } finally {
+                releaseResources();
+            }
+        }
     }
 
     public void cancel() {
-        this.responseConsumer.cancel();
+        try {
+            this.responseConsumer.cancel();
+        } catch (RuntimeException ex) {
+            failed(ex);
+            throw ex;
+        }
+    }
+
+    public void responseCompleted(final HttpContext context) {
+        try {
+            this.responseConsumer.responseCompleted(context);
+            T result = this.responseConsumer.getResult();
+            Exception ex = this.responseConsumer.getException();
+            if (ex == null) {
+                this.future.completed(result);
+            } else {
+                this.future.failed(ex);
+            }
+        } catch (RuntimeException ex) {
+            failed(ex);
+            throw ex;
+        }
     }
 
     public T getResult() {

Added: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java?rev=1164406&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java
Fri Sep  2 08:48:55 2011
@@ -0,0 +1,92 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.nio.protocol;
+
+import java.util.concurrent.Future;
+
+import org.apache.http.ConnectionReuseStrategy;
+import org.apache.http.concurrent.BasicFuture;
+import org.apache.http.concurrent.FutureCallback;
+import org.apache.http.nio.NHttpClientConnection;
+import org.apache.http.params.HttpParams;
+import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpProcessor;
+
+public class HttpAsyncRequestExecutor {
+
+    private final HttpProcessor httppocessor;
+    private final ConnectionReuseStrategy reuseStrategy;
+    private final HttpParams params;
+
+    public HttpAsyncRequestExecutor(
+            final HttpProcessor httppocessor,
+            final ConnectionReuseStrategy reuseStrategy,
+            final HttpParams params) {
+        super();
+        this.httppocessor = httppocessor;
+        this.reuseStrategy = reuseStrategy;
+        this.params = params;
+    }
+
+    public <T> Future<T> execute(
+            final HttpAsyncRequestProducer requestProducer,
+            final HttpAsyncResponseConsumer<T> responseConsumer,
+            final NHttpClientConnection conn,
+            final HttpContext context,
+            final FutureCallback<T> callback) {
+        if (conn == null) {
+            throw new IllegalArgumentException("HTTP connection may not be null");
+        }
+        if (context == null) {
+            throw new IllegalArgumentException("HTTP context may not be null");
+        }
+        BasicFuture<T> future = new BasicFuture<T>(callback);
+        HttpAsyncClientExchangeHandler<T> handler = new HttpAsyncClientExchangeHandlerImpl<T>(
+                future, requestProducer, responseConsumer, context,
+                this.httppocessor, conn, this.reuseStrategy, this.params);
+        conn.getContext().setAttribute(HttpAsyncClientProtocolHandler.HTTP_HANDLER, handler);
+        conn.requestOutput();
+        return future;
+    }
+
+    public <T> Future<T> execute(
+            final HttpAsyncRequestProducer requestProducer,
+            final HttpAsyncResponseConsumer<T> responseConsumer,
+            final NHttpClientConnection conn,
+            final HttpContext context) {
+        return execute(requestProducer, responseConsumer, conn, context);
+    }
+
+    public <T> Future<T> execute(
+            final HttpAsyncRequestProducer requestProducer,
+            final HttpAsyncResponseConsumer<T> responseConsumer,
+            final NHttpClientConnection conn) {
+        return execute(requestProducer, responseConsumer, conn, new BasicHttpContext());
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message