Skip to content
Permalink
Browse files
Improved async client examples
  • Loading branch information
ok2c committed Mar 14, 2021
1 parent 1174c24 commit 567b53d4b1130a2522970bb76f6c5eef8555435c
Showing 13 changed files with 152 additions and 97 deletions.
@@ -29,14 +29,17 @@
import java.util.concurrent.Future;

import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.message.StatusLine;
import org.apache.hc.core5.io.CloseMode;

/**
@@ -55,28 +58,29 @@ public static void main(final String[] args) throws Exception {
.build();
httpclient.start();

final String requestUri = "http://httpbin.org/basic-auth/user/passwd";
final SimpleHttpRequest httpget = SimpleHttpRequests.get(requestUri);
final SimpleHttpRequest request = SimpleRequestBuilder.get("http://httpbin.org/basic-auth/user/passwd")
.build();

System.out.println("Executing request " + requestUri);
System.out.println("Executing request " + request);
final Future<SimpleHttpResponse> future = httpclient.execute(
httpget,
SimpleRequestProducer.create(request),
SimpleResponseConsumer.create(),
new FutureCallback<SimpleHttpResponse>() {

@Override
public void completed(final SimpleHttpResponse response) {
System.out.println(requestUri + "->" + response.getCode());
System.out.println(request + "->" + new StatusLine(response));
System.out.println(response.getBody());
}

@Override
public void failed(final Exception ex) {
System.out.println(requestUri + "->" + ex);
System.out.println(request + "->" + ex);
}

@Override
public void cancelled() {
System.out.println(requestUri + " cancelled");
System.out.println(request + " cancelled");
}

});
@@ -30,14 +30,15 @@
import java.util.concurrent.TimeUnit;

import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.message.StatusLine;
import org.apache.hc.core5.io.CloseMode;
import org.apache.hc.core5.reactor.IOReactorConfig;
import org.apache.hc.core5.util.TimeValue;
@@ -63,28 +64,31 @@ public static void main(final String[] args) throws Exception {

client.start();

final HttpHost target = new HttpHost("httpbin.org");
final SimpleHttpRequest request = SimpleRequestBuilder.get()
.setHttpHost(new HttpHost("httpbin.org"))
.setPath("/")
.build();

final SimpleHttpRequest request = SimpleHttpRequests.get(target, "/");
System.out.println("Executing request " + request);
final Future<SimpleHttpResponse> future1 = client.execute(
SimpleRequestProducer.create(request),
SimpleResponseConsumer.create(),
new FutureCallback<SimpleHttpResponse>() {

@Override
public void completed(final SimpleHttpResponse response) {
System.out.println(request.getRequestUri() + "->" + response.getCode());
System.out.println(request + "->" + new StatusLine(response));
System.out.println(response.getBody());
}

@Override
public void failed(final Exception ex) {
System.out.println(request.getRequestUri() + "->" + ex);
System.out.println(request + "->" + ex);
}

@Override
public void cancelled() {
System.out.println(request.getRequestUri() + " cancelled");
System.out.println(request + " cancelled");
}

});
@@ -96,24 +100,23 @@ public void cancelled() {
// Previous connection should get evicted from the pool by now

final Future<SimpleHttpResponse> future2 = client.execute(
SimpleRequestProducer.create(request),
SimpleResponseConsumer.create(),
request,
new FutureCallback<SimpleHttpResponse>() {

@Override
public void completed(final SimpleHttpResponse response) {
System.out.println(request.getRequestUri() + "->" + response.getCode());
System.out.println(request + "->" + new StatusLine(response));
System.out.println(response.getBody());
}

@Override
public void failed(final Exception ex) {
System.out.println(request.getRequestUri() + "->" + ex);
System.out.println(request + "->" + ex);
}

@Override
public void cancelled() {
System.out.println(request.getRequestUri() + " cancelled");
System.out.println(request + " cancelled");
}

});
@@ -34,8 +34,8 @@
import javax.net.ssl.SSLSession;

import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
@@ -46,6 +46,7 @@
import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.message.StatusLine;
import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
import org.apache.hc.core5.io.CloseMode;
import org.apache.hc.core5.ssl.SSLContexts;
@@ -97,11 +98,15 @@ public TlsDetails create(final SSLEngine sslEngine) {

client.start();

final HttpHost target = new HttpHost("https", "httpbin.org", 443);
final String requestUri = "/";
final HttpHost target = new HttpHost("https", "httpbin.org");
final HttpClientContext clientContext = HttpClientContext.create();

final SimpleHttpRequest request = SimpleHttpRequests.get(target, requestUri);
final SimpleHttpRequest request = SimpleRequestBuilder.get()
.setHttpHost(target)
.setPath("/")
.build();

System.out.println("Executing request " + request);
final Future<SimpleHttpResponse> future = client.execute(
SimpleRequestProducer.create(request),
SimpleResponseConsumer.create(),
@@ -110,23 +115,23 @@ public TlsDetails create(final SSLEngine sslEngine) {

@Override
public void completed(final SimpleHttpResponse response) {
System.out.println(requestUri + "->" + response.getCode());
System.out.println(response.getBody());
System.out.println(request + "->" + new StatusLine(response));
final SSLSession sslSession = clientContext.getSSLSession();
if (sslSession != null) {
System.out.println("SSL protocol " + sslSession.getProtocol());
System.out.println("SSL cipher suite " + sslSession.getCipherSuite());
}
System.out.println(response.getBody());
}

@Override
public void failed(final Exception ex) {
System.out.println(requestUri + "->" + ex);
System.out.println(request + "->" + ex);
}

@Override
public void cancelled() {
System.out.println(requestUri + " cancelled");
System.out.println(request + " cancelled");
}

});
@@ -27,7 +27,6 @@
package org.apache.hc.client5.http.examples;

import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.CountDownLatch;
@@ -41,6 +40,8 @@
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.config.Http1Config;
import org.apache.hc.core5.http.message.BasicHttpRequest;
import org.apache.hc.core5.http.message.StatusLine;
import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
import org.apache.hc.core5.http.nio.CapacityChannel;
import org.apache.hc.core5.http.nio.DataStreamChannel;
@@ -50,6 +51,7 @@
import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.apache.hc.core5.http.support.BasicRequestBuilder;
import org.apache.hc.core5.http2.HttpVersionPolicy;
import org.apache.hc.core5.http2.config.H2Config;
import org.apache.hc.core5.io.CloseMode;
@@ -75,12 +77,13 @@ public static void main(final String[] args) throws Exception {

client.start();

final URI requestUri = new URI("http://httpbin.org/post");
final BasicRequestProducer requestProducer = new BasicRequestProducer(
"POST", requestUri, new BasicAsyncEntityProducer("stuff", ContentType.TEXT_PLAIN));
final BasicHttpRequest request = BasicRequestBuilder.post("http://httpbin.org/post").build();
final BasicRequestProducer requestProducer = new BasicRequestProducer(request,
new BasicAsyncEntityProducer("stuff", ContentType.TEXT_PLAIN));
final BasicResponseConsumer<String> responseConsumer = new BasicResponseConsumer<>(
new StringAsyncEntityConsumer());

System.out.println("Executing request " + request);
final CountDownLatch latch = new CountDownLatch(1);
client.execute(new AsyncClientExchangeHandler() {

@@ -93,12 +96,12 @@ public void releaseResources() {

@Override
public void cancel() {
System.out.println(requestUri + " cancelled");
System.out.println(request + " cancelled");
}

@Override
public void failed(final Exception cause) {
System.out.println(requestUri + "->" + cause);
System.out.println(request + "->" + cause);
}

@Override
@@ -120,15 +123,15 @@ public void produce(final DataStreamChannel channel) throws IOException {
public void consumeInformation(
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
System.out.println(requestUri + "->" + response.getCode());
System.out.println(request + "->" + new StatusLine(response));
}

@Override
public void consumeResponse(
final HttpResponse response,
final EntityDetails entityDetails,
final HttpContext context) throws HttpException, IOException {
System.out.println(requestUri + "->" + response.getCode());
System.out.println(request + "->" + new StatusLine(response));
responseConsumer.consumeResponse(response, entityDetails, context, null);
}

@@ -27,7 +27,6 @@
package org.apache.hc.client5.http.examples;

import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.CountDownLatch;
@@ -40,6 +39,8 @@
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.message.BasicHttpRequest;
import org.apache.hc.core5.http.message.StatusLine;
import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
import org.apache.hc.core5.http.nio.CapacityChannel;
import org.apache.hc.core5.http.nio.DataStreamChannel;
@@ -49,6 +50,7 @@
import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.apache.hc.core5.http.support.BasicRequestBuilder;
import org.apache.hc.core5.http2.HttpVersionPolicy;
import org.apache.hc.core5.http2.config.H2Config;
import org.apache.hc.core5.io.CloseMode;
@@ -71,12 +73,13 @@ public static void main(final String[] args) throws Exception {

client.start();

final URI requestUri = new URI("http://nghttp2.org/httpbin/post");
final BasicRequestProducer requestProducer = new BasicRequestProducer(
"POST", requestUri, new BasicAsyncEntityProducer("stuff", ContentType.TEXT_PLAIN));
final BasicHttpRequest request = BasicRequestBuilder.post("https://nghttp2.org/httpbin/post").build();
final BasicRequestProducer requestProducer = new BasicRequestProducer(request,
new BasicAsyncEntityProducer("stuff", ContentType.TEXT_PLAIN));
final BasicResponseConsumer<String> responseConsumer = new BasicResponseConsumer<>(
new StringAsyncEntityConsumer());

System.out.println("Executing request " + request);
final CountDownLatch latch = new CountDownLatch(1);
client.execute(new AsyncClientExchangeHandler() {

@@ -89,12 +92,12 @@ public void releaseResources() {

@Override
public void cancel() {
System.out.println(requestUri + " cancelled");
System.out.println(request + " cancelled");
}

@Override
public void failed(final Exception cause) {
System.out.println(requestUri + "->" + cause);
System.out.println(request + "->" + cause);
}

@Override
@@ -116,15 +119,15 @@ public void produce(final DataStreamChannel channel) throws IOException {
public void consumeInformation(
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
System.out.println(requestUri + "->" + response.getCode());
System.out.println(request + "->" + new StatusLine(response));
}

@Override
public void consumeResponse(
final HttpResponse response,
final EntityDetails entityDetails,
final HttpContext context) throws HttpException, IOException {
System.out.println(requestUri + "->" + response.getCode());
System.out.println(request + "->" + new StatusLine(response));
responseConsumer.consumeResponse(response, entityDetails, context, null);
}

0 comments on commit 567b53d

Please sign in to comment.