Unable to create PreBuiltXPackTransportClient client x-pack

I am working on x-pack watcher. I have added dependency of x-pack.

org.elasticsearch.client x-pack-transport 5.3.3 org.apache.logging.log4j log4j-api 2.7 org.apache.logging.log4j log4j-core 2.7

I can create watcher using curl request but unable to create from java code. I have created a watcher using curl and try to get it from jave code. here is my code.

package watcher;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.xpack.XPackClient;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.elasticsearch.xpack.watcher.client.WatcherClient;
import org.elasticsearch.xpack.watcher.support.xcontent.XContentSource;
import org.elasticsearch.xpack.watcher.transport.actions.get.GetWatchResponse;

public class GetWatcher {

public static void main(String[] args) throws UnknownHostException {

    @SuppressWarnings("resource")
    TransportClient client = new PreBuiltXPackTransportClient(
            Settings.builder().put("cluster.name", "my-application").build())
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

    XPackClient xpackClient = new XPackClient(client);
    WatcherClient watcherClient = xpackClient.watcher();

    GetWatchResponse getWatchResponse = watcherClient.prepareGetWatch("my-watch").get();
    XContentSource source = getWatchResponse.getSource();
    Map<String, Object> map = source.getAsMap();
    for (String key : map.keySet()) {
        System.out.println(key);
    }

}

}

Error-

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Exception in thread "main" java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at >sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.elasticsearch.plugins.PluginsService.loadPlugin(PluginsService.java:383)
at org.elasticsearch.plugins.PluginsService.(PluginsService.java:101)
at org.elasticsearch.client.transport.TransportClient.newPluginService(TransportClient.java:101)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:126)
at org.elasticsearch.client.transport.TransportClient.(TransportClient.java:268)
at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:127)
at org.elasticsearch.xpack.client.PreBuiltXPackTransportClient.(PreBuiltXPackTransportClient.java:55)
at org.elasticsearch.xpack.client.PreBuiltXPackTransportClient.(PreBuiltXPackTransportClient.java:50)
at org.elasticsearch.xpack.client.PreBuiltXPackTransportClient.(PreBuiltXPackTransportClient.java:46)
at watcher.GetWatcher.main(GetWatcher.java:21)
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: com.unboundid.util.Debug
at org.elasticsearch.xpack.XPackPlugin$1.run(XPackPlugin.java:160)
at org.elasticsearch.xpack.XPackPlugin$1.run(XPackPlugin.java:154)
at java.security.AccessController.doPrivileged(Native Method)
at org.elasticsearch.xpack.XPackPlugin.(XPackPlugin.java:154)
... 14 more
Caused by: java.lang.ClassNotFoundException: com.unboundid.util.Debug
at java.net.URLClassLoader$1.run(URLClassLoader.java:370)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at org.elasticsearch.xpack.XPackPlugin$1.run(XPackPlugin.java:158)
... 17 more

I have tried with PreBuiltTransportClient client. I can get all the indexes of elasticsearch using this client but unable to create or get watcher. It gives error-

Exception in thread "main" java.lang.NullPointerException
at org.elasticsearch.client.transport.TransportProxyClient.lambda$execute$0(TransportProxyClient.java:59)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:247)
at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:59)
at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:366)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:404)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:80)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:54)
at org.elasticsearch.action.ActionRequestBuilder.get(ActionRequestBuilder.java:62)

There is very documentation for x-pack watcher. I am getting only a single example on Java API | X-Pack for the Elastic Stack [6.2] | Elastic

Your error specifies a java.lang.ClassNotFoundException

Try adding the following to your build.gradle (or pom or whatever build tool you are using).

  compile 'com.unboundid:unboundid-ldapsdk:3.2.0'
  compile 'org.bouncycastle:bcprov-jdk15on:1.58'
  compile 'org.bouncycastle:bcpkix-jdk15on:1.58'

Thanks,
Baz.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.