Class OkHttpAsyncHttpClientBuilder
HttpClient backed by OkHttp.
The client built from this builder can support sending requests synchronously and asynchronously. Use
HttpClient.sendSync(HttpRequest, Context) to send the provided request synchronously with contextual
information.
Building a new HttpClient instance
HttpClient client = new OkHttpAsyncHttpClientBuilder()
.build();
Building a new HttpClient instance using http proxy.
Configuring the OkHttp client with a proxy is relevant when your application needs to communicate with Azure services through a proxy server.
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 OkHttpAsyncHttpClientBuilder()
.proxy(proxyOptions)
.build();
Building a new HttpClient instance with connection timeout.
Setting a reasonable connection timeout is particularly important in scenarios where network conditions might be unpredictable or where the server may not be responsive.
final Duration connectionTimeout = Duration.ofSeconds(250); // connection timeout of 250 seconds
HttpClient client = new OkHttpAsyncHttpClientBuilder()
.connectionTimeout(connectionTimeout)
.build();
Building a new HttpClient instance with HTTP/2 Support.
HttpClient client = new OkHttpAsyncHttpClientBuilder()
.build();
It is also possible to create a OkHttp HttpClient that only supports HTTP/2.
// Constructs an HttpClient that only supports HTTP/2.
HttpClient client = new OkHttpAsyncHttpClientBuilder(new OkHttpClient.Builder()
.protocols(Collections.singletonList(Protocol.H2_PRIOR_KNOWLEDGE))
.build())
.build();
- See Also:
-
HttpClientOkHttpAsyncHttpClient
-
Constructor Summary
ConstructorsConstructorDescriptionCreates OkHttpAsyncHttpClientBuilder.OkHttpAsyncHttpClientBuilder(okhttp3.OkHttpClient okHttpClient) Creates OkHttpAsyncHttpClientBuilder from the builder of an existing OkHttpClient. -
Method Summary
Modifier and TypeMethodDescriptionaddNetworkInterceptor(okhttp3.Interceptor networkInterceptor) Add a network layer interceptor to Http request pipeline.build()Creates a new OkHttp-backedHttpClientinstance on every call, using the configuration set in the builder at the time of the build method call.callTimeout(Duration callTimeout) Sets the default timeout for complete calls.configuration(Configuration configuration) Sets the configuration store that is used during construction of the HTTP client.connectionPool(okhttp3.ConnectionPool connectionPool) Sets the Http connection pool.connectionTimeout(Duration connectionTimeout) Sets the connection timeout for a request to be sent.dispatcher(okhttp3.Dispatcher dispatcher) Sets the dispatcher that also composes the thread pool for executing HTTP requests.followRedirects(boolean followRedirects) Sets the followRedirect flag on the underlying OkHttp-backedHttpClient.networkInterceptors(List<okhttp3.Interceptor> networkInterceptors) Add network layer interceptors to Http request pipeline.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
-
OkHttpAsyncHttpClientBuilder
public OkHttpAsyncHttpClientBuilder()Creates OkHttpAsyncHttpClientBuilder. -
OkHttpAsyncHttpClientBuilder
public OkHttpAsyncHttpClientBuilder(okhttp3.OkHttpClient okHttpClient) Creates OkHttpAsyncHttpClientBuilder from the builder of an existing OkHttpClient.- Parameters:
okHttpClient- the httpclient
-
-
Method Details
-
addNetworkInterceptor
Add a network layer interceptor to Http request pipeline.- Parameters:
networkInterceptor- the interceptor to add- Returns:
- the updated OkHttpAsyncHttpClientBuilder object
-
networkInterceptors
public OkHttpAsyncHttpClientBuilder networkInterceptors(List<okhttp3.Interceptor> networkInterceptors) Add network layer interceptors to Http request pipeline.This replaces all previously-set interceptors.
- Parameters:
networkInterceptors- The interceptors to add.- Returns:
- The updated OkHttpAsyncHttpClientBuilder object.
-
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 OkHttpAsyncHttpClientBuilder object.
- See Also:
-
OkHttpClient.Builder.readTimeout(Duration)
-
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.Given OkHttp doesn't have an equivalent timeout for just responses, this is handled manually.
- Parameters:
responseTimeout- Response timeout duration.- Returns:
- The updated OkHttpAsyncHttpClientBuilder 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 OkHttpAsyncHttpClientBuilder object.
- See Also:
-
OkHttpClient.Builder.writeTimeout(Duration)
-
connectionTimeout
Sets the connection timeout for a request to be sent.The connection timeout begins once the request attempts to connect to the remote host and finishes once the connection is resolved.
If
connectTimeoutis null eitherConfiguration.PROPERTY_AZURE_REQUEST_CONNECT_TIMEOUTor a 10-second timeout will be used, if it is aDurationless than or equal to zero then no timeout will be applied. When applying the timeout the greatest of one millisecond and the value ofconnectTimeoutwill be used.By default, the connection timeout is 10 seconds.
- Parameters:
connectionTimeout- Connect timeout duration.- Returns:
- The updated OkHttpAsyncHttpClientBuilder object.
- See Also:
-
OkHttpClient.Builder.connectTimeout(Duration)
-
callTimeout
Sets the default timeout for complete calls.The call timeout spans the entire call: resolving DNS, connecting, writing the request body, server processing, and reading the response body.
Null or
Duration.ZEROmeans no call timeout, otherwise values must be between 1 andInteger.MAX_VALUEwhen converted to milliseconds.By default, call timeout is not enabled.
- Parameters:
callTimeout- Call timeout duration.- Returns:
- The updated OkHttpAsyncHttpClientBuilder object.
- See Also:
-
OkHttpClient.Builder.callTimeout(Duration)
-
connectionPool
Sets the Http connection pool.- Parameters:
connectionPool- The OkHttp connection pool to use.- Returns:
- The updated OkHttpAsyncHttpClientBuilder object.
- See Also:
-
OkHttpClient.Builder.connectionPool(ConnectionPool)
-
dispatcher
Sets the dispatcher that also composes the thread pool for executing HTTP requests.- Parameters:
dispatcher- The dispatcher to use.- Returns:
- The updated OkHttpAsyncHttpClientBuilder object.
- See Also:
-
OkHttpClient.Builder.dispatcher(Dispatcher)
-
proxy
Sets the proxy.- Parameters:
proxyOptions- The proxy configuration to use.- Returns:
- The updated OkHttpAsyncHttpClientBuilder 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.- Returns:
- The updated OkHttpAsyncHttpClientBuilder object.
-
followRedirects
Sets the followRedirect flag on the underlying OkHttp-backed
HttpClient.If this is set to 'true' redirects will be followed automatically, and if your HTTP pipeline is configured with a redirect policy it will not be called.
- Parameters:
followRedirects- The followRedirects value to use.- Returns:
- The updated OkHttpAsyncHttpClientBuilder object.
-
build
Creates a new OkHttp-backedHttpClientinstance on every call, using the configuration set in the builder at the time of the build method call.- Returns:
- A new OkHttp-backed
HttpClientinstance.
-