Cancel Tasks API

edit

The Cancel Tasks API allows cancellation of a currently running task.

Cancel Tasks Request

edit

A CancelTasksRequest:

CancelTasksRequest request = new org.elasticsearch.client.tasks.CancelTasksRequest.Builder()
    .withNodesFiltered(Arrays.asList("nodeId1", "nodeId2"))
    .withActionsFiltered(Collections.singletonList("cluster:*"))
    .build();

There are no required parameters. The task cancellation command supports the same task selection parameters as the list tasks command.

Parameters

edit
CancelTasksRequest byTaskIdRequest = new org.elasticsearch.client.tasks.CancelTasksRequest.Builder() 
    .withTaskId(new org.elasticsearch.client.tasks.TaskId("myNode",44L)) 
    .build(); 

Cancel a task

Cancel only cluster-related tasks

Cancel all tasks running on nodes nodeId1 and nodeId2

Synchronous Execution

edit
CancelTasksResponse response = client.tasks().cancel(request, RequestOptions.DEFAULT);

Asynchronous Execution

edit

The asynchronous execution requires CancelTasksRequest instance and an ActionListener instance to be passed to the asynchronous method:

client.tasks().cancelAsync(request, RequestOptions.DEFAULT, listener); 

The CancelTasksRequest 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 CancelTasksResponse looks like:

ActionListener<CancelTasksResponse> listener =
    new ActionListener<CancelTasksResponse>() {
        @Override
        public void onResponse(CancelTasksResponse 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 a failure. The raised exception is provided as an argument

Cancel Tasks Response

edit
List<org.elasticsearch.client.tasks.TaskInfo> tasks = response.getTasks(); 

List of cancelled tasks

Map<String, List<org.elasticsearch.client.tasks.TaskInfo>> perNodeTasks = response.getPerNodeTasks(); 
List<org.elasticsearch.client.tasks.TaskGroup> groups = response.getTaskGroups(); 

List of cancelled tasks grouped by a node

List of cancelled tasks grouped by a parent task

List<org.elasticsearch.client.tasks.ElasticsearchException> nodeFailures = response.getNodeFailures(); 
List<org.elasticsearch.client.tasks.TaskOperationFailure> taskFailures = response.getTaskFailures(); 

List of node failures

List of task cancellation failures