2017-03-28 17:23:01 -04:00
[[api-java]]
== Java API
2017-12-01 06:24:26 -05:00
deprecated[7.0.0, The `TransportClient` is deprecated in favour of the {java-rest}/java-rest-high.html[Java High Level REST Client] and will be removed in Elasticsearch 8.0. The {java-rest}/java-rest-high-level-migration.html[migration guide] describes all the steps needed to migrate.]
2017-03-28 17:23:01 -04:00
{xpack} provides a Java client called `WatcherClient` that adds native Java
support for the {watcher}.
To obtain a `WatcherClient` instance, make sure you first set up the
`XPackClient`.
2020-07-23 12:42:33 -04:00
[discrete]
2017-03-28 17:23:01 -04:00
=== Installing XPackClient
You first need to make sure the +x-pack-transport-{version}+ JAR file is in the classpath.
You can extract this jar from the downloaded {xpack} bundle.
If you use Maven to manage dependencies, add the following to the `pom.xml`:
["source","xml",subs="attributes,callouts"]
--------------------------------------------------
<project ...>
<repositories>
<!-- add the elasticsearch repo -->
<repository>
<id>elasticsearch-releases</id>
<url>https://artifacts.elastic.co/maven</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
...
</repositories>
...
<dependencies>
<!-- add the x-pack jar as a dependency -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>x-pack-transport</artifactId>
<version>{version}</version>
</dependency>
...
</dependencies>
...
</project>
--------------------------------------------------
If you use Gradle, add the dependencies to `build.gradle`:
["source","groovy",subs="attributes,callouts"]
--------------------------------------------------------------
repositories {
/* ... Any other repositories ... */
// Add the Elasticsearch Maven Repository
maven {
2019-05-07 06:34:51 -04:00
name "elastic"
2017-03-28 17:23:01 -04:00
url "https://artifacts.elastic.co/maven"
}
}
dependencies {
// Provide the x-pack jar on the classpath for compilation and at runtime
compile "org.elasticsearch.client:x-pack-transport:{version}"
/* ... */
}
--------------------------------------------------------------
You can also download the https://artifacts.elastic.co/maven/org/elasticsearch/client/x-pack-transport/{version}/x-pack-transport-{version}.jar[X-Pack Transport JAR]
manually, directly from our Maven repository.
2020-07-23 12:42:33 -04:00
[discrete]
2017-03-28 17:23:01 -04:00
=== Obtaining the `WatcherClient`
To obtain an instance of the `WatcherClient` you first need to create the
`XPackClient`. The `XPackClient` is a wrapper around the standard Java
Elasticsearch `Client`:
[source,java]
--------------------------------------------------
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
2018-01-23 13:43:58 -05:00
import org.elasticsearch.xpack.core.XPackClient;
import org.elasticsearch.xpack.core.XPackPlugin;
import org.elasticsearch.core.watcher.client.WatcherClient;
2017-03-28 17:23:01 -04:00
...
TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
.put("cluster.name", "myClusterName")
...
.build())
2018-05-17 08:23:08 -04:00
.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
2017-03-28 17:23:01 -04:00
XPackClient xpackClient = new XPackClient(client);
WatcherClient watcherClient = xpackClient.watcher();
--------------------------------------------------
include::java/put-watch.asciidoc[]
include::java/get-watch.asciidoc[]
include::java/delete-watch.asciidoc[]
include::java/execute-watch.asciidoc[]
include::java/ack-watch.asciidoc[]
include::java/activate-watch.asciidoc[]
include::java/deactivate-watch.asciidoc[]
include::java/stats.asciidoc[]
include::java/service.asciidoc[]