[[api-java-execute-watch]]
==== Execute Watch API

This API forces the execution of a watch stored in the `.watches` index.
It can be used to test a watch without executing all the actions or by ignoring the condition.
The response contains a `BytesReference` that represents the record that would be written to the `.watch_history` index.

The following example executes a watch with the name `my-watch`

[source,java]
--------------------------------------------------
ExecuteWatchResponse executeWatchResponse = watcherClient.prepareExecuteWatch("my-watch")

    // Will execute the actions no matter what the condition returns
    .setIgnoreCondition(true)

    // A map containing alternative input to use instead of the input result from the watch's input
    .setAlternativeInput(new HashMap<String, Object>())

    // Trigger data to use (Note that "scheduled_time" is not provided to the ctx.trigger by this
    // execution method so you may want to include it here)
    .setTriggerData(new HashMap<String, Object>())

    // Simulating the "email_admin" action while ignoring its throttle state. Use 
    // "_all" to set the action execution mode to all actions
    .setActionMode("_all", ActionExecutionMode.FORCE_SIMULATE)

    // If the execution of this watch should be written to the `.watch_history` index and reflected
    // in the persisted Watch
    .setRecordExecution(false)
    
    .get()
--------------------------------------------------

Once the response is returned, you can explore it by getting execution record source:

[source,java]
--------------------------------------------------
XContentSource source = executeWatchResponse.getRecordSource();
--------------------------------------------------

The `XContentSource` provides you methods to explore the source:

[source,java]
--------------------------------------------------
Map<String, Object> map = source.getAsMap();
--------------------------------------------------

Or get a specific value associated with a known key:

[source,java]
--------------------------------------------------
String actionId = source.getValue("result.actions.0.id");
--------------------------------------------------