java.io.Closeable
, java.lang.AutoCloseable
public class RestClient
extends java.lang.Object
implements java.io.Closeable
Must be created using RestClientBuilder
, which allows to set all the different options or just rely on defaults.
The hosts that are part of the cluster need to be provided at creation time, but can also be replaced later
by calling setHosts(HttpHost...)
.
The method performRequest(String, String, Map, HttpEntity, Header...)
allows to send a request to the cluster. When
sending a request, a host gets selected out of the provided ones in a round-robin fashion. Failing hosts are marked dead and
retried after a certain amount of time (minimum 1 minute, maximum 30 minutes), depending on how many times they previously
failed (the more failures, the later they will be retried). In case of failures all of the alive nodes (or dead nodes that
deserve a retry) are retried until one responds or none of them does, in which case an IOException
will be thrown.
Requests can be either synchronous or asynchronous. The asynchronous variants all end with Async
.
Requests can be traced by enabling trace logging for "tracer". The trace logger outputs requests and responses in curl format.
Modifier and Type | Class | Description |
---|---|---|
static class |
RestClient.FailureListener |
Listener that allows to be notified whenever a failure happens.
|
Modifier and Type | Method | Description |
---|---|---|
static RestClientBuilder |
builder(org.apache.http.HttpHost... hosts) |
Returns a new
RestClientBuilder to help with RestClient creation. |
void |
close() |
|
Response |
performRequest(java.lang.String method,
java.lang.String endpoint,
java.util.Map<java.lang.String,java.lang.String> params,
org.apache.http.Header... headers) |
Sends a request to the Elasticsearch cluster that the client points to and waits for the corresponding response
to be returned.
|
Response |
performRequest(java.lang.String method,
java.lang.String endpoint,
java.util.Map<java.lang.String,java.lang.String> params,
org.apache.http.HttpEntity entity,
org.apache.http.Header... headers) |
Sends a request to the Elasticsearch cluster that the client points to and waits for the corresponding response
to be returned.
|
Response |
performRequest(java.lang.String method,
java.lang.String endpoint,
java.util.Map<java.lang.String,java.lang.String> params,
org.apache.http.HttpEntity entity,
HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory,
org.apache.http.Header... headers) |
Sends a request to the Elasticsearch cluster that the client points to.
|
Response |
performRequest(java.lang.String method,
java.lang.String endpoint,
org.apache.http.Header... headers) |
Sends a request to the Elasticsearch cluster that the client points to and waits for the corresponding response
to be returned.
|
void |
performRequestAsync(java.lang.String method,
java.lang.String endpoint,
java.util.Map<java.lang.String,java.lang.String> params,
org.apache.http.HttpEntity entity,
HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory,
ResponseListener responseListener,
org.apache.http.Header... headers) |
Sends a request to the Elasticsearch cluster that the client points to.
|
void |
performRequestAsync(java.lang.String method,
java.lang.String endpoint,
java.util.Map<java.lang.String,java.lang.String> params,
org.apache.http.HttpEntity entity,
ResponseListener responseListener,
org.apache.http.Header... headers) |
Sends a request to the Elasticsearch cluster that the client points to.
|
void |
performRequestAsync(java.lang.String method,
java.lang.String endpoint,
java.util.Map<java.lang.String,java.lang.String> params,
ResponseListener responseListener,
org.apache.http.Header... headers) |
Sends a request to the Elasticsearch cluster that the client points to.
|
void |
performRequestAsync(java.lang.String method,
java.lang.String endpoint,
ResponseListener responseListener,
org.apache.http.Header... headers) |
Sends a request to the Elasticsearch cluster that the client points to.
|
void |
setHosts(org.apache.http.HttpHost... hosts) |
Replaces the hosts that the client communicates with.
|
public static RestClientBuilder builder(org.apache.http.HttpHost... hosts)
RestClientBuilder
to help with RestClient
creation.
Creates a new builder instance and sets the hosts that the client will send requests to.public void setHosts(org.apache.http.HttpHost... hosts)
HttpHost
public Response performRequest(java.lang.String method, java.lang.String endpoint, org.apache.http.Header... headers) throws java.io.IOException
performRequest(String, String, Map, HttpEntity, Header...)
but without parameters
and request body.method
- the http methodendpoint
- the path of the request (without host and port)headers
- the optional request headersjava.io.IOException
- in case of a problem or the connection was abortedorg.apache.http.client.ClientProtocolException
- in case of an http protocol errorResponseException
- in case Elasticsearch responded with a status code that indicated an errorpublic Response performRequest(java.lang.String method, java.lang.String endpoint, java.util.Map<java.lang.String,java.lang.String> params, org.apache.http.Header... headers) throws java.io.IOException
performRequest(String, String, Map, HttpEntity, Header...)
but without request body.method
- the http methodendpoint
- the path of the request (without host and port)params
- the query_string parametersheaders
- the optional request headersjava.io.IOException
- in case of a problem or the connection was abortedorg.apache.http.client.ClientProtocolException
- in case of an http protocol errorResponseException
- in case Elasticsearch responded with a status code that indicated an errorpublic Response performRequest(java.lang.String method, java.lang.String endpoint, java.util.Map<java.lang.String,java.lang.String> params, org.apache.http.HttpEntity entity, org.apache.http.Header... headers) throws java.io.IOException
performRequest(String, String, Map, HttpEntity, HttpAsyncResponseConsumerFactory, Header...)
which doesn't require specifying an HttpAsyncResponseConsumerFactory
instance,
HttpAsyncResponseConsumerFactory
will be used to create the needed instances of HttpAsyncResponseConsumer
.method
- the http methodendpoint
- the path of the request (without host and port)params
- the query_string parametersentity
- the body of the request, null if not applicableheaders
- the optional request headersjava.io.IOException
- in case of a problem or the connection was abortedorg.apache.http.client.ClientProtocolException
- in case of an http protocol errorResponseException
- in case Elasticsearch responded with a status code that indicated an errorpublic Response performRequest(java.lang.String method, java.lang.String endpoint, java.util.Map<java.lang.String,java.lang.String> params, org.apache.http.HttpEntity entity, HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory, org.apache.http.Header... headers) throws java.io.IOException
IOException
will be thrown.method
- the http methodendpoint
- the path of the request (without host and port)params
- the query_string parametersentity
- the body of the request, null if not applicablehttpAsyncResponseConsumerFactory
- the HttpAsyncResponseConsumerFactory
used to create one
HttpAsyncResponseConsumer
callback per retry. Controls how the response body gets streamed from a non-blocking HTTP
connection on the client side.headers
- the optional request headersjava.io.IOException
- in case of a problem or the connection was abortedorg.apache.http.client.ClientProtocolException
- in case of an http protocol errorResponseException
- in case Elasticsearch responded with a status code that indicated an errorpublic void performRequestAsync(java.lang.String method, java.lang.String endpoint, ResponseListener responseListener, org.apache.http.Header... headers)
ResponseListener
will be notified upon completion or failure. Shortcut to
performRequestAsync(String, String, Map, HttpEntity, ResponseListener, Header...)
but without parameters and request body.method
- the http methodendpoint
- the path of the request (without host and port)responseListener
- the ResponseListener
to notify when the request is completed or failsheaders
- the optional request headerspublic void performRequestAsync(java.lang.String method, java.lang.String endpoint, java.util.Map<java.lang.String,java.lang.String> params, ResponseListener responseListener, org.apache.http.Header... headers)
ResponseListener
will be notified upon completion or failure. Shortcut to
performRequestAsync(String, String, Map, HttpEntity, ResponseListener, Header...)
but without request body.method
- the http methodendpoint
- the path of the request (without host and port)params
- the query_string parametersresponseListener
- the ResponseListener
to notify when the request is completed or failsheaders
- the optional request headerspublic void performRequestAsync(java.lang.String method, java.lang.String endpoint, java.util.Map<java.lang.String,java.lang.String> params, org.apache.http.HttpEntity entity, ResponseListener responseListener, org.apache.http.Header... headers)
ResponseListener
will be notified upon completion or failure.
Shortcut to performRequestAsync(String, String, Map, HttpEntity, HttpAsyncResponseConsumerFactory, ResponseListener,
Header...)
which doesn't require specifying an HttpAsyncResponseConsumerFactory
instance,
HttpAsyncResponseConsumerFactory
will be used to create the needed instances of HttpAsyncResponseConsumer
.method
- the http methodendpoint
- the path of the request (without host and port)params
- the query_string parametersentity
- the body of the request, null if not applicableresponseListener
- the ResponseListener
to notify when the request is completed or failsheaders
- the optional request headerspublic void performRequestAsync(java.lang.String method, java.lang.String endpoint, java.util.Map<java.lang.String,java.lang.String> params, org.apache.http.HttpEntity entity, HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory, ResponseListener responseListener, org.apache.http.Header... headers)
ResponseListener
gets notified upon request completion or failure.
Selects a host out of the provided ones in a round-robin fashion. Failing hosts are marked dead and retried after a certain
amount of time (minimum 1 minute, maximum 30 minutes), depending on how many times they previously failed (the more failures,
the later they will be retried). In case of failures all of the alive nodes (or dead nodes that deserve a retry) are retried
until one responds or none of them does, in which case an IOException
will be thrown.method
- the http methodendpoint
- the path of the request (without host and port)params
- the query_string parametersentity
- the body of the request, null if not applicablehttpAsyncResponseConsumerFactory
- the HttpAsyncResponseConsumerFactory
used to create one
HttpAsyncResponseConsumer
callback per retry. Controls how the response body gets streamed from a non-blocking HTTP
connection on the client side.responseListener
- the ResponseListener
to notify when the request is completed or failsheaders
- the optional request headerspublic void close() throws java.io.IOException
close
in interface java.lang.AutoCloseable
close
in interface java.io.Closeable
java.io.IOException