configuration.asciidoc 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. == Common configuration
  2. As explained in <<java-rest-low-usage-initialization>>, the `RestClientBuilder`
  3. supports providing both a `RequestConfigCallback` and an `HttpClientConfigCallback`
  4. which allow for any customization that the Apache Async Http Client exposes.
  5. Those callbacks make it possible to modify some specific behaviour of the client
  6. without overriding every other default configuration that the `RestClient`
  7. is initialized with. This section describes some common scenarios that require
  8. additional configuration for the 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",subs="attributes,callouts,macros"]
  20. --------------------------------------------------
  21. include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-timeouts]
  22. --------------------------------------------------
  23. === Number of threads
  24. The Apache Http Async Client starts by default one dispatcher thread, and a
  25. number of worker threads used by the connection manager, as many as the number
  26. of locally detected processors (depending on what
  27. `Runtime.getRuntime().availableProcessors()` returns). The number of threads
  28. can be modified as follows:
  29. ["source","java",subs="attributes,callouts,macros"]
  30. --------------------------------------------------
  31. include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-threads]
  32. --------------------------------------------------
  33. === Basic authentication
  34. Configuring basic authentication can be done by providing an
  35. `HttpClientConfigCallback` while building the `RestClient` through its builder.
  36. The interface has one method that receives an instance of
  37. https://hc.apache.org/httpcomponents-asyncclient-dev/httpasyncclient/apidocs/org/apache/http/impl/nio/client/HttpAsyncClientBuilder.html[`org.apache.http.impl.nio.client.HttpAsyncClientBuilder`]
  38. as an argument and has the same return type. The http client builder can be
  39. modified and then returned. In the following example we set a default
  40. credentials provider that requires basic authentication.
  41. ["source","java",subs="attributes,callouts,macros"]
  42. --------------------------------------------------
  43. include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-basic-auth]
  44. --------------------------------------------------
  45. Preemptive Authentication can be disabled, which means that every request will be sent without
  46. authorization headers to see if it is accepted and, upon receiving a HTTP 401 response, it will
  47. resend the exact same request with the basic authentication header. If you wish to do this, then
  48. you can do so by disabling it via the `HttpAsyncClientBuilder`:
  49. ["source","java",subs="attributes,callouts,macros"]
  50. --------------------------------------------------
  51. include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-disable-preemptive-auth]
  52. --------------------------------------------------
  53. <1> Disable preemptive authentication
  54. === Encrypted communication
  55. Encrypted communication can also be configured through the
  56. `HttpClientConfigCallback`. The
  57. https://hc.apache.org/httpcomponents-asyncclient-dev/httpasyncclient/apidocs/org/apache/http/impl/nio/client/HttpAsyncClientBuilder.html[`org.apache.http.impl.nio.client.HttpAsyncClientBuilder`]
  58. received as an argument exposes multiple methods to configure encrypted
  59. communication: `setSSLContext`, `setSSLSessionStrategy` and
  60. `setConnectionManager`, in order of precedence from the least important.
  61. The following is an example:
  62. ["source","java",subs="attributes,callouts,macros"]
  63. --------------------------------------------------
  64. include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-encrypted-communication]
  65. --------------------------------------------------
  66. === Others
  67. For any other required configuration needed, the Apache HttpAsyncClient docs
  68. should be consulted: https://hc.apache.org/httpcomponents-asyncclient-4.1.x/ .