Sub topic 2
Building a reusable `HttpClient`
This chapter assumes Java 11 or later, basic HTTP concepts, and familiarity with checked exceptions. Java’s HttpClient is immutable and thread-safe after construction. Create one shared instance and reuse it for many requests.
Choose client-wide settings
A client holds settings that usually apply to every request:
| Setting | Purpose |
|---|---|
| Connect timeout | Limits how long connection establishment may take |
| Redirect policy | Controls whether redirects are followed |
| HTTP version | Prefers HTTP/2 or HTTP/1.1 |
Build the client near your application’s startup code. Do not create a new client for every request. Reusing one client allows the implementation to reuse connections and manage resources efficiently.
import java.net.http.HttpClient;
import java.time.Duration;
public class ClientFactory {
private static final HttpClient CLIENT = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(5))
.followRedirects(HttpClient.Redirect.NORMAL)
.version(HttpClient.Version.HTTP_2)
.build();
public static HttpClient client() {
return CLIENT;
}
public static void main(String[] args) {
HttpClient client = ClientFactory.client();
System.out.println(client.version());
}
}HTTP_2 expresses a preference. If the server or connection cannot use HTTP/2, Java can negotiate HTTP/1.1 instead. Use HTTP_1_1 when a system specifically requires that protocol.
Redirect.NORMAL follows ordinary redirects, including redirects from one HTTPS URL to another HTTPS URL. It does not automatically follow every possible protocol downgrade. The other policies are NEVER and ALWAYS.
Understand the lifetime of the client
The client does not expose a general close() method. Its resources are managed by the implementation and its executor. Keep a shared client for the lifetime of the application. A client used by several threads is safe without external synchronization.
The request still contains operation-specific details such as the URI, headers, HTTP method, and request timeout:
flowchart TD
A[Application startup] --> B[Build one HttpClient]
B --> C[Create an HttpRequest]
C --> D[Send request]
D --> E[Reuse client for another request]caption: The normal lifecycle of a reusable HttpClient alt: An application builds one client at startup, creates requests, sends them, and reuses the client for later requests.
Separate shared and request-specific configuration
Put stable transport choices on the client. Put endpoint and operation choices on each request.
For example, a service may share one client configured for HTTP/2 and redirects, while each request supplies its own URI, authorization header, method, body, and timeout. This separation prevents one request from accidentally changing the behavior of other requests.
Exercise
Why should an application usually reuse one HttpClient instead of building one for every request?
Show answer
HttpClient is thread-safe, and reuse allows connection management and other resources to be shared across requests.