hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1463471 - in /httpcomponents/httpclient/trunk/httpclient/src: examples/org/apache/http/examples/client/ main/java/org/apache/http/client/async/ main/java/org/apache/http/impl/client/ test/java/org/apache/http/client/async/ test/java/org/ap...
Date Tue, 02 Apr 2013 10:22:56 GMT
Author: olegk
Date: Tue Apr  2 10:22:55 2013
New Revision: 1463471

URL: http://svn.apache.org/r1463471
Log:
Renamed HttpAsyncClientWithFuture to FutureRequestExecutionService; moved implementation classes
to o.a.h.impl.client package

Added:
    httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java
  (contents, props changed)
      - copied, changed from r1462087, httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAsyncWithFuture.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/FutureRequestExecutionMetrics.java
  (with props)
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/FutureRequestExecutionService.java
  (contents, props changed)
      - copied, changed from r1462087, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientWithFuture.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpRequestFutureTask.java
  (contents, props changed)
      - copied, changed from r1462087, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientFutureTask.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpRequestTaskCallable.java
  (contents, props changed)
      - copied, changed from r1462087, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientCallable.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestFutureRequestExecutionService.java
  (contents, props changed)
      - copied, changed from r1462087, httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/async/HttpClientWithFutureTest.java
Removed:
    httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAsyncWithFuture.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/async/ConnectionMetrics.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientCallable.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientFutureTask.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientWithFuture.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/async/HttpClientWithFutureTest.java
Modified:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClients.java

Copied: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java
(from r1462087, httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAsyncWithFuture.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java?p2=httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java&p1=httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAsyncWithFuture.java&r1=1462087&r2=1463471&rev=1463471&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAsyncWithFuture.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java
Tue Apr  2 10:22:55 2013
@@ -29,23 +29,31 @@ package org.apache.http.examples.client;
 import java.io.IOException;
 import java.util.List;
 import java.util.concurrent.CancellationException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.http.HttpResponse;
 import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.HttpClient;
 import org.apache.http.client.ResponseHandler;
-import org.apache.http.client.async.HttpAsyncClientFutureTask;
-import org.apache.http.client.async.HttpAsyncClientWithFuture;
 import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.protocol.HttpClientContext;
 import org.apache.http.concurrent.FutureCallback;
-import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.client.FutureRequestExecutionService;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpRequestFutureTask;
 
-public class ClientAsyncWithFuture {
+public class ClientWithRequestFuture {
 
     public static void main(String[] args) throws Exception {
         // the simplest way to create a HttpAsyncClientWithFuture
-        HttpAsyncClientWithFuture client = HttpClients.createAsync(3);
+        HttpClient httpclient = HttpClientBuilder.create()
+                .setMaxConnPerRoute(5)
+                .setMaxConnTotal(5).build();
+        ExecutorService execService = Executors.newFixedThreadPool(5);
+        FutureRequestExecutionService requestExecService = new FutureRequestExecutionService(httpclient,
execService);
 
         // Because things are asynchronous, you must provide a ResponseHandler
         ResponseHandler<Boolean> handler = new ResponseHandler<Boolean>() {
@@ -57,14 +65,16 @@ public class ClientAsyncWithFuture {
 
         // Simple request ...
         HttpGet request1 = new HttpGet("http://google.com");
-        HttpAsyncClientFutureTask<Boolean> futureTask1 = client.execute(request1, handler);
+        HttpRequestFutureTask<Boolean> futureTask1 = requestExecService.execute(request1,
+                HttpClientContext.create(), handler);
         Boolean wasItOk1 = futureTask1.get();
         System.out.println("It was ok? "  + wasItOk1);
 
         // Cancel a request
         try {
             HttpGet request2 = new HttpGet("http://google.com");
-            HttpAsyncClientFutureTask<Boolean> futureTask2 = client.execute(request2,
handler);
+            HttpRequestFutureTask<Boolean> futureTask2 = requestExecService.execute(request2,
+                    HttpClientContext.create(), handler);
             futureTask2.cancel(true);
             Boolean wasItOk2 = futureTask2.get();
             System.out.println("It was cancelled so it should never print this: " + wasItOk2);
@@ -74,7 +84,8 @@ public class ClientAsyncWithFuture {
 
         // Request with a timeout
         HttpGet request3 = new HttpGet("http://google.com");
-        HttpAsyncClientFutureTask<Boolean> futureTask3 = client.execute(request3, handler);
+        HttpRequestFutureTask<Boolean> futureTask3 = requestExecService.execute(request3,
+                HttpClientContext.create(), handler);
         Boolean wasItOk3 = futureTask3.get(10, TimeUnit.SECONDS);
         System.out.println("It was ok? "  + wasItOk3);
 
@@ -96,7 +107,8 @@ public class ClientAsyncWithFuture {
         HttpGet request4 = new HttpGet("http://google.com");
         // using a null HttpContext here since it is optional
         // the callback will be called when the task completes, fails, or is cancelled
-        HttpAsyncClientFutureTask<Boolean> futureTask4 = client.execute(request4, null,
handler, callback);
+        HttpRequestFutureTask<Boolean> futureTask4 = requestExecService.execute(request4,
+                HttpClientContext.create(), handler, callback);
         Boolean wasItOk4 = futureTask4.get(10, TimeUnit.SECONDS);
         System.out.println("It was ok? "  + wasItOk4);
 
@@ -106,8 +118,9 @@ public class ClientAsyncWithFuture {
         HttpGet request7 = new HttpGet("http://yahoo.com");
         // using a null HttpContext here since it is optional
         // the callback will be called for each request as their responses come back.
-        List<Future<Boolean>> futureTask = client.executeMultiple(null, handler,
callback,20,
-                TimeUnit.SECONDS, request5, request6, request7);
+        List<Future<Boolean>> futureTask = requestExecService.executeMultiple(
+                HttpClientContext.create(), handler, callback,
+                20,TimeUnit.SECONDS, request5, request6, request7);
         // you can still access the futures directly, if you want. The futures are in the
same order as the requests.
         for (Future<Boolean> future : futureTask) {
             System.out.println("another result " + future.get());

Propchange: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/FutureRequestExecutionMetrics.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/FutureRequestExecutionMetrics.java?rev=1463471&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/FutureRequestExecutionMetrics.java
(added)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/FutureRequestExecutionMetrics.java
Tue Apr  2 10:22:55 2013
@@ -0,0 +1,156 @@
+/*
+ * ====================================================================
+ * 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.client;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * Collection of different counters used to gather metrics for {@link FutureRequestExecutionService}.
+ */
+public final class FutureRequestExecutionMetrics {
+
+    private final AtomicLong activeConnections = new AtomicLong();
+    private final AtomicLong scheduledConnections = new AtomicLong();
+    private final DurationCounter successfulConnections = new DurationCounter();
+    private final DurationCounter failedConnections = new DurationCounter();
+    private final DurationCounter requests = new DurationCounter();
+    private final DurationCounter tasks = new DurationCounter();
+
+    FutureRequestExecutionMetrics() {
+    }
+
+    AtomicLong getActiveConnections() {
+        return activeConnections;
+    }
+
+    AtomicLong getScheduledConnections() {
+        return scheduledConnections;
+    }
+
+    DurationCounter getSuccessfulConnections() {
+        return successfulConnections;
+    }
+
+    DurationCounter getFailedConnections() {
+        return failedConnections;
+    }
+
+    DurationCounter getRequests() {
+        return requests;
+    }
+
+    DurationCounter getTasks() {
+        return tasks;
+    }
+
+    public long getActiveConnectionCount() {
+        return activeConnections.get();
+    }
+
+    public long getScheduledConnectionCount() {
+        return scheduledConnections.get();
+    }
+
+    public long getSuccessfulConnectionCount() {
+        return successfulConnections.count();
+    }
+
+    public long getSuccessfulConnectionAverageDuration() {
+        return successfulConnections.averageDuration();
+    }
+
+    public long getFailedConnectionCount() {
+        return failedConnections.count();
+    }
+
+    public long getFailedConnectionAverageDuration() {
+        return failedConnections.averageDuration();
+    }
+
+    public long getRequestCount() {
+        return requests.count();
+    }
+
+    public long getRequestAverageDuration() {
+        return requests.averageDuration();
+    }
+
+    public long getTaskCount() {
+        return tasks.count();
+    }
+
+    public long getTaskAverageDuration() {
+        return tasks.averageDuration();
+    }
+
+    @Override
+    public String toString() {
+        final StringBuilder builder = new StringBuilder();
+        builder.append("[activeConnections=").append(activeConnections)
+                .append(", scheduledConnections=").append(scheduledConnections)
+                .append(", successfulConnections=").append(successfulConnections)
+                .append(", failedConnections=").append(failedConnections)
+                .append(", requests=").append(requests)
+                .append(", tasks=").append(tasks)
+                .append("]");
+        return builder.toString();
+    }
+
+    /**
+     * A counter that can measure duration and number of events.
+     */
+    static class DurationCounter {
+
+        private final AtomicLong count = new AtomicLong(0);
+        private final AtomicLong cumulativeDuration = new AtomicLong(0);
+
+        public void increment(final long startTime) {
+            count.incrementAndGet();
+            cumulativeDuration.addAndGet(System.currentTimeMillis() - startTime);
+        }
+
+        public long count() {
+            return count.get();
+        }
+
+        public long averageDuration() {
+            final long counter = count.get();
+            return cumulativeDuration.get() / counter;
+        }
+
+        @Override
+        public String toString() {
+            final StringBuilder builder = new StringBuilder();
+            builder.append("[count=").append(count())
+                    .append(", averageDuration=").append(averageDuration())
+                    .append("]");
+            return builder.toString();
+        }
+
+    }
+
+}
\ No newline at end of file

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/FutureRequestExecutionMetrics.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/FutureRequestExecutionMetrics.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/FutureRequestExecutionMetrics.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/FutureRequestExecutionService.java
(from r1462087, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientWithFuture.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/FutureRequestExecutionService.java?p2=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/FutureRequestExecutionService.java&p1=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientWithFuture.java&r1=1462087&r2=1463471&rev=1463471&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientWithFuture.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/FutureRequestExecutionService.java
Tue Apr  2 10:22:55 2013
@@ -24,7 +24,7 @@
  * <http://www.apache.org/>.
  *
  */
-package org.apache.http.client.async;
+package org.apache.http.impl.client;
 
 import java.io.Closeable;
 import java.io.IOException;
@@ -41,27 +41,25 @@ import org.apache.http.annotation.Thread
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.ResponseHandler;
 import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.client.protocol.HttpClientContext;
 import org.apache.http.concurrent.FutureCallback;
-import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.protocol.HttpContext;
 
 /**
- * HttpAsyncClientWithFuture wraps calls to execute with a {@link HttpAsyncClientFutureTask}
+ * HttpAsyncClientWithFuture wraps calls to execute with a {@link HttpRequestFutureTask}
  * and schedules them using the provided executor service. Scheduled calls may be cancelled.
  * Similar to the non-blockcing HttpAsyncClient, a callback handler api is provided.
  */
 @ThreadSafe
-public class HttpAsyncClientWithFuture implements Closeable {
-    final HttpClient httpclient;
+public class FutureRequestExecutionService implements Closeable {
 
+    private final HttpClient httpclient;
     private final ExecutorService executorService;
-
-    private final ConnectionMetrics metrics = new ConnectionMetrics();
-
+    private final FutureRequestExecutionMetrics metrics = new FutureRequestExecutionMetrics();
     private final AtomicBoolean closed = new AtomicBoolean(false);
 
     /**
-     * Create a new HttpAsyncClientWithFuture.
+     * Create a new FutureRequestExecutionService.
      *
      * @param httpclient
      *            you should tune your httpclient instance to match your needs. You should
@@ -72,7 +70,7 @@ public class HttpAsyncClientWithFuture i
      * @param executorService
      *            any executorService will do here. E.g. {@link Executors#newFixedThreadPool(int)}
      */
-    public HttpAsyncClientWithFuture(
+    public FutureRequestExecutionService(
             final HttpClient httpclient,
             final ExecutorService executorService) {
         this.httpclient = httpclient;
@@ -91,10 +89,11 @@ public class HttpAsyncClientWithFuture i
      * @return HttpAsyncClientFutureTask for the scheduled request.
      * @throws InterruptedException
      */
-    public <T> HttpAsyncClientFutureTask<T> execute(
+    public <T> HttpRequestFutureTask<T> execute(
             final HttpUriRequest request,
-            final ResponseHandler<T> responseHandler) throws InterruptedException {
-        return execute(request, null, responseHandler, null);
+            final HttpContext context,
+            final ResponseHandler<T> responseHandler) {
+        return execute(request, context, responseHandler, null);
     }
 
     /**
@@ -114,18 +113,18 @@ public class HttpAsyncClientWithFuture i
      * @return HttpAsyncClientFutureTask for the scheduled request.
      * @throws InterruptedException
      */
-    public <T> HttpAsyncClientFutureTask<T> execute(
+    public <T> HttpRequestFutureTask<T> execute(
             final HttpUriRequest request,
             final HttpContext context,
             final ResponseHandler<T> responseHandler,
-            final FutureCallback<T> callback) throws InterruptedException {
+            final FutureCallback<T> callback) {
         if(closed.get()) {
             throw new IllegalStateException("Close has been called on this httpclient instance.");
         }
-        metrics.scheduledConnections.incrementAndGet();
-        final HttpAsyncClientCallable<T> callable = new HttpAsyncClientCallable<T>(
+        metrics.getScheduledConnections().incrementAndGet();
+        final HttpRequestTaskCallable<T> callable = new HttpRequestTaskCallable<T>(
             httpclient, request, context, responseHandler, callback, metrics);
-        final HttpAsyncClientFutureTask<T> httpRequestFutureTask = new HttpAsyncClientFutureTask<T>(
+        final HttpRequestFutureTask<T> httpRequestFutureTask = new HttpRequestFutureTask<T>(
             request, callable);
         executorService.execute(httpRequestFutureTask);
 
@@ -147,7 +146,7 @@ public class HttpAsyncClientWithFuture i
     public <T> List<Future<T>> executeMultiple(
             final ResponseHandler<T> responseHandler,
             final HttpUriRequest... requests) throws InterruptedException {
-        return executeMultiple(null, responseHandler, null, -1, null, requests);
+        return executeMultiple(HttpClientContext.create(), responseHandler, null, -1, null,
requests);
     }
 
     /**
@@ -175,10 +174,10 @@ public class HttpAsyncClientWithFuture i
             final FutureCallback<T> callback,
             final long timeout, final TimeUnit timeUnit,
             final HttpUriRequest... requests) throws InterruptedException {
-        metrics.scheduledConnections.incrementAndGet();
+        metrics.getScheduledConnections().incrementAndGet();
         final List<Callable<T>> callables = new ArrayList<Callable<T>>();
         for (final HttpUriRequest request : requests) {
-            final HttpAsyncClientCallable<T> callable = new HttpAsyncClientCallable<T>(
+            final HttpRequestTaskCallable<T> callable = new HttpRequestTaskCallable<T>(
                 httpclient, request, context, responseHandler, callback, metrics);
             callables.add(callable);
         }
@@ -191,17 +190,17 @@ public class HttpAsyncClientWithFuture i
 
     /**
      * @return metrics gathered for this instance.
-     * @see ConnectionMetrics
+     * @see FutureRequestExecutionMetrics
      */
-    public ConnectionMetrics metrics() {
+    public FutureRequestExecutionMetrics metrics() {
         return metrics;
     }
 
     public void close() throws IOException {
         closed.set(true);
         executorService.shutdownNow();
-        if(httpclient instanceof CloseableHttpClient) {
-            ((CloseableHttpClient) httpclient).close();
+        if (httpclient instanceof Closeable) {
+            ((Closeable) httpclient).close();
         }
     }
 }

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/FutureRequestExecutionService.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/FutureRequestExecutionService.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/FutureRequestExecutionService.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClients.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClients.java?rev=1463471&r1=1463470&r2=1463471&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClients.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClients.java
Tue Apr  2 10:22:55 2013
@@ -27,12 +27,7 @@
 
 package org.apache.http.impl.client;
 
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-
 import org.apache.http.annotation.Immutable;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.async.HttpAsyncClientWithFuture;
 import org.apache.http.conn.HttpClientConnectionManager;
 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
 
@@ -66,17 +61,4 @@ public class HttpClients {
         return new MinimalHttpClient(connManager);
     }
 
-    /**
-     * Creates a simple HttpAsyncClientWithFuture with an executor with the specified number
of threads and a matching httpclient.
-     * @param threads
-     *            the number of connections and threads used for the httpclient and the executor
used by @see
-     *            HttpAsyncClientWithFuture.
-     * @return a HttpAsyncClientWithFuture with an httpclient and executor that can handle
the specified amount of
-     *         threads/connections.
-     */
-    public static HttpAsyncClientWithFuture createAsync(int threads) {
-        HttpClient httpClient = HttpClientBuilder.create().setMaxConnPerRoute(5).setMaxConnTotal(5).build();
-        ExecutorService executorService = Executors.newFixedThreadPool(5);
-        return new HttpAsyncClientWithFuture(httpClient, executorService);
-    }
 }

Copied: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpRequestFutureTask.java
(from r1462087, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientFutureTask.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpRequestFutureTask.java?p2=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpRequestFutureTask.java&p1=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientFutureTask.java&r1=1462087&r2=1463471&rev=1463471&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientFutureTask.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpRequestFutureTask.java
Tue Apr  2 10:22:55 2013
@@ -24,7 +24,7 @@
  * <http://www.apache.org/>.
  *
  */
-package org.apache.http.client.async;
+package org.apache.http.impl.client;
 
 import java.util.concurrent.FutureTask;
 
@@ -36,15 +36,14 @@ import org.apache.http.client.methods.Ht
  *
  * @param <V>
  */
-public class HttpAsyncClientFutureTask<V> extends FutureTask<V> {
+public class HttpRequestFutureTask<V> extends FutureTask<V> {
 
     private final HttpUriRequest request;
+    private final HttpRequestTaskCallable<V> callable;
 
-    private final HttpAsyncClientCallable<V> callable;
-
-    public HttpAsyncClientFutureTask(
+    public HttpRequestFutureTask(
             final HttpUriRequest request,
-            final HttpAsyncClientCallable<V> httpCallable) {
+            final HttpRequestTaskCallable<V> httpCallable) {
         super(httpCallable);
         this.request = request;
         this.callable = httpCallable;
@@ -67,14 +66,14 @@ public class HttpAsyncClientFutureTask<V
      * @return the time in millis the task was scheduled.
      */
     public long scheduledTime() {
-        return callable.scheduled;
+        return callable.getScheduled();
     }
 
     /**
      * @return the time in millis the task was started.
      */
     public long startedTime() {
-        return callable.started;
+        return callable.getStarted();
     }
 
     /**
@@ -82,7 +81,7 @@ public class HttpAsyncClientFutureTask<V
      */
     public long endedTime() {
         if (isDone()) {
-            return callable.ended;
+            return callable.getEnded();
         } else {
             throw new IllegalStateException("Task is not done yet");
         }
@@ -115,4 +114,5 @@ public class HttpAsyncClientFutureTask<V
     public String toString() {
         return request.getRequestLine().getUri();
     }
+
 }
\ No newline at end of file

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpRequestFutureTask.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpRequestFutureTask.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpRequestFutureTask.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpRequestTaskCallable.java
(from r1462087, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientCallable.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpRequestTaskCallable.java?p2=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpRequestTaskCallable.java&p1=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientCallable.java&r1=1462087&r2=1463471&rev=1463471&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/async/HttpAsyncClientCallable.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpRequestTaskCallable.java
Tue Apr  2 10:22:55 2013
@@ -24,7 +24,7 @@
  * <http://www.apache.org/>.
  *
  */
-package org.apache.http.client.async;
+package org.apache.http.impl.client;
 
 import java.util.concurrent.Callable;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -35,40 +35,29 @@ import org.apache.http.client.methods.Ht
 import org.apache.http.concurrent.FutureCallback;
 import org.apache.http.protocol.HttpContext;
 
-/**
- * Implementation of Callable that is wrapped with a {@link HttpAsyncClientFutureTask} by
- * {@link HttpAsyncClientWithFuture}. The callable orchestrates the invocation of
- * {@link HttpClient#execute(HttpUriRequest, ResponseHandler, HttpContext)} and callbacks
in
- * {@link HttpAsyncClientCallback}.
- *
- * @param <V>
- *            type returned by the responseHandler
- */
-final class HttpAsyncClientCallable<V> implements Callable<V> {
+class HttpRequestTaskCallable<V> implements Callable<V> {
 
     private final HttpUriRequest request;
-
     private final HttpClient httpclient;
-
     private final AtomicBoolean cancelled = new AtomicBoolean(false);
 
-    final long scheduled = System.currentTimeMillis();
-    long started = -1;
-    long ended = -1;
+    private final long scheduled = System.currentTimeMillis();
+    private long started = -1;
+    private long ended = -1;
 
     private final HttpContext context;
     private final ResponseHandler<V> responseHandler;
     private final FutureCallback<V> callback;
 
-    private final ConnectionMetrics metrics;
+    private final FutureRequestExecutionMetrics metrics;
 
-    HttpAsyncClientCallable(
+    HttpRequestTaskCallable(
             final HttpClient httpClient,
             final HttpUriRequest request,
             final HttpContext context,
             final ResponseHandler<V> responseHandler,
             final FutureCallback<V> callback,
-            final ConnectionMetrics metrics) {
+            final FutureRequestExecutionMetrics metrics) {
         this.httpclient = httpClient;
         this.responseHandler = responseHandler;
         this.request = request;
@@ -77,26 +66,34 @@ final class HttpAsyncClientCallable<V> i
         this.metrics = metrics;
     }
 
-    /*
-     * (non-Javadoc)
-     * @see java.util.concurrent.Callable#call()
-     */
+    public long getScheduled() {
+        return scheduled;
+    }
+
+    public long getStarted() {
+        return started;
+    }
+
+    public long getEnded() {
+        return ended;
+    }
+
     public V call() throws Exception {
         if (!cancelled.get()) {
             try {
-                metrics.activeConnections.incrementAndGet();
+                metrics.getActiveConnections().incrementAndGet();
                 started = System.currentTimeMillis();
                 try {
-                    metrics.scheduledConnections.decrementAndGet();
+                    metrics.getScheduledConnections().decrementAndGet();
                     final V result = httpclient.execute(request, responseHandler, context);
                     ended = System.currentTimeMillis();
-                    metrics.successfulConnections.increment(started);
+                    metrics.getSuccessfulConnections().increment(started);
                     if (callback != null) {
                         callback.completed(result);
                     }
                     return result;
                 } catch (final Exception e) {
-                    metrics.failedConnections.increment(started);
+                    metrics.getFailedConnections().increment(started);
                     ended = System.currentTimeMillis();
                     if (callback != null) {
                         callback.failed(e);
@@ -104,9 +101,9 @@ final class HttpAsyncClientCallable<V> i
                     throw e;
                 }
             } finally {
-                metrics.requests.increment(started);
-                metrics.tasks.increment(started);
-                metrics.activeConnections.decrementAndGet();
+                metrics.getRequests().increment(started);
+                metrics.getTasks().increment(started);
+                metrics.getActiveConnections().decrementAndGet();
             }
         } else {
             throw new IllegalStateException("call has been cancelled for request " + request.getURI());

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpRequestTaskCallable.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpRequestTaskCallable.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpRequestTaskCallable.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestFutureRequestExecutionService.java
(from r1462087, httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/async/HttpClientWithFutureTest.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestFutureRequestExecutionService.java?p2=httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestFutureRequestExecutionService.java&p1=httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/async/HttpClientWithFutureTest.java&r1=1462087&r2=1463471&rev=1463471&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/async/HttpClientWithFutureTest.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestFutureRequestExecutionService.java
Tue Apr  2 10:22:55 2013
@@ -24,13 +24,15 @@
  * <http://www.apache.org/>.
  *
  */
-package org.apache.http.client.async;
+package org.apache.http.impl.client;
 
 import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.util.List;
 import java.util.concurrent.CancellationException;
 import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
@@ -42,10 +44,11 @@ import org.apache.http.HttpException;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.HttpClient;
 import org.apache.http.client.ResponseHandler;
 import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.protocol.HttpClientContext;
 import org.apache.http.concurrent.FutureCallback;
-import org.apache.http.impl.client.HttpClients;
 import org.apache.http.localserver.LocalTestServer;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpRequestHandler;
@@ -57,7 +60,7 @@ public class HttpClientWithFutureTest {
 
     private LocalTestServer localServer;
     private String uri;
-    private HttpAsyncClientWithFuture httpAsyncClientWithFuture;
+    private FutureRequestExecutionService httpAsyncClientWithFuture;
 
     private final AtomicBoolean blocked = new AtomicBoolean(false);
 
@@ -82,7 +85,11 @@ public class HttpClientWithFutureTest {
             this.localServer.start();
             final InetSocketAddress address = localServer.getServiceAddress();
             uri = "http://" + address.getHostName() + ":" + address.getPort() + "/wait";
-            httpAsyncClientWithFuture = HttpClients.createAsync(5);
+            final HttpClient httpClient = HttpClientBuilder.create()
+                    .setMaxConnPerRoute(5)
+                    .build();
+            final ExecutorService executorService = Executors.newFixedThreadPool(5);
+            httpAsyncClientWithFuture = new FutureRequestExecutionService(httpClient, executorService);
     }
 
     @After
@@ -94,15 +101,15 @@ public class HttpClientWithFutureTest {
 
     @Test
     public void shouldExecuteSingleCall() throws InterruptedException, ExecutionException
{
-        final HttpAsyncClientFutureTask<Boolean> task = httpAsyncClientWithFuture.execute(
-            new HttpGet(uri), new OkidokiHandler());
+        final HttpRequestFutureTask<Boolean> task = httpAsyncClientWithFuture.execute(
+            new HttpGet(uri), HttpClientContext.create(), new OkidokiHandler());
         Assert.assertTrue("request should have returned OK", task.get().booleanValue());
     }
 
     @Test(expected=CancellationException.class)
     public void shouldCancel() throws InterruptedException, ExecutionException {
-        final HttpAsyncClientFutureTask<Boolean> task = httpAsyncClientWithFuture.execute(
-            new HttpGet(uri), new OkidokiHandler());
+        final HttpRequestFutureTask<Boolean> task = httpAsyncClientWithFuture.execute(
+            new HttpGet(uri), HttpClientContext.create(), new OkidokiHandler());
         task.cancel(true);
         task.get();
     }
@@ -110,8 +117,8 @@ public class HttpClientWithFutureTest {
     @Test(expected=TimeoutException.class)
     public void shouldTimeout() throws InterruptedException, ExecutionException, TimeoutException
{
         blocked.set(true);
-        final HttpAsyncClientFutureTask<Boolean> task = httpAsyncClientWithFuture.execute(
-            new HttpGet(uri), new OkidokiHandler());
+        final HttpRequestFutureTask<Boolean> task = httpAsyncClientWithFuture.execute(
+            new HttpGet(uri), HttpClientContext.create(), new OkidokiHandler());
         task.get(10, TimeUnit.MILLISECONDS);
     }
 

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestFutureRequestExecutionService.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestFutureRequestExecutionService.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestFutureRequestExecutionService.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message