In the example below I create one Java 11 httpClient and then create multiple concurrent HttpRequests.
- Is this bad practice?
- Should each HttpRequest have its own HttpClient?
- Is there an upper limit on the number of HttpRequests a HttpClient can have?
Code
private static void httpClientExample(){
HttpClient httpClient = HttpClient.newHttpClient();
System.out.println("TP1");
var task1 = httpClient.sendAsync(HttpRequest.newBuilder()
.uri(URI.create("https://www.bing.com/"))
.build(), HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::uri).thenAccept(System.out::println);
var task2 = httpClient.sendAsync(HttpRequest.newBuilder()
.uri(URI.create("https://openjdk.java.net/"))
.build(), HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::uri).thenAccept(System.out::println);
var task3 = httpClient.sendAsync(HttpRequest.newBuilder()
.uri(URI.create("https://www.google.co.uk/"))
.build(), HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::uri).thenAccept(System.out::println);
System.out.println("Requests Sent");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Main Thread Completed");
}
This is not explicitly documented in the API docs of
HttpClient
. But it would be expected that an HttpClient is designed to handle multiple requests. This is in a sense implied on Introduction to the Java HTTP Client:Now, your question is likely about managing concurrency on your clients. Rather than with using the same instance of
HttpClient
, this has much to do with the executor service it uses, which is something you can customize (see here):This way, you can manage the thread pool used by the client to run asynchronous requests.
In other words:
No
No
You will have to test the optimal concurrency settings for your application and then use an executor service configured accordingly.