You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2012/02/02 18:21:57 UTC
svn commit: r1239732 - in /httpcomponents/httpclient/trunk/fluent-hc/src:
examples/org/apache/http/client/fluent/
main/java/org/apache/http/client/fluent/
Author: olegk
Date: Thu Feb 2 17:21:56 2012
New Revision: 1239732
URL: http://svn.apache.org/viewvc?rev=1239732&view=rev
Log:
Added facility to execute requests asynchronously using fluent HC API
Added:
httpcomponents/httpclient/trunk/fluent-hc/src/examples/org/apache/http/client/fluent/FluentAsync.java (with props)
httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Async.java (with props)
httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/ContentResponseHandler.java
- copied, changed from r1239536, httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java
Modified:
httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java
httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Executor.java
httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Request.java
httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Response.java
Added: httpcomponents/httpclient/trunk/fluent-hc/src/examples/org/apache/http/client/fluent/FluentAsync.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/examples/org/apache/http/client/fluent/FluentAsync.java?rev=1239732&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/examples/org/apache/http/client/fluent/FluentAsync.java (added)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/examples/org/apache/http/client/fluent/FluentAsync.java Thu Feb 2 17:21:56 2012
@@ -0,0 +1,88 @@
+/*
+ * ====================================================================
+ *
+ * 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.client.fluent;
+
+import java.util.LinkedList;
+import java.util.Queue;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+import org.apache.http.concurrent.FutureCallback;
+
+/**
+ * This example demonstrates how the he HttpClient fluent API can be used to execute multiple
+ * requests asynchronously using background threads.
+ */
+public class FluentAsync {
+
+ public static void main(String[] args)throws Exception {
+ // Use pool of two threads
+ ExecutorService threadpool = Executors.newFixedThreadPool(2);
+ Async async = Async.newInstance().use(threadpool);
+
+ Request[] requests = new Request[] {
+ Request.Get("http://www.google.com/"),
+ Request.Get("http://www.yahoo.com/"),
+ Request.Get("http://www.apache.com/"),
+ Request.Get("http://www.apple.com/")
+ };
+
+
+ Queue<Future<Content>> queue = new LinkedList<Future<Content>>();
+ // Execute requests asynchronously
+ for (final Request request: requests) {
+ Future<Content> future = async.execute(request, new FutureCallback<Content>() {
+
+ public void failed(final Exception ex) {
+ System.out.println(ex.getMessage() + ": " + request);
+ }
+
+ public void completed(final Content content) {
+ System.out.println("Request completed: " + request);
+ }
+
+ public void cancelled() {
+ }
+
+ });
+ queue.add(future);
+ }
+
+ while(!queue.isEmpty()) {
+ Future<Content> future = queue.remove();
+ try {
+ future.get();
+ } catch (ExecutionException ex) {
+ }
+ }
+ System.out.println("Done");
+ threadpool.shutdown();
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/fluent-hc/src/examples/org/apache/http/client/fluent/FluentAsync.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/fluent-hc/src/examples/org/apache/http/client/fluent/FluentAsync.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/fluent-hc/src/examples/org/apache/http/client/fluent/FluentAsync.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Async.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Async.java?rev=1239732&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Async.java (added)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Async.java Thu Feb 2 17:21:56 2012
@@ -0,0 +1,119 @@
+/*
+ * ====================================================================
+ *
+ * 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.client.fluent;
+
+import java.util.concurrent.Future;
+
+import org.apache.http.client.ResponseHandler;
+import org.apache.http.concurrent.BasicFuture;
+import org.apache.http.concurrent.FutureCallback;
+
+public class Async {
+
+ private Executor executor;
+ private java.util.concurrent.Executor concurrentExec;
+
+ public static Async newInstance() {
+ return new Async();
+ }
+
+ Async() {
+ super();
+ }
+
+ public Async use(final Executor executor) {
+ this.executor = executor;
+ return this;
+ }
+
+ public Async use(final java.util.concurrent.Executor concurrentExec) {
+ this.concurrentExec = concurrentExec;
+ return this;
+ }
+
+ static class ExecRunnable<T> implements Runnable {
+
+ private final BasicFuture<T> future;
+ private final Request request;
+ private final Executor executor;
+ private final ResponseHandler<T> handler;
+
+ ExecRunnable(
+ final BasicFuture<T> future,
+ final Request request,
+ final Executor executor,
+ final ResponseHandler<T> handler) {
+ super();
+ this.future = future;
+ this.request = request;
+ this.executor = executor;
+ this.handler = handler;
+ }
+
+ public void run() {
+ try {
+ Response response = this.executor.execute(this.request);
+ T result = response.handleResponse(this.handler);
+ this.future.completed(result);
+ } catch (Exception ex) {
+ this.future.failed(ex);
+ }
+ }
+
+ }
+
+ public <T> Future<T> execute(
+ final Request request, final ResponseHandler<T> handler, final FutureCallback<T> callback) {
+ BasicFuture<T> future = new BasicFuture<T>(callback);
+ ExecRunnable<T> runnable = new ExecRunnable<T>(
+ future,
+ request,
+ this.executor != null ? this.executor : Executor.newInstance(),
+ handler);
+ if (this.concurrentExec != null) {
+ this.concurrentExec.execute(runnable);
+ } else {
+ Thread t = new Thread(runnable);
+ t.setDaemon(true);
+ t.start();
+ }
+ return future;
+ }
+
+ public <T> Future<T> execute(final Request request, final ResponseHandler<T> handler) {
+ return execute(request, handler, null);
+ }
+
+ public Future<Content> execute(final Request request, final FutureCallback<Content> callback) {
+ return execute(request, new ContentResponseHandler(), callback);
+ }
+
+ public Future<Content> execute(final Request request) {
+ return execute(request, new ContentResponseHandler(), null);
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Async.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Async.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Async.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java?rev=1239732&r1=1239731&r2=1239732&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java (original)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java Thu Feb 2 17:21:56 2012
@@ -70,4 +70,9 @@ public class Content {
return new ByteArrayInputStream(this.raw);
}
+ @Override
+ public String toString() {
+ return asString();
+ }
+
}
Copied: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/ContentResponseHandler.java (from r1239536, httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/ContentResponseHandler.java?p2=httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/ContentResponseHandler.java&p1=httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java&r1=1239536&r2=1239732&rev=1239732&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java (original)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/ContentResponseHandler.java Thu Feb 2 17:21:56 2012
@@ -26,48 +26,34 @@
package org.apache.http.client.fluent;
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
+import java.io.IOException;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.StatusLine;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.HttpResponseException;
+import org.apache.http.client.ResponseHandler;
import org.apache.http.entity.ContentType;
-import org.apache.http.protocol.HTTP;
+import org.apache.http.util.EntityUtils;
-public class Content {
+class ContentResponseHandler implements ResponseHandler<Content> {
- public static final Content NO_CONTENT = new Content(new byte[] {}, ContentType.DEFAULT_BINARY);
-
- private final byte[] raw;
- private final ContentType type;
-
- Content(final byte[] raw, final ContentType type) {
- super();
- this.raw = raw;
- this.type = type;
- }
-
- public ContentType getType() {
- return this.type;
- }
-
- public byte[] asBytes() {
- return this.raw.clone();
- }
-
- public String asString() {
- String charset = this.type.getCharset();
- if (charset == null) {
- charset = HTTP.DEFAULT_CONTENT_TYPE;
+ public Content handleResponse(
+ final HttpResponse response) throws ClientProtocolException, IOException {
+ StatusLine statusLine = response.getStatusLine();
+ HttpEntity entity = response.getEntity();
+ if (statusLine.getStatusCode() >= 300) {
+ throw new HttpResponseException(statusLine.getStatusCode(),
+ statusLine.getReasonPhrase());
}
- try {
- return new String(this.raw, charset);
- } catch (UnsupportedEncodingException ex) {
- return new String(this.raw);
+ if (entity != null) {
+ return new Content(
+ EntityUtils.toByteArray(entity),
+ ContentType.getOrDefault(entity));
+ } else {
+ return Content.NO_CONTENT;
}
}
- public InputStream asStream() {
- return new ByteArrayInputStream(this.raw);
- }
-
}
Modified: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Executor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Executor.java?rev=1239732&r1=1239731&r2=1239732&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Executor.java (original)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Executor.java Thu Feb 2 17:21:56 2012
@@ -41,7 +41,6 @@ import org.apache.http.client.Credential
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.protocol.ClientContext;
-import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
@@ -53,9 +52,16 @@ import org.apache.http.protocol.BasicHtt
public class Executor {
- final static PoolingClientConnectionManager CONNMGR = new PoolingClientConnectionManager(
- SchemeRegistryFactory.createSystemDefault());
- final static DefaultHttpClient CLIENT = new DefaultHttpClient(CONNMGR);
+ final static PoolingClientConnectionManager CONNMGR;
+ final static DefaultHttpClient CLIENT;
+
+ static {
+ CONNMGR = new PoolingClientConnectionManager(
+ SchemeRegistryFactory.createSystemDefault());
+ CONNMGR.setDefaultMaxPerRoute(100);
+ CONNMGR.setMaxTotal(200);
+ CLIENT = new DefaultHttpClient(CONNMGR);
+ }
public static Executor newInstance() {
return new Executor(CLIENT);
@@ -142,27 +148,15 @@ public class Executor {
}
public Response execute(
- final Request req) throws ClientProtocolException, IOException {
+ final Request request) throws ClientProtocolException, IOException {
this.localContext.setAttribute(ClientContext.CREDS_PROVIDER, this.credentialsProvider);
this.localContext.setAttribute(ClientContext.AUTH_CACHE, this.authCache);
this.localContext.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore);
- HttpRequestBase httprequest = req.getHttpRequest();
+ HttpRequestBase httprequest = request.getHttpRequest();
httprequest.reset();
return new Response(this.httpclient.execute(httprequest, this.localContext));
}
- public static void setMaxTotal(int max) {
- CONNMGR.setMaxTotal(max);
- }
-
- public static void setDefaultMaxPerRoute(int max) {
- CONNMGR.setDefaultMaxPerRoute(max);
- }
-
- public static void setMaxPerRoute(final HttpRoute route, int max) {
- CONNMGR.setMaxPerRoute(route, max);
- }
-
public static void registerScheme(final Scheme scheme) {
CONNMGR.getSchemeRegistry().register(scheme);
}
Modified: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Request.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Request.java?rev=1239732&r1=1239731&r2=1239732&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Request.java (original)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Request.java Thu Feb 2 17:21:56 2012
@@ -306,4 +306,9 @@ public class Request {
return body(new InputStreamEntity(instream, -1, contentType));
}
+ @Override
+ public String toString() {
+ return this.request.getRequestLine().toString();
+ }
+
}
Modified: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Response.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Response.java?rev=1239732&r1=1239731&r2=1239732&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Response.java (original)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Response.java Thu Feb 2 17:21:56 2012
@@ -83,26 +83,7 @@ public class Response {
}
public Content returnContent() throws ClientProtocolException, IOException {
- return handleResponse(new ResponseHandler<Content>() {
-
- public Content handleResponse(
- final HttpResponse response) throws ClientProtocolException, IOException {
- StatusLine statusLine = response.getStatusLine();
- HttpEntity entity = response.getEntity();
- if (statusLine.getStatusCode() >= 300) {
- throw new HttpResponseException(statusLine.getStatusCode(),
- statusLine.getReasonPhrase());
- }
- if (entity != null) {
- return new Content(
- EntityUtils.toByteArray(entity),
- ContentType.getOrDefault(entity));
- } else {
- return Content.NO_CONTENT;
- }
- }
-
- });
+ return handleResponse(new ContentResponseHandler());
}
public HttpResponse returnResponse() throws IOException {