Create or update watch API

edit

Execution

edit

General information about the installed Watcher features can be retrieved using the watcher() method:

// you can also use the WatchSourceBuilder from org.elasticsearch.plugin:x-pack-core to create a watch programmatically
BytesReference watch = new BytesArray("{ \n" +
    "  \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" +
    "  \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" +
    "  \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" +
    "}");
PutWatchRequest request = new PutWatchRequest("my_watch_id", watch, XContentType.JSON);
request.setActive(false); 
PutWatchResponse response = client.watcher().putWatch(request, RequestOptions.DEFAULT);

Allows to store the watch, but to not trigger it. Defaults to true

Response

edit

The returned PutWatchResponse contains created, id, and version information.

String watchId = response.getId(); 
boolean isCreated = response.isCreated(); 
long version = response.getVersion(); 

_id contains id of the watch

created is a boolean indicating whether the watch was created for the first time

_version returns the newly created version

Asynchronous execution

edit

This request can be executed asynchronously:

client.watcher().putWatchAsync(request, RequestOptions.DEFAULT, listener); 

The PutWatchRequest to execute and the ActionListener to use when the execution completes

The asynchronous method does not block and returns immediately. Once it is completed the ActionListener is called back using the onResponse method if the execution successfully completed or using the onFailure method if it failed.

A typical listener for PutWatchResponse looks like:

ActionListener<PutWatchResponse> listener = new ActionListener<PutWatchResponse>() {
    @Override
    public void onResponse(PutWatchResponse response) {
        
    }

    @Override
    public void onFailure(Exception e) {
        
    }
};

Called when the execution is successfully completed. The response is provided as an argument

Called in case of failure. The raised exception is provided as an argument