|
| 1 | +package com.wdbyte.httpclient; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.CharBuffer; |
| 5 | +import java.util.concurrent.CountDownLatch; |
| 6 | +import java.util.concurrent.ExecutionException; |
| 7 | +import java.util.concurrent.Future; |
| 8 | + |
| 9 | +import org.apache.hc.client5.http.async.methods.AbstractCharResponseConsumer; |
| 10 | +import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; |
| 11 | +import org.apache.hc.client5.http.async.methods.SimpleHttpRequests; |
| 12 | +import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; |
| 13 | +import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; |
| 14 | +import org.apache.hc.client5.http.impl.async.HttpAsyncClients; |
| 15 | +import org.apache.hc.core5.concurrent.FutureCallback; |
| 16 | +import org.apache.hc.core5.http.ContentType; |
| 17 | +import org.apache.hc.core5.http.HttpException; |
| 18 | +import org.apache.hc.core5.http.HttpResponse; |
| 19 | +import org.apache.hc.core5.http.nio.AsyncRequestProducer; |
| 20 | +import org.apache.hc.core5.http.nio.support.AsyncRequestBuilder; |
| 21 | + |
| 22 | +/** |
| 23 | + * HttpClient 5 异步请求 |
| 24 | +* @author https://www.wdbyte.com |
| 25 | + * @date 2022/06/25 |
| 26 | + */ |
| 27 | +public class HttpClient5Async { |
| 28 | + |
| 29 | + public static void main(String[] args) { |
| 30 | + getAsync1("http://httpbin.org/get"); |
| 31 | + getAsync2("http://httpbin.org/get"); |
| 32 | + getAsync3("http://httpbin.org/get"); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * 异步请求 |
| 37 | + * |
| 38 | + * @param url |
| 39 | + * @return |
| 40 | + */ |
| 41 | + public static String getAsync1(String url) { |
| 42 | + try (CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault()) { |
| 43 | + // 开始 http clinet |
| 44 | + httpclient.start(); |
| 45 | + // 执行请求 |
| 46 | + SimpleHttpRequest request1 = SimpleHttpRequests.get(url); |
| 47 | + Future<SimpleHttpResponse> future = httpclient.execute(request1, null); |
| 48 | + // 等待直到返回完毕 |
| 49 | + SimpleHttpResponse response1 = future.get(); |
| 50 | + System.out.println("getAsync1:" + request1.getRequestUri() + "->" + response1.getCode()); |
| 51 | + } catch (IOException | ExecutionException | InterruptedException e) { |
| 52 | + throw new RuntimeException(e); |
| 53 | + } |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * 异步请求,根据响应情况回调 |
| 59 | + * |
| 60 | + * @param url |
| 61 | + * @return |
| 62 | + */ |
| 63 | + public static String getAsync2(String url) { |
| 64 | + try (CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault()) { |
| 65 | + // 开始 http clinet |
| 66 | + httpclient.start(); |
| 67 | + // 根据请求响应情况进行回调操作 |
| 68 | + CountDownLatch latch = new CountDownLatch(1); |
| 69 | + SimpleHttpRequest request = SimpleHttpRequests.get(url); |
| 70 | + httpclient.execute(request, new FutureCallback<SimpleHttpResponse>() { |
| 71 | + @Override |
| 72 | + public void completed(SimpleHttpResponse response2) { |
| 73 | + latch.countDown(); |
| 74 | + System.out.println("getAsync2:" + request.getRequestUri() + "->" + response2.getCode()); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void failed(Exception ex) { |
| 79 | + latch.countDown(); |
| 80 | + System.out.println("getAsync2:" + request.getRequestUri() + "->" + ex); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void cancelled() { |
| 85 | + latch.countDown(); |
| 86 | + System.out.println("getAsync2:" + request.getRequestUri() + " cancelled"); |
| 87 | + } |
| 88 | + |
| 89 | + }); |
| 90 | + latch.await(); |
| 91 | + } catch (IOException | InterruptedException e) { |
| 92 | + throw new RuntimeException(e); |
| 93 | + } |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * 异步请求,对响应流做点什么 |
| 99 | + * |
| 100 | + * @param url |
| 101 | + * @return |
| 102 | + */ |
| 103 | + public static String getAsync3(String url) { |
| 104 | + try (CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault()) { |
| 105 | + // 开始 http clinet |
| 106 | + httpclient.start(); |
| 107 | + // 根据请求响应情况进行回调操作 |
| 108 | + SimpleHttpRequest request = SimpleHttpRequests.get(url); |
| 109 | + |
| 110 | + CountDownLatch latch = new CountDownLatch(1); |
| 111 | + AsyncRequestProducer producer = AsyncRequestBuilder.get("http://httpbin.org/get").build(); |
| 112 | + AbstractCharResponseConsumer<HttpResponse> consumer3 = new AbstractCharResponseConsumer<HttpResponse>() { |
| 113 | + |
| 114 | + HttpResponse response; |
| 115 | + |
| 116 | + @Override |
| 117 | + protected void start(HttpResponse response, ContentType contentType) throws HttpException, IOException { |
| 118 | + System.out.println("getAsync3: 开始响应...."); |
| 119 | + this.response = response; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + protected int capacityIncrement() { |
| 124 | + return Integer.MAX_VALUE; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + protected void data(CharBuffer data, boolean endOfStream) throws IOException { |
| 129 | + System.out.println("getAsync3: 收到数据...."); |
| 130 | + // Do something useful |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + protected HttpResponse buildResult() throws IOException { |
| 135 | + System.out.println("getAsync3: 接收完毕..."); |
| 136 | + return response; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void releaseResources() { |
| 141 | + } |
| 142 | + |
| 143 | + }; |
| 144 | + httpclient.execute(producer, consumer3, new FutureCallback<HttpResponse>() { |
| 145 | + |
| 146 | + @Override |
| 147 | + public void completed(HttpResponse response) { |
| 148 | + latch.countDown(); |
| 149 | + System.out.println("getAsync3: "+request.getRequestUri() + "->" + response.getCode()); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void failed(Exception ex) { |
| 154 | + latch.countDown(); |
| 155 | + System.out.println("getAsync3: "+request.getRequestUri() + "->" + ex); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public void cancelled() { |
| 160 | + latch.countDown(); |
| 161 | + System.out.println("getAsync3: "+request.getRequestUri() + " cancelled"); |
| 162 | + } |
| 163 | + |
| 164 | + }); |
| 165 | + latch.await(); |
| 166 | + } catch (IOException | InterruptedException e) { |
| 167 | + throw new RuntimeException(e); |
| 168 | + } |
| 169 | + return null; |
| 170 | + |
| 171 | + } |
| 172 | +} |
0 commit comments