|
@@ -71,11 +71,9 @@ client will communicate with, provided as instances of
|
|
|
https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpHost.html[HttpHost]
|
|
|
as follows:
|
|
|
|
|
|
-[source,java]
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
--------------------------------------------------
|
|
|
-RestClient restClient = RestClient.builder(
|
|
|
- new HttpHost("localhost", 9200, "http"),
|
|
|
- new HttpHost("localhost", 9201, "http")).build();
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-init]
|
|
|
--------------------------------------------------
|
|
|
|
|
|
The `RestClient` class is thread-safe and ideally has the same lifecycle as
|
|
@@ -83,29 +81,52 @@ the application that uses it. It is important that it gets closed when no
|
|
|
longer needed so that all the resources used by it get properly released,
|
|
|
as well as the underlying http client instance and its threads:
|
|
|
|
|
|
-[source,java]
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
--------------------------------------------------
|
|
|
-restClient.close();
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-close]
|
|
|
--------------------------------------------------
|
|
|
|
|
|
`RestClientBuilder` also allows to optionally set the following configuration
|
|
|
parameters while building the `RestClient` instance:
|
|
|
|
|
|
-`setDefaultHeaders`:: default headers that need to be sent with each request,
|
|
|
-to prevent having to specify them with each single request
|
|
|
-`setMaxRetryTimeoutMillis`:: the timeout that should be honoured in case
|
|
|
-multiple attempts are made for the same request. The default value is 30
|
|
|
-seconds, same as the default socket timeout. In case the socket timeout is
|
|
|
-customized, the maximum retry timeout should be adjusted accordingly
|
|
|
-`setFailureListener`:: a listener that gets notified every time a node
|
|
|
-fails, in case actions need to be taken. Used internally when sniffing on
|
|
|
-failure is enabled
|
|
|
-`setRequestConfigCallback`:: callback that allows to modify the default
|
|
|
-request configuration (e.g. request timeouts, authentication, or anything that
|
|
|
-the https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/config/RequestConfig.Builder.html[`org.apache.http.client.config.RequestConfig.Builder`]
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
+--------------------------------------------------
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-init-default-headers]
|
|
|
+--------------------------------------------------
|
|
|
+<1> Set the default headers that need to be sent with each request, to
|
|
|
+prevent having to specify them with each single request
|
|
|
+
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
+--------------------------------------------------
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-init-max-retry-timeout]
|
|
|
+--------------------------------------------------
|
|
|
+<1> Set the timeout that should be honoured in case multiple attempts are made
|
|
|
+for the same request. The default value is 30 seconds, same as the default
|
|
|
+socket timeout. In case the socket timeout is customized, the maximum retry
|
|
|
+timeout should be adjusted accordingly
|
|
|
+
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
+--------------------------------------------------
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-init-failure-listener]
|
|
|
+--------------------------------------------------
|
|
|
+<1> Set a listener that gets notified every time a node fails, in case actions
|
|
|
+need to be taken. Used internally when sniffing on failure is enabled.
|
|
|
+
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
+--------------------------------------------------
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-init-request-config-callback]
|
|
|
+--------------------------------------------------
|
|
|
+<1> Set a callback that allows to modify the default request configuration
|
|
|
+(e.g. request timeouts, authentication, or anything that the
|
|
|
+https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/config/RequestConfig.Builder.html[`org.apache.http.client.config.RequestConfig.Builder`]
|
|
|
allows to set)
|
|
|
-`setHttpClientConfigCallback`:: callback that allows to modify the http client
|
|
|
- configuration (e.g. encrypted communication over ssl, or anything that the
|
|
|
+
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
+--------------------------------------------------
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-init-client-config-callback]
|
|
|
+--------------------------------------------------
|
|
|
+<1> Set a callback that allows to modify the http client configuration
|
|
|
+(e.g. encrypted communication over ssl, or anything that the
|
|
|
http://hc.apache.org/httpcomponents-asyncclient-dev/httpasyncclient/apidocs/org/apache/http/impl/nio/client/HttpAsyncClientBuilder.html[`org.apache.http.impl.nio.client.HttpAsyncClientBuilder`]
|
|
|
allows to set)
|
|
|
|
|
@@ -115,101 +136,135 @@ http://hc.apache.org/httpcomponents-asyncclient-dev/httpasyncclient/apidocs/org/
|
|
|
|
|
|
Once the `RestClient` has been created, requests can be sent by calling one of
|
|
|
the available `performRequest` or `performRequestAsync` method variants.
|
|
|
-The `performRequest` methods are synchronous and they return the `Response`
|
|
|
-directly, meaning that the client will block and wait for a response to be returned.
|
|
|
-The `performRequestAsync` variants, which return `void` and accept an extra
|
|
|
-`ResponseListener` as an argument, are executed asynchronously. The provided
|
|
|
-listener will be notified upon completion or failure.
|
|
|
-
|
|
|
-[source,java]
|
|
|
---------------------------------------------------
|
|
|
-// Synchronous variants
|
|
|
-Response performRequest(String method, String endpoint,
|
|
|
- Header... headers)
|
|
|
- throws IOException;
|
|
|
-
|
|
|
-Response performRequest(String method, String endpoint,
|
|
|
- Map<String, String> params, Header... headers)
|
|
|
- throws IOException;
|
|
|
-
|
|
|
-Response performRequest(String method, String endpoint,
|
|
|
- Map<String, String> params,
|
|
|
- HttpEntity entity,
|
|
|
- Header... headers)
|
|
|
- throws IOException;
|
|
|
-
|
|
|
-Response performRequest(String method, String endpoint,
|
|
|
- Map<String, String> params,
|
|
|
- HttpEntity entity,
|
|
|
- HttpAsyncResponseConsumerFactory responseConsumerFactory,
|
|
|
- Header... headers)
|
|
|
- throws IOException;
|
|
|
-
|
|
|
-// Asynchronous variants
|
|
|
-void performRequestAsync(String method, String endpoint,
|
|
|
- ResponseListener responseListener,
|
|
|
- Header... headers);
|
|
|
-
|
|
|
-void performRequestAsync(String method, String endpoint,
|
|
|
- Map<String, String> params,
|
|
|
- ResponseListener responseListener,
|
|
|
- Header... headers);
|
|
|
-
|
|
|
-void performRequestAsync(String method, String endpoint,
|
|
|
- Map<String, String> params,
|
|
|
- HttpEntity entity,
|
|
|
- ResponseListener responseListener,
|
|
|
- Header... headers);
|
|
|
-
|
|
|
-void performRequestAsync(String method, String endpoint,
|
|
|
- Map<String, String> params,
|
|
|
- HttpEntity entity,
|
|
|
- HttpAsyncResponseConsumerFactory responseConsumerFactory,
|
|
|
- ResponseListener responseListener,
|
|
|
- Header... headers);
|
|
|
---------------------------------------------------
|
|
|
-
|
|
|
-[[java-rest-low-usage-requests-arguments]]
|
|
|
-==== Request Arguments
|
|
|
-
|
|
|
-The following are the arguments accepted by the different methods:
|
|
|
-
|
|
|
-`method`:: the http method or verb
|
|
|
-`endpoint`:: the request path, which identifies the Elasticsearch API to
|
|
|
-call (e.g. `/_cluster/health`)
|
|
|
-`params`:: the optional parameters to be sent as querystring parameters
|
|
|
-`entity`:: the optional request body enclosed in an
|
|
|
-`org.apache.http.HttpEntity` object
|
|
|
-`responseConsumerFactory`:: the optional factory that is used to create an
|
|
|
-http://hc.apache.org/httpcomponents-core-ga/httpcore-nio/apidocs/org/apache/http/nio/protocol/HttpAsyncResponseConsumer.html[`org.apache.http.nio.protocol.HttpAsyncResponseConsumer`]
|
|
|
- callback instance per request attempt. Controls how the response body gets
|
|
|
- streamed from a non-blocking HTTP connection on the client side. When not
|
|
|
- provided, the default implementation is used which buffers the whole response
|
|
|
- body in heap memory, up to 100 MB
|
|
|
-`responseListener`:: the listener to be notified upon asynchronous
|
|
|
-request success or failure
|
|
|
-`headers`:: optional request headers
|
|
|
+The `performRequest` methods are synchronous and return the `Response` directly,
|
|
|
+meaning that the client will block and wait for a response to be returned.
|
|
|
+The `performRequestAsync` variants return `void` and accept an extra
|
|
|
+`ResponseListener` as an argument instead, meaning that they are executed
|
|
|
+asynchronously. The provided listener will be notified upon request completion
|
|
|
+or failure.
|
|
|
+
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
+--------------------------------------------------
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-verb-endpoint]
|
|
|
+--------------------------------------------------
|
|
|
+<1> Send a request by providing only the verb and the endpoint, minimum set
|
|
|
+of required arguments
|
|
|
+
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
+--------------------------------------------------
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-verb-endpoint-params]
|
|
|
+--------------------------------------------------
|
|
|
+<1> Send a request by providing the verb, the endpoint, and some querystring
|
|
|
+parameter
|
|
|
+
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
+--------------------------------------------------
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-verb-endpoint-params-body]
|
|
|
+--------------------------------------------------
|
|
|
+<1> Send a request by providing the verb, the endpoint, optional querystring
|
|
|
+parameters and the request body enclosed in an `org.apache.http.HttpEntity`
|
|
|
+object
|
|
|
+
|
|
|
+IMPORTANT: The `ContentType` specified for the `HttpEntity` is important
|
|
|
+because it will be used to set the `Content-Type` header so that Elasticsearch
|
|
|
+can properly parse the content.
|
|
|
+
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
+--------------------------------------------------
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-response-consumer]
|
|
|
+--------------------------------------------------
|
|
|
+<1> Send a request by providing the verb, the endpoint, optional querystring
|
|
|
+parameters, optional request body and the optional factory that is used to
|
|
|
+create an http://hc.apache.org/httpcomponents-core-ga/httpcore-nio/apidocs/org/apache/http/nio/protocol/HttpAsyncResponseConsumer.html[`org.apache.http.nio.protocol.HttpAsyncResponseConsumer`]
|
|
|
+callback instance per request attempt. Controls how the response body gets
|
|
|
+streamed from a non-blocking HTTP connection on the client side. When not
|
|
|
+provided, the default implementation is used which buffers the whole response
|
|
|
+body in heap memory, up to 100 MB.
|
|
|
+
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
+--------------------------------------------------
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-verb-endpoint-async]
|
|
|
+--------------------------------------------------
|
|
|
+<1> Define what needs to happen when the request is successfully performed
|
|
|
+<2> Define what needs to happen when the request fails, meaning whenever
|
|
|
+there's a connection error or a response with error status code is returned.
|
|
|
+<3> Send an async request by providing only the verb, the endpoint, and the
|
|
|
+response listener to be notified once the request is completed, minimum set
|
|
|
+of required arguments
|
|
|
+
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
+--------------------------------------------------
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-verb-endpoint-params-async]
|
|
|
+--------------------------------------------------
|
|
|
+<1> Send an async request by providing the verb, the endpoint, some querystring
|
|
|
+parameter and the response listener to be notified once the request is completed
|
|
|
+
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
+--------------------------------------------------
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-verb-endpoint-params-body-async]
|
|
|
+--------------------------------------------------
|
|
|
+<1> Send an async request by providing the verb, the endpoint, optional
|
|
|
+querystring parameters, the request body enclosed in an
|
|
|
+`org.apache.http.HttpEntity` object and the response listener to be
|
|
|
+notified once the request is completed
|
|
|
+
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
+--------------------------------------------------
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-response-consumer-async]
|
|
|
+--------------------------------------------------
|
|
|
+<1> Send an async request by providing the verb, the endpoint, optional
|
|
|
+querystring parameters, optional request body and the optional factory that is
|
|
|
+used to create an http://hc.apache.org/httpcomponents-core-ga/httpcore-nio/apidocs/org/apache/http/nio/protocol/HttpAsyncResponseConsumer.html[`org.apache.http.nio.protocol.HttpAsyncResponseConsumer`]
|
|
|
+callback instance per request attempt. Controls how the response body gets
|
|
|
+streamed from a non-blocking HTTP connection on the client side. When not
|
|
|
+provided, the default implementation is used which buffers the whole response
|
|
|
+body in heap memory, up to 100 MB.
|
|
|
+
|
|
|
+The following is a basic example of how async requests can be sent:
|
|
|
+
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
+--------------------------------------------------
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-async-example]
|
|
|
+--------------------------------------------------
|
|
|
+<1> Process the returned response
|
|
|
+<2> Handle the returned exception, due to communication error or a response
|
|
|
+with status code that indicates an error
|
|
|
+
|
|
|
+Each of the above listed method supports sending headers along with the
|
|
|
+request through a `Header` varargs argument as in the following examples:
|
|
|
+
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
+--------------------------------------------------
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-headers]
|
|
|
+--------------------------------------------------
|
|
|
+
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
+--------------------------------------------------
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-headers-async]
|
|
|
+--------------------------------------------------
|
|
|
|
|
|
[[java-rest-low-usage-responses]]
|
|
|
=== Reading responses
|
|
|
|
|
|
The `Response` object, either returned by the synchronous `performRequest` methods or
|
|
|
received as an argument in `ResponseListener#onSuccess(Response)`, wraps the
|
|
|
-response object returned by the http client and exposes the following information:
|
|
|
-
|
|
|
-`getRequestLine`:: information about the performed request
|
|
|
-`getHost`:: the host that returned the response
|
|
|
-`getStatusLine`:: the response status line
|
|
|
-`getHeaders`:: the response headers, which can also be retrieved by name
|
|
|
-though `getHeader(String)`
|
|
|
-`getEntity`:: the response body enclosed in an
|
|
|
-https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpEntity.html[`org.apache.http.HttpEntity`]
|
|
|
+response object returned by the http client and exposes some additional information.
|
|
|
+
|
|
|
+["source","java",subs="attributes,callouts,macros"]
|
|
|
+--------------------------------------------------
|
|
|
+include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-response2]
|
|
|
+--------------------------------------------------
|
|
|
+<1> Information about the performed request
|
|
|
+<2> The host that returned the response
|
|
|
+<3> The response status line, from which you can for instance retrieve the status code
|
|
|
+<4> The response headers, which can also be retrieved by name though `getHeader(String)`
|
|
|
+<5> The response body enclosed in an https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpEntity.html[`org.apache.http.HttpEntity`]
|
|
|
object
|
|
|
|
|
|
When performing a request, an exception is thrown (or received as an argument
|
|
|
in `ResponseListener#onFailure(Exception)` in the following scenarios:
|
|
|
|
|
|
-`IOException`:: communication problem (e.g. SocketTimeoutException etc.)
|
|
|
+`IOException`:: communication problem (e.g. SocketTimeoutException)
|
|
|
`ResponseException`:: a response was returned, but its status code indicated
|
|
|
an error (not `2xx`). A `ResponseException` originates from a valid
|
|
|
http response, hence it exposes its corresponding `Response` object which gives
|
|
@@ -227,37 +282,6 @@ with the get api as it can return `404` when the document is missing, in which
|
|
|
case the response body will not contain an error but rather the usual get api
|
|
|
response, just without the document as it was not found.
|
|
|
|
|
|
-
|
|
|
-[[java-rest-low-usage-example]]
|
|
|
-=== Example requests
|
|
|
-
|
|
|
-Here are a couple of examples:
|
|
|
-
|
|
|
-[source,java]
|
|
|
---------------------------------------------------
|
|
|
-Response response = restClient.performRequest("GET", "/",
|
|
|
- Collections.singletonMap("pretty", "true"));
|
|
|
-System.out.println(EntityUtils.toString(response.getEntity()));
|
|
|
-
|
|
|
-//index a document
|
|
|
-HttpEntity entity = new NStringEntity(
|
|
|
- "{\n" +
|
|
|
- " \"user\" : \"kimchy\",\n" +
|
|
|
- " \"post_date\" : \"2009-11-15T14:12:12\",\n" +
|
|
|
- " \"message\" : \"trying out Elasticsearch\"\n" +
|
|
|
- "}", ContentType.APPLICATION_JSON);
|
|
|
-
|
|
|
-Response indexResponse = restClient.performRequest(
|
|
|
- "PUT",
|
|
|
- "/twitter/tweet/1",
|
|
|
- Collections.<String, String>emptyMap(),
|
|
|
- entity);
|
|
|
---------------------------------------------------
|
|
|
-
|
|
|
-IMPORTANT: The `ContentType` that you specify for the `HttpEntity` is important
|
|
|
-because it will be used to set the `Content-Type` header so that Elasticsearch
|
|
|
-can properly parse the content.
|
|
|
-
|
|
|
Note that the low-level client doesn't expose any helper for json marshalling
|
|
|
and un-marshalling. Users are free to use the library that they prefer for that
|
|
|
purpose.
|
|
@@ -272,40 +296,6 @@ possible to provide a custom
|
|
|
http://hc.apache.org/httpcomponents-core-ga/httpcore-nio/apidocs/org/apache/http/nio/protocol/HttpAsyncResponseConsumer.html[`org.apache.http.nio.protocol.HttpAsyncResponseConsumer`]
|
|
|
that controls how bytes are read and buffered.
|
|
|
|
|
|
-The following is a basic example of how async requests can be sent:
|
|
|
-
|
|
|
-[source,java]
|
|
|
---------------------------------------------------
|
|
|
-int numRequests = 10;
|
|
|
-final CountDownLatch latch = new CountDownLatch(numRequests);
|
|
|
-
|
|
|
-for (int i = 0; i < numRequests; i++) {
|
|
|
- restClient.performRequestAsync(
|
|
|
- "PUT",
|
|
|
- "/twitter/tweet/" + i,
|
|
|
- Collections.<String, String>emptyMap(),
|
|
|
- //assume that the documents are stored in an entities array
|
|
|
- entities[i],
|
|
|
- new ResponseListener() {
|
|
|
- @Override
|
|
|
- public void onSuccess(Response response) {
|
|
|
- System.out.println(response);
|
|
|
- latch.countDown();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onFailure(Exception exception) {
|
|
|
- latch.countDown();
|
|
|
- }
|
|
|
- }
|
|
|
- );
|
|
|
-}
|
|
|
-
|
|
|
-//wait for all requests to be completed
|
|
|
-latch.await();
|
|
|
-
|
|
|
---------------------------------------------------
|
|
|
-
|
|
|
[[java-rest-low-usage-logging]]
|
|
|
=== Logging
|
|
|
|