hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1598512 - in /httpcomponents/httpcore/trunk/httpcore-nio/src: examples/org/apache/http/examples/nio/ main/java/org/apache/http/nio/protocol/
Date Fri, 30 May 2014 09:11:31 GMT
Author: olegk
Date: Fri May 30 09:11:31 2014
New Revision: 1598512

URL: http://svn.apache.org/r1598512
Log:
Added pipelining HTTP client example

Added:
    httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/PipeliningHttpClient.java
  (with props)
Modified:
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequester.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/PipeliningClientExchangeHandler.java

Added: httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/PipeliningHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/PipeliningHttpClient.java?rev=1598512&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/PipeliningHttpClient.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/PipeliningHttpClient.java
Fri May 30 09:11:31 2014
@@ -0,0 +1,161 @@
+/*
+ * ====================================================================
+ * 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.examples.nio;
+
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+
+import org.apache.http.HttpHost;
+import org.apache.http.HttpResponse;
+import org.apache.http.concurrent.FutureCallback;
+import org.apache.http.config.ConnectionConfig;
+import org.apache.http.impl.nio.DefaultHttpClientIODispatch;
+import org.apache.http.impl.nio.pool.BasicNIOConnPool;
+import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
+import org.apache.http.message.BasicHttpRequest;
+import org.apache.http.nio.protocol.BasicAsyncRequestProducer;
+import org.apache.http.nio.protocol.BasicAsyncResponseConsumer;
+import org.apache.http.nio.protocol.HttpAsyncRequestExecutor;
+import org.apache.http.nio.protocol.HttpAsyncRequester;
+import org.apache.http.nio.reactor.ConnectingIOReactor;
+import org.apache.http.nio.reactor.IOEventDispatch;
+import org.apache.http.protocol.HttpCoreContext;
+import org.apache.http.protocol.HttpProcessor;
+import org.apache.http.protocol.HttpProcessorBuilder;
+import org.apache.http.protocol.RequestConnControl;
+import org.apache.http.protocol.RequestContent;
+import org.apache.http.protocol.RequestExpectContinue;
+import org.apache.http.protocol.RequestTargetHost;
+import org.apache.http.protocol.RequestUserAgent;
+
+/**
+ * Minimal pipelining HTTP/1.1 client.
+ * <p/>
+ * Please note that this example represents a minimal HTTP client implementation.
+ * It does not support HTTPS as is.
+ * You either need to provide BasicNIOConnPool with a connection factory
+ * that supports SSL or use a more complex HttpAsyncClient.
+ *
+ * @see org.apache.http.impl.nio.pool.BasicNIOConnPool#BasicNIOConnPool(org.apache.http.nio.reactor.ConnectingIOReactor,
+ *   org.apache.http.nio.pool.NIOConnFactory, int)
+ * @see org.apache.http.impl.nio.pool.BasicNIOConnFactory
+ */
+public class PipeliningHttpClient {
+
+    public static void main(String[] args) throws Exception {
+        // Create HTTP protocol processing chain
+        HttpProcessor httpproc = HttpProcessorBuilder.create()
+                // Use standard client-side protocol interceptors
+                .add(new RequestContent())
+                .add(new RequestTargetHost())
+                .add(new RequestConnControl())
+                .add(new RequestUserAgent("Test/1.1"))
+                .add(new RequestExpectContinue(true)).build();
+        // Create client-side HTTP protocol handler
+        HttpAsyncRequestExecutor protocolHandler = new HttpAsyncRequestExecutor();
+        // Create client-side I/O event dispatch
+        final IOEventDispatch ioEventDispatch = new DefaultHttpClientIODispatch(protocolHandler,
+                ConnectionConfig.DEFAULT);
+        // Create client-side I/O reactor
+        final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
+        // Create HTTP connection pool
+        BasicNIOConnPool pool = new BasicNIOConnPool(ioReactor, ConnectionConfig.DEFAULT);
+        // Limit total number of connections to just two
+        pool.setDefaultMaxPerRoute(2);
+        pool.setMaxTotal(2);
+        // Run the I/O reactor in a separate thread
+        Thread t = new Thread(new Runnable() {
+
+            public void run() {
+                try {
+                    // Ready to go!
+                    ioReactor.execute(ioEventDispatch);
+                } catch (InterruptedIOException ex) {
+                    System.err.println("Interrupted");
+                } catch (IOException e) {
+                    System.err.println("I/O error: " + e.getMessage());
+                }
+                System.out.println("Shutdown");
+            }
+
+        });
+        // Start the client thread
+        t.start();
+        // Create HTTP requester
+        HttpAsyncRequester requester = new HttpAsyncRequester(httpproc);
+
+        final HttpHost target = new HttpHost("www.apache.org");
+        List<BasicAsyncRequestProducer> requestProducers = Arrays.asList(
+                new BasicAsyncRequestProducer(target, new BasicHttpRequest("GET", "/index.html")),
+                new BasicAsyncRequestProducer(target, new BasicHttpRequest("GET", "/foundation/index.html")),
+                new BasicAsyncRequestProducer(target, new BasicHttpRequest("GET", "/foundation/how-it-works.html"))
+        );
+        List<BasicAsyncResponseConsumer> responseConsumers = Arrays.asList(
+                new BasicAsyncResponseConsumer(),
+                new BasicAsyncResponseConsumer(),
+                new BasicAsyncResponseConsumer()
+        );
+
+        final CountDownLatch latch = new CountDownLatch(1);
+
+        HttpCoreContext context = HttpCoreContext.create();
+        requester.executePipelined(
+                target, requestProducers, responseConsumers, pool, context,
+                new FutureCallback<List<HttpResponse>>() {
+
+                    @Override
+                    public void completed(final List<HttpResponse> result) {
+                        latch.countDown();
+                        for (HttpResponse response: result) {
+                            System.out.println(target + "->" + response.getStatusLine());
+                        }
+                    }
+
+                    @Override
+                    public void failed(final Exception ex) {
+                        latch.countDown();
+                        System.out.println(target + "->" + ex);
+                    }
+
+                    @Override
+                    public void cancelled() {
+                        latch.countDown();
+                        System.out.println(target + " cancelled");
+                    }
+
+                });
+
+        latch.await();
+        System.out.println("Shutting down I/O reactor");
+        ioReactor.shutdown();
+        System.out.println("Done");
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/PipeliningHttpClient.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/PipeliningHttpClient.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

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

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequester.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequester.java?rev=1598512&r1=1598511&r2=1598512&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequester.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequester.java
Fri May 30 09:11:31 2014
@@ -243,8 +243,8 @@ public class HttpAsyncRequester {
      */
     public <T, E extends PoolEntry<HttpHost, NHttpClientConnection>> Future<List<T>>
executePipelined(
             final HttpHost target,
-            final List<HttpAsyncRequestProducer> requestProducers,
-            final List<HttpAsyncResponseConsumer<T>> responseConsumers,
+            final List<? extends HttpAsyncRequestProducer> requestProducers,
+            final List<? extends HttpAsyncResponseConsumer<T>> responseConsumers,
             final ConnPool<HttpHost, E> connPool,
             final HttpContext context,
             final FutureCallback<List<T>> callback) {
@@ -447,15 +447,15 @@ public class HttpAsyncRequester {
     class ConnPipelinedRequestCallback<T, E extends PoolEntry<HttpHost, NHttpClientConnection>>
implements FutureCallback<E> {
 
         private final BasicFuture<List<T>> requestFuture;
-        private final List<HttpAsyncRequestProducer> requestProducers;
-        private final List<HttpAsyncResponseConsumer<T>> responseConsumers;
+        private final List<? extends HttpAsyncRequestProducer> requestProducers;
+        private final List<? extends HttpAsyncResponseConsumer<T>> responseConsumers;
         private final ConnPool<HttpHost, E> connPool;
         private final HttpContext context;
 
         ConnPipelinedRequestCallback(
                 final BasicFuture<List<T>> requestFuture,
-                final List<HttpAsyncRequestProducer> requestProducers,
-                final List<HttpAsyncResponseConsumer<T>> responseConsumers,
+                final List<? extends HttpAsyncRequestProducer> requestProducers,
+                final List<? extends HttpAsyncResponseConsumer<T>> responseConsumers,
                 final ConnPool<HttpHost, E> connPool,
                 final HttpContext context) {
             super();

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/PipeliningClientExchangeHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/PipeliningClientExchangeHandler.java?rev=1598512&r1=1598511&r2=1598512&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/PipeliningClientExchangeHandler.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/PipeliningClientExchangeHandler.java
Fri May 30 09:11:31 2014
@@ -79,10 +79,10 @@ public class PipeliningClientExchangeHan
     private volatile boolean done;
 
     /**
-     * Creates new instance of BasicAsyncRequestExecutionHandler.
+     * Creates new instance of <tt>PipeliningClientExchangeHandler<tt/>.
      *
-     * @param requestProducer the request producer.
-     * @param responseConsumer the response consumer.
+     * @param requestProducers the request producers.
+     * @param responseConsumers the response consumers.
      * @param callback the future callback invoked when the operation is completed.
      * @param localContext the local execution context.
      * @param conn the actual connection.
@@ -90,8 +90,8 @@ public class PipeliningClientExchangeHan
      * @param connReuseStrategy the connection re-use strategy.
      */
     public PipeliningClientExchangeHandler(
-            final List<HttpAsyncRequestProducer> requestProducers,
-            final List<HttpAsyncResponseConsumer<T>> responseConsumers,
+            final List<? extends HttpAsyncRequestProducer> requestProducers,
+            final List<? extends HttpAsyncResponseConsumer<T>> responseConsumers,
             final FutureCallback<List<T>> callback,
             final HttpContext localContext,
             final NHttpClientConnection conn,
@@ -116,17 +116,17 @@ public class PipeliningClientExchangeHan
     }
 
     /**
-     * Creates new instance of BasicAsyncRequestExecutionHandler.
+     * Creates new instance of <tt>PipeliningClientExchangeHandler<tt/>.
      *
-     * @param requestProducer the request producer.
-     * @param responseConsumer the response consumer.
+     * @param requestProducers the request producers.
+     * @param responseConsumers the response consumers.
      * @param localContext the local execution context.
      * @param conn the actual connection.
      * @param httppocessor the HTTP protocol processor.
      */
     public PipeliningClientExchangeHandler(
-            final List<HttpAsyncRequestProducer> requestProducers,
-            final List<HttpAsyncResponseConsumer<T>> responseConsumers,
+            final List<? extends HttpAsyncRequestProducer> requestProducers,
+            final List<? extends HttpAsyncResponseConsumer<T>> responseConsumers,
             final HttpContext localContext,
             final NHttpClientConnection conn,
             final HttpProcessor httppocessor) {



Mime
View raw message