hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1631187 - in /httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client: AsyncClientPipelined.java AsyncClientPipelinedStreaming.java
Date Sun, 12 Oct 2014 15:15:16 GMT
Author: olegk
Date: Sun Oct 12 15:15:16 2014
New Revision: 1631187

URL: http://svn.apache.org/r1631187
Log:
Added examples of pipelined request execution

Added:
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientPipelined.java
  (with props)
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientPipelinedStreaming.java
  (with props)

Added: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientPipelined.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientPipelined.java?rev=1631187&view=auto
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientPipelined.java
(added)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientPipelined.java
Sun Oct 12 15:15:16 2014
@@ -0,0 +1,71 @@
+/*
+ * ====================================================================
+ * 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.client;
+
+import java.util.Arrays;
+import java.util.List;
+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.HttpGet;
+import org.apache.http.impl.nio.client.CloseableHttpPipeliningClient;
+import org.apache.http.impl.nio.client.HttpAsyncClients;
+
+/**
+ * This example demonstrates a pipelinfed execution of multiple HTTP request / response exchanges
+ * Response content is buffered in memory for simplicity.
+ */
+public class AsyncClientPipelined {
+
+    public static void main(final String[] args) throws Exception {
+        CloseableHttpPipeliningClient httpclient = HttpAsyncClients.createPipelining();
+        try {
+            httpclient.start();
+
+            HttpHost targetHost = new HttpHost("localhost", 8080);
+            HttpGet[] resquests = {
+                    new HttpGet("/docs/index.html"),
+                    new HttpGet("/docs/introduction.html"),
+                    new HttpGet("/docs/setup.html"),
+                    new HttpGet("/docs/config/index.html")
+            };
+
+            Future<List<HttpResponse>> future = httpclient.execute(targetHost,
+                    Arrays.<HttpRequest>asList(resquests), null);
+            List<HttpResponse> responses = future.get();
+            System.out.println(responses);
+
+            System.out.println("Shutting down");
+        } finally {
+            httpclient.close();
+        }
+        System.out.println("Done");
+    }
+
+}

Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientPipelined.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientPipelined.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

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

Added: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientPipelinedStreaming.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientPipelinedStreaming.java?rev=1631187&view=auto
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientPipelinedStreaming.java
(added)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientPipelinedStreaming.java
Sun Oct 12 15:15:16 2014
@@ -0,0 +1,136 @@
+/*
+ * ====================================================================
+ * 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.client;
+
+import java.io.IOException;
+import java.nio.CharBuffer;
+import java.util.ArrayList;
+import java.util.List;
+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.HttpGet;
+import org.apache.http.impl.nio.client.CloseableHttpPipeliningClient;
+import org.apache.http.impl.nio.client.HttpAsyncClients;
+import org.apache.http.nio.IOControl;
+import org.apache.http.nio.client.methods.AsyncCharConsumer;
+import org.apache.http.nio.protocol.BasicAsyncRequestProducer;
+import org.apache.http.protocol.HttpContext;
+
+/**
+ * This example demonstrates a pipelinfed execution of multiple HTTP request / response exchanges
+ * with a full content streaming.
+ */
+public class AsyncClientPipelinedStreaming {
+
+    public static void main(final String[] args) throws Exception {
+        CloseableHttpPipeliningClient httpclient = HttpAsyncClients.createPipelining();
+        try {
+            httpclient.start();
+
+            HttpHost targetHost = new HttpHost("localhost", 8080);
+            HttpGet[] resquests = {
+                    new HttpGet("/docs/index.html"),
+                    new HttpGet("/docs/introduction.html"),
+                    new HttpGet("/docs/setup.html"),
+                    new HttpGet("/docs/config/index.html")
+            };
+
+            List<MyRequestProducer> requestProducers = new ArrayList<MyRequestProducer>();
+            List<MyResponseConsumer> responseConsumers = new ArrayList<MyResponseConsumer>();
+            for (HttpGet request: resquests) {
+                requestProducers.add(new MyRequestProducer(targetHost, request));
+                responseConsumers.add(new MyResponseConsumer(request));
+            }
+
+            Future<List<Boolean>> future = httpclient.execute(
+                    targetHost, requestProducers, responseConsumers, null);
+            future.get();
+            System.out.println("Shutting down");
+        } finally {
+            httpclient.close();
+        }
+        System.out.println("Done");
+    }
+
+    static class MyRequestProducer extends BasicAsyncRequestProducer {
+
+        private final HttpRequest request;
+
+        MyRequestProducer(final HttpHost target, final HttpRequest request) {
+            super(target, request);
+            this.request = request;
+        }
+
+        @Override
+        public void requestCompleted(final HttpContext context) {
+            super.requestCompleted(context);
+            System.out.println();
+            System.out.println("Request sent: " + this.request.getRequestLine());
+            System.out.println("=================================================");
+        }
+    }
+
+    static class MyResponseConsumer extends AsyncCharConsumer<Boolean> {
+
+        private final HttpRequest request;
+
+        MyResponseConsumer(final HttpRequest request) {
+            this.request = request;
+        }
+
+        @Override
+        protected void onResponseReceived(final HttpResponse response) {
+            System.out.println();
+            System.out.println("Response received: " + response.getStatusLine() + " ->
" + this.request.getRequestLine());
+            System.out.println("=================================================");
+        }
+
+        @Override
+        protected void onCharReceived(final CharBuffer buf, final IOControl ioctrl) throws
IOException {
+            while (buf.hasRemaining()) {
+                System.out.print(buf.get());
+            }
+        }
+
+        @Override
+        protected void releaseResources() {
+        }
+
+        @Override
+        protected Boolean buildResult(final HttpContext context) {
+            System.out.println();
+            System.out.println("=================================================");
+            System.out.println();
+            return Boolean.TRUE;
+        }
+
+    }
+
+}

Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientPipelinedStreaming.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientPipelinedStreaming.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

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



Mime
View raw message