View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.examples;
28  
29  import java.io.IOException;
30  import java.nio.ByteBuffer;
31  import java.util.List;
32  import java.util.concurrent.CountDownLatch;
33  import java.util.concurrent.TimeUnit;
34  
35  import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
36  import org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient;
37  import org.apache.hc.core5.http.ContentType;
38  import org.apache.hc.core5.http.EntityDetails;
39  import org.apache.hc.core5.http.Header;
40  import org.apache.hc.core5.http.HttpException;
41  import org.apache.hc.core5.http.HttpResponse;
42  import org.apache.hc.core5.http.config.Http1Config;
43  import org.apache.hc.core5.http.message.BasicHttpRequest;
44  import org.apache.hc.core5.http.message.StatusLine;
45  import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
46  import org.apache.hc.core5.http.nio.CapacityChannel;
47  import org.apache.hc.core5.http.nio.DataStreamChannel;
48  import org.apache.hc.core5.http.nio.RequestChannel;
49  import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer;
50  import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
51  import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
52  import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
53  import org.apache.hc.core5.http.protocol.HttpContext;
54  import org.apache.hc.core5.http.support.BasicRequestBuilder;
55  import org.apache.hc.core5.http2.HttpVersionPolicy;
56  import org.apache.hc.core5.http2.config.H2Config;
57  import org.apache.hc.core5.io.CloseMode;
58  import org.apache.hc.core5.reactor.IOReactorConfig;
59  import org.apache.hc.core5.util.Timeout;
60  
61  /**
62   * This example demonstrates a full-duplex, streaming HTTP/1.1 message exchange.
63   */
64  public class AsyncClientFullDuplexExchange {
65  
66      public static void main(final String[] args) throws Exception {
67  
68          final IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
69                  .setSoTimeout(Timeout.ofSeconds(5))
70                  .build();
71  
72          final MinimalHttpAsyncClient client = HttpAsyncClients.createMinimal(
73                  HttpVersionPolicy.NEGOTIATE,
74                  H2Config.DEFAULT,
75                  Http1Config.DEFAULT,
76                  ioReactorConfig);
77  
78          client.start();
79  
80          final BasicHttpRequest request = BasicRequestBuilder.post("http://httpbin.org/post").build();
81          final BasicRequestProducer requestProducer = new BasicRequestProducer(request,
82                  new BasicAsyncEntityProducer("stuff", ContentType.TEXT_PLAIN));
83          final BasicResponseConsumer<String> responseConsumer = new BasicResponseConsumer<>(
84                  new StringAsyncEntityConsumer());
85  
86          System.out.println("Executing request " + request);
87          final CountDownLatch latch = new CountDownLatch(1);
88          client.execute(new AsyncClientExchangeHandler() {
89  
90              @Override
91              public void releaseResources() {
92                  requestProducer.releaseResources();
93                  responseConsumer.releaseResources();
94                  latch.countDown();
95              }
96  
97              @Override
98              public void cancel() {
99                  System.out.println(request + " cancelled");
100             }
101 
102             @Override
103             public void failed(final Exception cause) {
104                 System.out.println(request + "->" + cause);
105             }
106 
107             @Override
108             public void produceRequest(final RequestChannel channel, final HttpContext context) throws HttpException, IOException {
109                 requestProducer.sendRequest(channel, context);
110             }
111 
112             @Override
113             public int available() {
114                 return requestProducer.available();
115             }
116 
117             @Override
118             public void produce(final DataStreamChannel channel) throws IOException {
119                 requestProducer.produce(channel);
120             }
121 
122             @Override
123             public void consumeInformation(
124                     final HttpResponse response,
125                     final HttpContext context) throws HttpException, IOException {
126                 System.out.println(request + "->" + new StatusLine(response));
127             }
128 
129             @Override
130             public void consumeResponse(
131                     final HttpResponse response,
132                     final EntityDetails entityDetails,
133                     final HttpContext context) throws HttpException, IOException {
134                 System.out.println(request + "->" + new StatusLine(response));
135                 responseConsumer.consumeResponse(response, entityDetails, context, null);
136             }
137 
138             @Override
139             public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
140                 responseConsumer.updateCapacity(capacityChannel);
141             }
142 
143             @Override
144             public void consume(final ByteBuffer src) throws IOException {
145                 responseConsumer.consume(src);
146             }
147 
148             @Override
149             public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
150                 responseConsumer.streamEnd(trailers);
151             }
152 
153         });
154         latch.await(1, TimeUnit.MINUTES);
155 
156         System.out.println("Shutting down");
157         client.close(CloseMode.GRACEFUL);
158     }
159 
160 }