1

我正在使用下面的代码示例,我正在调用该cancelWF方法来取消工作流的执行。onCatch使用成功调用该方法RuntimeException("Simply cancel"),但在 Amazon SWF 控制台上,WF 不会立即结束,它会等待超时并以WorkflowExecutionTerminated事件结束。

如果您想了解更多信息,可以在此处获得整个项目。

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();
    }
}

这是我的一次处决的事件历史,

在此处输入图像描述

4

0 回答 0