configuration.asciidoc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. === Common configuration
  2. The `RestClientBuilder` supports providing both a `RequestConfigCallback` and
  3. an `HttpClientConfigCallback` which allow for any customization that the Apache
  4. Async Http Client exposes. Those callbacks make it possible to modify some
  5. specific behaviour of the client without overriding every other default
  6. configuration that the `RestClient` is initialized with. This section
  7. describes some common scenarios that require additional configuration for the
  8. low-level Java REST Client.
  9. ==== Timeouts
  10. Configuring requests timeouts can be done by providing an instance of
  11. `RequestConfigCallback` while building the `RestClient` through its builder.
  12. The interface has one method that receives an instance of
  13. https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/config/RequestConfig.Builder.html[`org.apache.http.client.config.RequestConfig.Builder`]
  14. as an argument and has the same return type. The request config builder can
  15. be modified and then returned. In the following example we increase the
  16. connect timeout (defaults to 1 second) and the socket timeout (defaults to 30
  17. seconds). Also we adjust the max retry timeout accordingly (defaults to 30
  18. seconds too).
  19. [source,java]
  20. --------------------------------------------------
  21. RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200))
  22. .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
  23. @Override
  24. public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
  25. return requestConfigBuilder.setConnectTimeout(5000)
  26. .setSocketTimeout(60000);
  27. }
  28. })
  29. .setMaxRetryTimeoutMillis(60000)
  30. .build();
  31. --------------------------------------------------
  32. ==== Number of threads
  33. The Apache Http Async Client starts by default one dispatcher thread, and a
  34. number of worker threads used by the connection manager, as many as the number
  35. of locally detected processors (depending on what
  36. `Runtime.getRuntime().availableProcessors()` returns). The number of threads
  37. can be modified as follows:
  38. [source,java]
  39. --------------------------------------------------
  40. RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200))
  41. .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
  42. @Override
  43. public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
  44. return httpClientBuilder.setDefaultIOReactorConfig(
  45. IOReactorConfig.custom().setIoThreadCount(1).build());
  46. }
  47. })
  48. .build();
  49. --------------------------------------------------
  50. ==== Basic authentication
  51. Configuring basic authentication can be done by providing an
  52. `HttpClientConfigCallback` while building the `RestClient` through its builder.
  53. The interface has one method that receives an instance of
  54. https://hc.apache.org/httpcomponents-asyncclient-dev/httpasyncclient/apidocs/org/apache/http/impl/nio/client/HttpAsyncClientBuilder.html[`org.apache.http.impl.nio.client.HttpAsyncClientBuilder`]
  55. as an argument and has the same return type. The http client builder can be
  56. modified and then returned. In the following example we set a default
  57. credentials provider that requires basic authentication.
  58. [source,java]
  59. --------------------------------------------------
  60. final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  61. credentialsProvider.setCredentials(AuthScope.ANY,
  62. new UsernamePasswordCredentials("user", "password"));
  63. RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200))
  64. .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
  65. @Override
  66. public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
  67. return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
  68. }
  69. })
  70. .build();
  71. --------------------------------------------------
  72. You can disable Preemptive Authentication, which means that every request will be sent without
  73. authorization headers to see if it is accepted and, upon receiving a HTTP 401 response, it will
  74. resend the exact same request with the basic authentication header. If you wish to do this, then
  75. you can do so by disabling it via the `HttpAsyncClientBuilder`:
  76. [source,java]
  77. --------------------------------------------------
  78. final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  79. credentialsProvider.setCredentials(AuthScope.ANY,
  80. new UsernamePasswordCredentials("user", "password"));
  81. RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200))
  82. .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
  83. @Override
  84. public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
  85. // disable preemptive authentication
  86. httpClientBuilder.disableAuthCaching();
  87. return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
  88. }
  89. })
  90. .build();
  91. --------------------------------------------------
  92. ==== Encrypted communication
  93. Encrypted communication can also be configured through the
  94. `HttpClientConfigCallback`. The
  95. https://hc.apache.org/httpcomponents-asyncclient-dev/httpasyncclient/apidocs/org/apache/http/impl/nio/client/HttpAsyncClientBuilder.html[`org.apache.http.impl.nio.client.HttpAsyncClientBuilder`]
  96. received as an argument exposes multiple methods to configure encrypted
  97. communication: `setSSLContext`, `setSSLSessionStrategy` and
  98. `setConnectionManager`, in order of precedence from the least important.
  99. The following is an example:
  100. [source,java]
  101. --------------------------------------------------
  102. KeyStore keystore = KeyStore.getInstance("jks");
  103. try (InputStream is = Files.newInputStream(keyStorePath)) {
  104. keystore.load(is, keyStorePass.toCharArray());
  105. }
  106. RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200))
  107. .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
  108. @Override
  109. public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
  110. return httpClientBuilder.setSSLContext(sslcontext);
  111. }
  112. })
  113. .build();
  114. --------------------------------------------------
  115. ==== Others
  116. For any other required configuration needed, the Apache HttpAsyncClient docs
  117. should be consulted: https://hc.apache.org/httpcomponents-asyncclient-4.1.x/ .