| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 | [[sniffer]]== SnifferMinimal library that allows to automatically discover nodes from a runningElasticsearch cluster and set them to an existing `RestClient` instance.It retrieves by default the nodes that belong to the cluster using theNodes Info api and uses jackson to parse the obtained json response.Compatible with Elasticsearch 2.x and onwards.[[java-rest-sniffer-javadoc]]=== JavadocThe javadoc for the REST client sniffer can be found at {rest-client-sniffer-javadoc}/index.html.=== Maven RepositoryThe REST client sniffer is subject to the same release cycle asElasticsearch. Replace the version with the desired sniffer version, firstreleased with `5.0.0-alpha4`. There is no relation between the sniffer versionand the Elasticsearch version that the client can communicate with. Sniffersupports fetching the nodes list from Elasticsearch 2.x and onwards.If you are looking for a SNAPSHOT version, the Elastic Maven Snapshot repository is availableat https://snapshots.elastic.co/maven/.==== Maven configurationHere is how you can configure the dependency using maven as a dependency manager.Add the following to your `pom.xml` file:["source","xml",subs="attributes"]--------------------------------------------------<dependency>    <groupId>org.elasticsearch.client</groupId>    <artifactId>elasticsearch-rest-client-sniffer</artifactId>    <version>{version}</version></dependency>--------------------------------------------------==== Gradle configurationHere is how you can configure the dependency using gradle as a dependency manager.Add the following to your `build.gradle` file:["source","groovy",subs="attributes"]--------------------------------------------------dependencies {    compile 'org.elasticsearch.client:elasticsearch-rest-client-sniffer:{version}'}--------------------------------------------------=== UsageOnce a `RestClient` instance has been created as shown in <<java-rest-low-usage-initialization>>,a `Sniffer` can be associated to it. The `Sniffer` will make use of the provided `RestClient`to periodically (every 5 minutes by default) fetch the list of current nodes from the clusterand update them by calling `RestClient#setNodes`.["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SnifferDocumentation.java[sniffer-init]--------------------------------------------------It is important to close the `Sniffer` so that its background thread getsproperly shutdown and all of its resources are released. The `Sniffer`object should have the same lifecycle as the `RestClient` and get closedright before the client:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SnifferDocumentation.java[sniffer-close]--------------------------------------------------The `Sniffer` updates the nodes by default every 5 minutes. This interval canbe customized by providing it (in milliseconds) as follows:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SnifferDocumentation.java[sniffer-interval]--------------------------------------------------It is also possible to enable sniffing on failure, meaning that after eachfailure the nodes list gets updated straightaway rather than at the followingordinary sniffing round. In this case a `SniffOnFailureListener` needs tobe created at first and provided at `RestClient` creation. Also once the`Sniffer` is later created, it needs to be associated with that same`SniffOnFailureListener` instance, which will be notified at each failureand use the `Sniffer` to perform the additional sniffing round as described.["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SnifferDocumentation.java[sniff-on-failure]--------------------------------------------------<1> Set the failure listener to the `RestClient` instance<2> When sniffing on failure, not only do the nodes get updated after eachfailure, but an additional sniffing round is also scheduled sooner than usual,by default one minute after the failure, assuming that things will go back tonormal and we want to detect that as soon as possible. Said interval can becustomized  at `Sniffer` creation time through the `setSniffAfterFailureDelayMillis`method. Note that this last configuration parameter has no effect in case sniffingon failure is not enabled like explained above.<3> Set the `Sniffer` instance to the failure listenerThe Elasticsearch Nodes Info api doesn't return the protocol to use whenconnecting to the nodes but only their `host:port` key-pair, hence `http`is used by default. In case `https` should be used instead, the`ElasticsearchNodesSniffer` instance has to be manually created and providedas follows:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SnifferDocumentation.java[sniffer-https]--------------------------------------------------In the same way it is also possible to customize the `sniffRequestTimeout`,which defaults to one second. That is the `timeout` parameter provided as aquerystring parameter when calling the Nodes Info api, so that when thetimeout expires on the server side, a valid response is still returnedalthough it may contain only a subset of the nodes that are part of thecluster, the ones that have responded until then.["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SnifferDocumentation.java[sniff-request-timeout]--------------------------------------------------Also, a custom `NodesSniffer` implementation can be provided for advanceduse-cases that may require fetching the `Node`s from external sources ratherthan from Elasticsearch:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SnifferDocumentation.java[custom-nodes-sniffer]--------------------------------------------------<1> Fetch the hosts from the external source
 |