Class JdkHttpClientBuilder
HttpClient type using the JDK HttpClient APIs,
first introduced as preview in JDK 9, but made generally available from JDK 11 onwards.-
Constructor Summary
ConstructorsConstructorDescriptionCreates JdkHttpClientBuilder.JdkHttpClientBuilder(java.net.http.HttpClient.Builder httpClientBuilder) Creates JdkHttpClientBuilder from the builder of an existingHttpClient.Builder. -
Method Summary
Modifier and TypeMethodDescriptionbuild()Build a HttpClient with current configurations.configuration(Configuration configuration) Sets the configuration store that is used during construction of the HTTP client.connectionTimeout(Duration connectionTimeout) Sets the connection timeout.Sets the executor to be used for asynchronous and dependent tasks.proxy(ProxyOptions proxyOptions) Sets the proxy.readTimeout(Duration readTimeout) Sets the read timeout duration used when reading the server response.responseTimeout(Duration responseTimeout) Sets the response timeout duration used when waiting for a server to reply.writeTimeout(Duration writeTimeout) Sets the writing timeout for a request to be sent.
-
Constructor Details
-
JdkHttpClientBuilder
public JdkHttpClientBuilder()Creates JdkHttpClientBuilder. -
JdkHttpClientBuilder
public JdkHttpClientBuilder(java.net.http.HttpClient.Builder httpClientBuilder) Creates JdkHttpClientBuilder from the builder of an existingHttpClient.Builder.- Parameters:
httpClientBuilder- the HttpClient builder to use- Throws:
NullPointerException- ifhttpClientBuilderis null
-
-
Method Details
-
executor
Sets the executor to be used for asynchronous and dependent tasks. This cannot be null.If this method is not invoked prior to building, a default executor is created for each newly built
HttpClient.- Parameters:
executor- the executor to be used for asynchronous and dependent tasks- Returns:
- the updated JdkHttpClientBuilder object
- Throws:
NullPointerException- ifexecutoris null
-
connectionTimeout
Sets the connection timeout.Code Samples
HttpClient client = new JdkHttpClientBuilder() .connectionTimeout(Duration.ofSeconds(250)) // connection timeout of 250 seconds .build();The default connection timeout is 10 seconds.- Parameters:
connectionTimeout- the connection timeout- Returns:
- the updated JdkHttpClientBuilder object
-
writeTimeout
Sets the writing timeout for a request to be sent.The writing timeout does not apply to the entire request but to the request being sent over the wire. For example a request body which emits
108KBbuffers will trigger10write operations, the last write tracker will update when each operation completes and the outbound buffer will be periodically checked to determine if it is still draining.If
writeTimeoutis null eitherConfiguration.PROPERTY_AZURE_REQUEST_WRITE_TIMEOUTor a 60-second timeout will be used, if it is aDurationless than or equal to zero then no write timeout will be applied. When applying the timeout the greatest of one millisecond and the value ofwriteTimeoutwill be used.- Parameters:
writeTimeout- Write operation timeout duration.- Returns:
- The updated
JdkHttpClientBuilderobject.
-
responseTimeout
Sets the response timeout duration used when waiting for a server to reply.The response timeout begins once the request write completes and finishes once the first response read is triggered when the server response is received.
If
responseTimeoutis null eitherConfiguration.PROPERTY_AZURE_REQUEST_RESPONSE_TIMEOUTor a 60-second timeout will be used, if it is aDurationless than or equal to zero then no timeout will be applied to the response. When applying the timeout the greatest of one millisecond and the value ofresponseTimeoutwill be used.- Parameters:
responseTimeout- Response timeout duration.- Returns:
- The updated
JdkHttpClientBuilderobject.
-
readTimeout
Sets the read timeout duration used when reading the server response.The read timeout begins once the first response read is triggered after the server response is received. This timeout triggers periodically but won't fire its operation if another read operation has completed between when the timeout is triggered and completes.
If
readTimeoutis null orConfiguration.PROPERTY_AZURE_REQUEST_READ_TIMEOUTor a 60-second timeout will be used, if it is aDurationless than or equal to zero then no timeout period will be applied to response read. When applying the timeout the greatest of one millisecond and the value ofreadTimeoutwill be used.- Parameters:
readTimeout- Read timeout duration.- Returns:
- The updated
JdkHttpClientBuilderobject.
-
proxy
Sets the proxy.Code Samples
final String proxyHost = "<proxy-host>"; // e.g. localhost final int proxyPort = 9999; // Proxy port ProxyOptions proxyOptions = new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); HttpClient client = new JdkHttpClientBuilder() .proxy(proxyOptions) .build();- Parameters:
proxyOptions- The proxy configuration to use.- Returns:
- the updated JdkHttpClientBuilder object
-
configuration
Sets the configuration store that is used during construction of the HTTP client.The default configuration store is a clone of the
global configuration store, useConfiguration.NONEto bypass using configuration settings during construction.- Parameters:
configuration- The configuration store used to- Returns:
- The updated JdkHttpClientBuilder object.
-
build
Build a HttpClient with current configurations.- Returns:
- a
HttpClient.
-