| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 | == Common configurationThe `RestClientBuilder` supports providing both a `RequestConfigCallback` andan `HttpClientConfigCallback` which allow for any customization that the ApacheAsync Http Client exposes. Those callbacks make it possible to modify somespecific behaviour of the client without overriding every other defaultconfiguration that the `RestClient` is initialized with. This sectiondescribes some common scenarios that require additional configuration for thelow-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]--------------------------------------------------RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200))        .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {            @Override            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {                return requestConfigBuilder.setConnectTimeout(5000)                        .setSocketTimeout(60000);            }        })        .setMaxRetryTimeoutMillis(60000)        .build();--------------------------------------------------=== 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]--------------------------------------------------RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200))        .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {            @Override            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {                return httpClientBuilder.setDefaultIOReactorConfig(                        IOReactorConfig.custom().setIoThreadCount(1).build());            }        })        .build();--------------------------------------------------=== 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]--------------------------------------------------final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();credentialsProvider.setCredentials(AuthScope.ANY,        new UsernamePasswordCredentials("user", "password"));RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200))        .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {            @Override            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {                return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);            }        })        .build();--------------------------------------------------=== 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]--------------------------------------------------KeyStore keystore = KeyStore.getInstance("jks");try (InputStream is = Files.newInputStream(keyStorePath)) {    keystore.load(is, keyStorePass.toCharArray());}RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200))        .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {            @Override            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {                return httpClientBuilder.setSSLContext(sslcontext);            }        })        .build();--------------------------------------------------=== OthersFor any other required configuration needed, the Apache HttpAsyncClient docsshould be consulted: https://hc.apache.org/httpcomponents-asyncclient-4.1.x/ .
 |