| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 | == Common configurationAs explained in <<java-rest-low-usage-initialization>>, the `RestClientBuilder`supports providing both a `RequestConfigCallback` and an `HttpClientConfigCallback`which allow for any customization that the Apache Async Http Client exposes.Those callbacks make it possible to modify some specific behaviour of the clientwithout overriding every other default configuration that the `RestClient`is initialized with. This section describes some common scenarios that requireadditional configuration for the low-level Java REST Client.=== TimeoutsConfiguring requests timeouts can be done by providing an instance of`RequestConfigCallback` while building the `RestClient` through its builder.The interface has one method that receives an instance ofhttps://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/config/RequestConfig.Builder.html[`org.apache.http.client.config.RequestConfig.Builder`] as an argument and has the same return type. The request config builder canbe modified and then returned. In the following example we increase theconnect timeout (defaults to 1 second) and the socket timeout (defaults to 30seconds). Also we adjust the max retry timeout accordingly (defaults to 30seconds too).["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-timeouts]--------------------------------------------------=== Number of threadsThe Apache Http Async Client starts by default one dispatcher thread, and anumber of worker threads used by the connection manager, as many as the numberof locally detected processors (depending on what`Runtime.getRuntime().availableProcessors()` returns). The number of threadscan be modified as follows:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-threads]--------------------------------------------------=== Basic authenticationConfiguring basic authentication can be done by providing an`HttpClientConfigCallback` while building the `RestClient` through its builder.The interface has one method that receives an instance ofhttps://hc.apache.org/httpcomponents-asyncclient-dev/httpasyncclient/apidocs/org/apache/http/impl/nio/client/HttpAsyncClientBuilder.html[`org.apache.http.impl.nio.client.HttpAsyncClientBuilder`] as an argument and has the same return type. The http client builder can bemodified and then returned. In the following example we set a defaultcredentials provider that requires basic authentication.["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-basic-auth]--------------------------------------------------Preemptive Authentication can be disabled, which means that every request will be sent withoutauthorization headers to see if it is accepted and, upon receiving a HTTP 401 response, it willresend the exact same request with the basic authentication header. If you wish to do this, thenyou can do so by disabling it via the `HttpAsyncClientBuilder`:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-disable-preemptive-auth]--------------------------------------------------<1> Disable preemptive authentication=== Encrypted communicationEncrypted communication can also be configured through the`HttpClientConfigCallback`. Thehttps://hc.apache.org/httpcomponents-asyncclient-dev/httpasyncclient/apidocs/org/apache/http/impl/nio/client/HttpAsyncClientBuilder.html[`org.apache.http.impl.nio.client.HttpAsyncClientBuilder`] received as an argument exposes multiple methods to configure encrypted communication: `setSSLContext`, `setSSLSessionStrategy` and `setConnectionManager`, in order of precedence from the least important. The following is an example:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-encrypted-communication]--------------------------------------------------If no explicit configuration is provided, the http://docs.oracle.com/javase/7/docs/technotes/guides/security/jsse/JSSERefGuide.html#CustomizingStores[system default configuration]will be used.=== OthersFor any other required configuration needed, the Apache HttpAsyncClient docsshould be consulted: https://hc.apache.org/httpcomponents-asyncclient-4.1.x/ .
 |