1

I am using the below code sample where I am calling the cancelWF method to cancel the execution of workflow. The onCatch method is successfully invoked with the RuntimeException("Simply cancel"), but on the Amazon SWF console the WF does not end immediately, it waits will timeout and ends with a WorkflowExecutionTerminated event.

The whole project is available here if you want more info.

package aws.swf;

import aws.swf.common.Constants;
import aws.swf.common.DelayRequest;
import aws.swf.common.MyActivityClient;
import aws.swf.common.MyActivityClientImpl;
import aws.swf.common.MyWorkflow;
import aws.swf.common.SWFClient;
import com.amazonaws.services.simpleworkflow.AmazonSimpleWorkflow;
import com.amazonaws.services.simpleworkflow.flow.WorkflowWorker;
import com.amazonaws.services.simpleworkflow.flow.annotations.Asynchronous;
import com.amazonaws.services.simpleworkflow.flow.core.Promise;
import com.amazonaws.services.simpleworkflow.flow.core.TryCatch;

import java.util.concurrent.CancellationException;

public class D_CancelWorkflow implements MyWorkflow {

    private TryCatch tryCatch;
    private final MyActivityClient activityClient = new MyActivityClientImpl();

    @Override
    public void sum() {
        tryCatch = new TryCatch() {
            @Override
            protected void doTry() throws Throwable {
                System.out.printf("[WF %s] Started exec\n", D_CancelWorkflow.this);
                Promise<Integer> result = activityClient.getNumWithDelay(new DelayRequest("req1", 1));
                cancelWF(result);
                newDelayRequest(result);
            }

            @Override
            protected void doCatch(Throwable e) throws Throwable {
                if (e instanceof CancellationException) {
                    System.out.printf("[WF %s] Cancelled With message [%s]\n",
                            D_CancelWorkflow.this, e.getCause().getMessage());
                } else {
                    e.printStackTrace();
                }
                rethrow(e);
            }
        };
    }

    @Asynchronous
    private void newDelayRequest(Promise<Integer> num) {
        activityClient.getNumWithDelay(new DelayRequest("req2", 1));
    }

    @Asynchronous
    private void cancelWF(Promise<Integer> ignore) {
        System.out.printf("[WF %s] Cancelling WF\n", D_CancelWorkflow.this);
        this.tryCatch.cancel(new RuntimeException("Simply cancel"));
    }

    public static void main(String[] args) throws Exception {
        AmazonSimpleWorkflow awsSwfClient = new SWFClient().getClient();
        WorkflowWorker workflowWorker =
                new WorkflowWorker(awsSwfClient, Constants.DOMAIN, Constants.TASK_LIST);
        workflowWorker.addWorkflowImplementationType(D_CancelWorkflow.class);
        workflowWorker.start();
    }
}

This is the event history for one of my execution,

enter image description here

Kobi
  • 135,331
  • 41
  • 252
  • 292
vikkyhacks
  • 3,190
  • 9
  • 32
  • 47
  • I don't know what you *should* do, but I think you're canceling the `tryCatch`, not the workflow: https://docs.aws.amazon.com/amazonswf/latest/awsflowguide/errorhandling.html#test.cancellation.resources – Kobi Dec 04 '19 at 02:18
  • Interesting. What happens when you rethrow other type of exception from doCatch? For example RuntimeException that wraps the cancellation one? – Maxim Fateev Dec 04 '19 at 04:35
  • @MaximFateev: Nothing happens, I tried 'reThrow(err)' and 'throw err' but still the workflow does not cancel. – vikkyhacks Dec 04 '19 at 09:47
  • @Kobi Isn't that how I am supposed to explicitly cancel a TryCatch ? https://docs.aws.amazon.com/amazonswf/latest/awsflowguide/errorhandling.html#errorhandling.canceltask – vikkyhacks Dec 04 '19 at 09:53

0 Answers0