[prev in list] [next in list] [prev in thread] [next in thread]
List: httpcomponents-commits
Subject: svn commit: r1053876 - in /httpcomponents/httpasyncclient/trunk/src:
From: olegk () apache ! org
Date: 2010-12-30 12:42:03
Message-ID: 20101230124203.865182388ABB () eris ! apache ! org
[Download RAW message or body]
Author: olegk
Date: Thu Dec 30 12:42:02 2010
New Revision: 1053876
URL: http://svn.apache.org/viewvc?rev=1053876&view=rev
Log:
Keep track of pedning responses; terminate pending HTTP exchanges upon unexpected \
exception in the I/O reactor thread
Added:
httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/HttpAsyncResponseSet.java \
(with props) httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/ResponseCompletedCallback.java \
(with props) Modified:
httpcomponents/httpasyncclient/trunk/src/examples/org/apache/http/examples/nio/client/AsyncClientRequest.java
httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/BasicHttpAsyncClient.java
httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java
httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/SchemeRegistryFactory.java
httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/nio/client/HttpAsyncClient.java
httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/nio/conn/ssl/SSLLayeringStrategy.java
Modified: httpcomponents/httpasyncclient/trunk/src/examples/org/apache/http/examples/nio/client/AsyncClientRequest.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/examples/or \
g/apache/http/examples/nio/client/AsyncClientRequest.java?rev=1053876&r1=1053875&r2=1053876&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/examples/org/apache/http/examples/nio/client/AsyncClientRequest.java \
(original)
+++ httpcomponents/httpasyncclient/trunk/src/examples/org/apache/http/examples/nio/client/AsyncClientRequest.java \
Thu Dec 30 12:42:02 2010 @@ -30,10 +30,9 @@ import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.Future;
-import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.nio.client.BasicHttpAsyncClient;
-import org.apache.http.message.BasicHttpRequest;
import org.apache.http.nio.client.HttpAsyncClient;
public class AsyncClientRequest {
@@ -42,11 +41,10 @@ public class AsyncClientRequest {
HttpAsyncClient httpclient = new BasicHttpAsyncClient();
httpclient.start();
try {
- HttpHost target = new HttpHost("www.apache.org", 80);
Queue<Future<HttpResponse>> queue = new \
LinkedList<Future<HttpResponse>>(); for (int i = 0; i < 10; i++) {
- BasicHttpRequest request = new BasicHttpRequest("GET", "/");
- queue.add(httpclient.execute(target, request, null));
+ HttpGet request = new HttpGet("http://www.apache.org/");
+ queue.add(httpclient.execute(request, null));
}
while (!queue.isEmpty()) {
Future<HttpResponse> future = queue.remove();
Modified: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/BasicHttpAsyncClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/o \
rg/apache/http/impl/nio/client/BasicHttpAsyncClient.java?rev=1053876&r1=1053875&r2=1053876&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/BasicHttpAsyncClient.java \
(original)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/BasicHttpAsyncClient.java \
Thu Dec 30 12:42:02 2010 @@ -27,6 +27,8 @@
package org.apache.http.impl.nio.client;
import java.io.IOException;
+import java.net.URI;
+import java.util.Iterator;
import java.util.concurrent.Future;
import org.apache.commons.logging.Log;
@@ -36,6 +38,8 @@ import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.nio.conn.DefaultHttpAsyncRoutePlanner;
@@ -44,6 +48,7 @@ import org.apache.http.impl.nio.reactor.
import org.apache.http.nio.client.HttpAsyncClient;
import org.apache.http.nio.client.HttpAsyncRequestProducer;
import org.apache.http.nio.client.HttpAsyncResponseConsumer;
+import org.apache.http.nio.concurrent.BasicFuture;
import org.apache.http.nio.concurrent.FutureCallback;
import org.apache.http.nio.conn.ClientConnectionManager;
import org.apache.http.nio.reactor.ConnectingIOReactor;
@@ -70,9 +75,9 @@ public class BasicHttpAsyncClient implem
private final HttpParams params;
private final ConnectingIOReactor ioReactor;
private final ClientConnectionManager connmgr;
+ private final HttpAsyncResponseSet pendingResponses;
private Thread reactorThread;
- private volatile Exception ex;
public BasicHttpAsyncClient(
final ConnectingIOReactor ioReactor,
@@ -87,6 +92,7 @@ public class BasicHttpAsyncClient implem
}
this.ioReactor = ioReactor;
this.connmgr = connmgr;
+ this.pendingResponses = new HttpAsyncResponseSet();
}
public BasicHttpAsyncClient(
@@ -100,6 +106,7 @@ public class BasicHttpAsyncClient implem
}
this.ioReactor = new DefaultConnectingIOReactor(2, this.params);
this.connmgr = new PoolingClientConnectionManager(this.ioReactor);
+ this.pendingResponses = new HttpAsyncResponseSet();
}
public BasicHttpAsyncClient() throws IOReactorException {
@@ -155,7 +162,12 @@ public class BasicHttpAsyncClient implem
this.ioReactor.execute(ioEventDispatch);
} catch (Exception ex) {
this.log.error("I/O reactor terminated abnormally", ex);
- this.ex = ex;
+ Iterator<HttpAsyncResponseConsumer<?>> it = \
this.pendingResponses.iterator(); + while (it.hasNext()) {
+ HttpAsyncResponseConsumer<?> responseConsumer = it.next();
+ responseConsumer.failed(ex);
+ it.remove();
+ }
}
}
@@ -167,10 +179,6 @@ public class BasicHttpAsyncClient implem
return this.ioReactor.getStatus();
}
- public Exception getException() {
- return this.ex;
- }
-
public synchronized void start() {
this.reactorThread = new Thread() {
@@ -200,6 +208,7 @@ public class BasicHttpAsyncClient implem
final HttpAsyncResponseConsumer<T> responseConsumer,
final HttpContext context,
final FutureCallback<T> callback) {
+ this.pendingResponses.add(responseConsumer);
DefaultAsyncRequestDirector<T> httpexchange;
synchronized (this) {
httpexchange = new DefaultAsyncRequestDirector<T>(
@@ -207,7 +216,8 @@ public class BasicHttpAsyncClient implem
requestProducer,
responseConsumer,
context,
- callback,
+ new ResponseCompletedCallback<T>(callback,
+ responseConsumer, this.pendingResponses),
this.connmgr,
createHttpProcessor(),
createHttpRoutePlanner(),
@@ -238,4 +248,43 @@ public class BasicHttpAsyncClient implem
final FutureCallback<HttpResponse> callback) {
return execute(target, request, new BasicHttpContext(), callback);
}
+
+ private HttpHost determineTarget(final HttpUriRequest request) throws \
ClientProtocolException { + // A null target may be acceptable if there is a \
default target. + // Otherwise, the null target is detected in the director.
+ HttpHost target = null;
+
+ URI requestURI = request.getURI();
+ if (requestURI.isAbsolute()) {
+ if (requestURI.getHost() == null) {
+ throw new ClientProtocolException(
+ "URI does not specify a valid host name: " + requestURI);
+ }
+ target = new HttpHost(requestURI.getHost(), requestURI.getPort(), \
requestURI.getScheme()); + // TODO use URIUtils#extractTarget once it \
becomes available + }
+ return target;
+ }
+
+ public Future<HttpResponse> execute(
+ final HttpUriRequest request,
+ final FutureCallback<HttpResponse> callback) {
+ return execute(request, new BasicHttpContext(), callback);
+ }
+
+ public Future<HttpResponse> execute(
+ final HttpUriRequest request,
+ final HttpContext context,
+ final FutureCallback<HttpResponse> callback) {
+ HttpHost target;
+ try {
+ target = determineTarget(request);
+ } catch (ClientProtocolException ex) {
+ BasicFuture<HttpResponse> future = new \
BasicFuture<HttpResponse>(callback); + future.failed(ex);
+ return future;
+ }
+ return execute(target, request, context, callback);
+ }
+
}
Modified: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/o \
rg/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java?rev=1053876&r1=1053875&r2=1053876&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java \
(original)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java \
Thu Dec 30 12:42:02 2010 @@ -132,9 +132,7 @@ class DefaultAsyncRequestDirector<T> imp
this.route, userToken,
connectTimeout, TimeUnit.MILLISECONDS,
new InternalFutureCallback());
- } catch (HttpException ex) {
- failed(ex);
- } catch (IOException ex) {
+ } catch (Exception ex) {
failed(ex);
}
}
Added: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/HttpAsyncResponseSet.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/HttpAsyncResponseSet.java?rev=1053876&view=auto
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/HttpAsyncResponseSet.java \
(added)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/HttpAsyncResponseSet.java \
Thu Dec 30 12:42:02 2010 @@ -0,0 +1,55 @@
+/*
+ * ====================================================================
+ * 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.impl.nio.client;
+
+import java.util.Iterator;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.http.nio.client.HttpAsyncResponseConsumer;
+
+class HttpAsyncResponseSet {
+
+ private final ConcurrentHashMap<HttpAsyncResponseConsumer<?>, Boolean> map;
+
+ public HttpAsyncResponseSet() {
+ super();
+ this.map = new ConcurrentHashMap<HttpAsyncResponseConsumer<?>, Boolean>();
+ }
+
+ public void add(final HttpAsyncResponseConsumer<?> o) {
+ this.map.put(o, Boolean.TRUE);
+ }
+
+ public void remove(final Object o) {
+ this.map.remove(o);
+ }
+
+ public Iterator<HttpAsyncResponseConsumer<?>> iterator() {
+ return this.map.keySet().iterator();
+ }
+
+}
Propchange: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/HttpAsyncResponseSet.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/HttpAsyncResponseSet.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/HttpAsyncResponseSet.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/ResponseCompletedCallback.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/o \
rg/apache/http/impl/nio/client/ResponseCompletedCallback.java?rev=1053876&view=auto \
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/ResponseCompletedCallback.java \
(added)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/ResponseCompletedCallback.java \
Thu Dec 30 12:42:02 2010 @@ -0,0 +1,69 @@
+/*
+ * ====================================================================
+ * 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.impl.nio.client;
+
+import org.apache.http.nio.client.HttpAsyncResponseConsumer;
+import org.apache.http.nio.concurrent.FutureCallback;
+
+class ResponseCompletedCallback<T> implements FutureCallback<T> {
+
+ private final FutureCallback<T> callback;
+ private final HttpAsyncResponseConsumer<T> responseConsumer;
+ private final HttpAsyncResponseSet set;
+
+ public ResponseCompletedCallback(
+ final FutureCallback<T> callback,
+ final HttpAsyncResponseConsumer<T> responseConsumer,
+ final HttpAsyncResponseSet set) {
+ super();
+ this.callback = callback;
+ this.responseConsumer = responseConsumer;
+ this.set = set;
+ }
+
+ public void completed(final T result) {
+ this.set.remove(this.responseConsumer);
+ if (this.callback != null) {
+ this.callback.completed(result);
+ }
+ }
+
+ public void failed(final Exception ex) {
+ this.set.remove(this.responseConsumer);
+ if (this.callback != null) {
+ this.callback.failed(ex);
+ }
+ }
+
+ public void cancelled() {
+ this.set.remove(this.responseConsumer);
+ if (this.callback != null) {
+ this.callback.cancelled();
+ }
+ }
+
+}
Propchange: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/ResponseCompletedCallback.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/ResponseCompletedCallback.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/ResponseCompletedCallback.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/SchemeRegistryFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/o \
rg/apache/http/impl/nio/conn/SchemeRegistryFactory.java?rev=1053876&r1=1053875&r2=1053876&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/SchemeRegistryFactory.java \
(original)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/SchemeRegistryFactory.java \
Thu Dec 30 12:42:02 2010 @@ -42,7 +42,7 @@ public final class SchemeRegistryFactory
registry.register(
new Scheme("http", 80, null));
registry.register(
- new Scheme("https", 443, \
SSLLayeringStrategy.getLayeringStrategy())); + new Scheme("https", \
443, SSLLayeringStrategy.getDefaultStrategy())); return registry;
}
Modified: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/nio/client/HttpAsyncClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/o \
rg/apache/http/nio/client/HttpAsyncClient.java?rev=1053876&r1=1053875&r2=1053876&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/nio/client/HttpAsyncClient.java \
(original)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/nio/client/HttpAsyncClient.java \
Thu Dec 30 12:42:02 2010 @@ -31,6 +31,7 @@ import java.util.concurrent.Future;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.nio.concurrent.FutureCallback;
import org.apache.http.nio.reactor.IOReactorStatus;
import org.apache.http.params.HttpParams;
@@ -65,4 +66,12 @@ public interface HttpAsyncClient {
HttpHost target, HttpRequest request,
FutureCallback<HttpResponse> callback);
+ Future<HttpResponse> execute(
+ HttpUriRequest request, HttpContext context,
+ FutureCallback<HttpResponse> callback);
+
+ Future<HttpResponse> execute(
+ HttpUriRequest request,
+ FutureCallback<HttpResponse> callback);
+
}
Modified: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/nio/conn/ssl/SSLLayeringStrategy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/o \
rg/apache/http/nio/conn/ssl/SSLLayeringStrategy.java?rev=1053876&r1=1053875&r2=1053876&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/nio/conn/ssl/SSLLayeringStrategy.java \
(original)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/nio/conn/ssl/SSLLayeringStrategy.java \
Thu Dec 30 12:42:02 2010 @@ -60,10 +60,8 @@ public class SSLLayeringStrategy impleme
public static final String SSL = "SSL";
public static final String SSLV2 = "SSLv2";
- private static final SSLLayeringStrategy DEFAULT_STRATEGY = new \
SSLLayeringStrategy();
-
- public static SSLLayeringStrategy getLayeringStrategy() {
- return DEFAULT_STRATEGY;
+ public static SSLLayeringStrategy getDefaultStrategy() {
+ return new SSLLayeringStrategy();
}
private final SSLContext sslContext;
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic