In a fork/join, if one activity errors -- how to cancel the rest?

Greetings. I’m trying to figure something out. Imagine I launch 100 parallel activities in a workflow and use Promise.allOf(…) to join. Each activity could be long running (seconds/minutes). If any activity fails, the allOf(…) will fire the exception. But, what is the state of the other activities? I assume they are still running. What if I want to stop them? One solution might be to cancel the workflow, and therefore terminate the activities. That would force me to wrap the fork/join/cancel logic into a child-workflow. Is there any other way to cancel the activities without cancelling the workflow? Thanks!

1 Like

I’ll tag maxim in here. If you configure your activity to use a heartbeat and propagate that to your activity code in the appropriate places, that will enable you to cancel it from the workflow scope. Note this means its up to you to add heartbeats to your activity code and handle cleanup.

Give me a few minutes. I decided to write a sample to demonstrate this :slight_smile:

It looks like I will not be able to complete the sample today. But here is the code. It works, but due to some problem with exception mapping in the SDK it emits an unexpected exception trace to the standard error. I’m looking into fixing the SDK issue.

public class HelloCancellationScope {

  static final String TASK_QUEUE = "HelloCancellationScope";

  public interface GreetingWorkflow {
    String getGreeting(String name);

  public interface GreetingActivities {
    String composeGreeting(String greeting, String name);

  public static class GreetingWorkflowImpl implements GreetingWorkflow {

    private static String[] greetings =
        new String[] {"Hello", "Bye", "Hola", "Привет", "Oi", "Hallo"};

    private final GreetingActivities activities =

    public String getGreeting(String name) {
      Promise<String>[] results = new Promise[greetings.length];
      CancellationScope scope =
              () -> {
                for (int i = 0; i < greetings.length; i++) {
                  results[i] = Async.function(activities::composeGreeting, greetings[i], name);
      // As code inside the scope is non blocking the run doesn't block.;
      // Wait for one of the activities to complete.
      // Cancel all other activities
      // Get the result from one of the Promises.
      String result = null;
      for (int i = 0; i < greetings.length; i++) {
        if (results[i].isCompleted()) {
          result = results[i].get();
      // Wait for all activities to complete.
      return result;

  static class GreetingActivitiesImpl implements GreetingActivities {

    public String composeGreeting(String greeting, String name) {
      ActivityExecutionContext context = Activity.getExecutionContext();
      Random random = new Random();
      int count = random.nextInt(30);
      System.out.println("Activity for " + greeting + " going to take " + count + " seconds");
      for (int i = 0; i < count; i++) {
        try {
        } catch (InterruptedException e) {
          // Empty
        try {
        } catch (ActivityCancelledException e) {
          System.out.println("Activity for " + greeting + " was cancelled");
          throw e;
      System.out.println("Activity for " + greeting + " completed");
      return greeting + " " + name + "!";

  public static void main(String[] args) {
    // gRPC stubs wrapper that talks to the local docker instance of temporal service.
    WorkflowServiceStubs service = WorkflowServiceStubs.newInstance();
    // client that can be used to start and signal workflows
    WorkflowClient client = WorkflowClient.newInstance(service);

    // worker factory that can be used to create workers for specific task queues
    WorkerFactory factory = WorkerFactory.newInstance(client);
    // Worker that listens on a task queue and hosts both workflow and activity implementations.
    Worker worker = factory.newWorker(TASK_QUEUE);
    // Workflows are stateful. So you need a type to create instances.
    // Activities are stateless and thread safe. So a shared instance is used.
    worker.registerActivitiesImplementations(new GreetingActivitiesImpl());
    // Start listening to the workflow and activity task queues.

    // Start a workflow execution. Usually this is done from another program.\n'
    // Uses task queue from the GreetingWorkflow @WorkflowMethod annotation.
    GreetingWorkflow workflow =
            GreetingWorkflow.class, WorkflowOptions.newBuilder().setTaskQueue(TASK_QUEUE).build());
    // Execute a workflow waiting for it to complete.
    String greeting = workflow.getGreeting("World");

Thank you, this is an awesome example! The CancellationScope totally makes sense. I’ve been experimenting this afternoon and it behaves well.


1 Like