Execute Watch API

edit

The execute watch API allows clients to immediately execute a watch, either one that has been previously added via the Put Watch API or inline as part of the request.

Execute by id

edit

Submit the following request to execute a previously added watch:

ExecuteWatchRequest request = ExecuteWatchRequest.byId("my_watch_id");
request.setAlternativeInput("{ \"foo\" : \"bar\" }");                                         
request.setActionMode("action1", ExecuteWatchRequest.ActionExecutionMode.SIMULATE);             
request.setRecordExecution(true);                                                               
request.setIgnoreCondition(true);                                                               
request.setTriggerData("{\"triggered_time\":\"now\"}");                                         
request.setDebug(true);                                                                         
ExecuteWatchResponse response = client.watcher().executeWatch(request, RequestOptions.DEFAULT);

Alternative input for the watch to use in json format

Set the mode for action "action1" to SIMULATE

Record this execution in watcher history

Execute the watch regardless of the watch’s condition

Set the trigger data for the watch in json format

Enable debug mode

Execute by id Response

edit

The returned Response contains details of the execution:

String id = response.getRecordId();                                         
Map<String, Object> watch = response.getRecordAsMap();                      
String watch_id = ObjectPath.eval("watch_record.watch_id", watch);          

The record ID for this execution

The execution response as a java Map

Extract information from the response map using ObjectPath

Asynchronous execution by id

edit

This request can be executed asynchronously:

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

The ExecuteWatchRequest 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 ExecuteWatchResponse looks like:

ActionListener<ExecuteWatchResponse> listener = new ActionListener<ExecuteWatchResponse>() {
    @Override
    public void onResponse(ExecuteWatchResponse 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

Execute inline

edit

Submit the following request to execute a watch defined as part of the request:

String watchJson = "{ \n" +
    "  \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" +
    "  \"input\": { \"none\": {} },\n" +
    "  \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" +
    "}";
ExecuteWatchRequest request = ExecuteWatchRequest.inline(watchJson);
request.setAlternativeInput("{ \"foo\" : \"bar\" }");                                         
request.setActionMode("action1", ExecuteWatchRequest.ActionExecutionMode.SIMULATE);             
request.setIgnoreCondition(true);                                                               
request.setTriggerData("{\"triggered_time\":\"now\"}");                                         
request.setDebug(true);                                                                         
ExecuteWatchResponse response = client.watcher().executeWatch(request, RequestOptions.DEFAULT);

Alternative input for the watch to use in json format

Set the mode for action "action1" to SIMULATE

Execute the watch regardless of the watch’s condition

Set the trigger data for the watch in json format

Enable debug mode

Note that inline watches cannot be recorded.

The response format and asynchronous execution methods are the same as for the Execute Watch by ID API.