Return-Path: X-Original-To: apmail-hc-commits-archive@www.apache.org Delivered-To: apmail-hc-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 76FA67C2E for ; Fri, 2 Sep 2011 08:49:33 +0000 (UTC) Received: (qmail 65853 invoked by uid 500); 2 Sep 2011 08:49:32 -0000 Delivered-To: apmail-hc-commits-archive@hc.apache.org Received: (qmail 65802 invoked by uid 500); 2 Sep 2011 08:49:24 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 65781 invoked by uid 99); 2 Sep 2011 08:49:18 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 02 Sep 2011 08:49:18 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 02 Sep 2011 08:49:16 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id DC057238897D for ; Fri, 2 Sep 2011 08:48:55 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@hc.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110902084855.DC057238897D@eris.apache.org> 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 implements HttpAsyncClientExchangeHandler { + private final BasicFuture future; private final HttpAsyncRequestProducer requestProducer; private final HttpAsyncResponseConsumer responseConsumer; private final HttpContext localContext; @@ -55,6 +57,7 @@ class HttpAsyncClientExchangeHandlerImpl private final HttpParams params; public HttpAsyncClientExchangeHandlerImpl( + final BasicFuture future, final HttpAsyncRequestProducer requestProducer, final HttpAsyncResponseConsumer 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 + * . + * + */ +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 Future execute( + final HttpAsyncRequestProducer requestProducer, + final HttpAsyncResponseConsumer responseConsumer, + final NHttpClientConnection conn, + final HttpContext context, + final FutureCallback 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 future = new BasicFuture(callback); + HttpAsyncClientExchangeHandler handler = new HttpAsyncClientExchangeHandlerImpl( + future, requestProducer, responseConsumer, context, + this.httppocessor, conn, this.reuseStrategy, this.params); + conn.getContext().setAttribute(HttpAsyncClientProtocolHandler.HTTP_HANDLER, handler); + conn.requestOutput(); + return future; + } + + public Future execute( + final HttpAsyncRequestProducer requestProducer, + final HttpAsyncResponseConsumer responseConsumer, + final NHttpClientConnection conn, + final HttpContext context) { + return execute(requestProducer, responseConsumer, conn, context); + } + + public Future execute( + final HttpAsyncRequestProducer requestProducer, + final HttpAsyncResponseConsumer 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