Skip to main content

Asynchronous Activity Completion - Temporal Java SDK feature guide

This page shows how to asynchronously complete an Activity

Asynchronous Activity Completion enables the Activity Function to return without the Activity Execution completing.

There are three steps to follow:

  1. The Activity provides the external system with identifying information needed to complete the Activity Execution. Identifying information can be a Task Token, or a combination of Namespace, Workflow Id, and Activity Id.
  2. The Activity Function completes in a way that identifies it as waiting to be completed by an external system.
  3. The Temporal Client is used to Heartbeat and complete the Activity.

To complete an Activity asynchronously, set the ActivityCompletionClient interface to the complete() method.

    @Override
public String composeGreeting(String greeting, String name) {

// Get the activity execution context
ActivityExecutionContext context = Activity.getExecutionContext();

// Set a correlation token that can be used to complete the activity asynchronously
byte[] taskToken = context.getTaskToken();

/**
* For the example we will use a {@link java.util.concurrent.ForkJoinPool} to execute our
* activity. In real-life applications this could be any service. The composeGreetingAsync
* method is the one that will actually complete workflow action execution.
*/
ForkJoinPool.commonPool().execute(() -> composeGreetingAsync(taskToken, greeting, name));
context.doNotCompleteOnReturn();

// Since we have set doNotCompleteOnReturn(), the workflow action method return value is
// ignored.
return "ignored";
}

// Method that will complete action execution using the defined ActivityCompletionClient
private void composeGreetingAsync(byte[] taskToken, String greeting, String name) {
String result = greeting + " " + name + "!";

// Complete our workflow activity using ActivityCompletionClient
completionClient.complete(taskToken, result);
}
}

Alternatively, set the doNotCompleteOnReturn() method during an Activity Execution.

    @Override
public String composeGreeting(String greeting, String name) {

// Get the activity execution context
ActivityExecutionContext context = Activity.getExecutionContext();

// Set a correlation token that can be used to complete the activity asynchronously
byte[] taskToken = context.getTaskToken();

/**
* For the example we will use a {@link java.util.concurrent.ForkJoinPool} to execute our
* activity. In real-life applications this could be any service. The composeGreetingAsync
* method is the one that will actually complete workflow action execution.
*/
ForkJoinPool.commonPool().execute(() -> composeGreetingAsync(taskToken, greeting, name));
context.doNotCompleteOnReturn();

// Since we have set doNotCompleteOnReturn(), the workflow action method return value is
// ignored.
return "ignored";
}

When this method is called during an Activity Execution, the Activity Execution does not complete when its method returns.