无法在Activity Worker Class中捕获异常

时间:2014-10-18 18:23:44

标签: amazon-ec2 amazon-swf

我有一些活动,他们可以抛出一些自定义异常。问题是我无法捕获Activity Worker类中抛出的异常,并且正在显示堆栈跟踪。我试图抓住Throwable但没有用,因为堆栈跟踪再次显示出来。

活动实施类

public class TestActivitiesImpl implements TestActivities{

    @Override
    public Integer testAct1() {
        System.out.println("Activity 1 ---->Start");
        int count = 0;
        while(count < 1000){
            count ++;
        }
        return 1;
    }

    @SuppressWarnings("unused")
    @Override
    public Integer testAct2() throws MyException {
        System.out.println("Activity 2 ---->Start");
        if(true){
            throw new MyException("Failed to execute the activity");
        }
        return 1;
    }
}

活动工人类

public class TestActivitiesWorker {
    public static void main(String[] args) {

        try {
            ClientConfiguration config = new ClientConfiguration().withSocketTimeout(70*1000);

            String swfAccessId = "ABCDXYZ";
            String swfSecretKey = "ABCDXYZ";
            AWSCredentials awsCredentials = new BasicAWSCredentials(swfAccessId, swfSecretKey);

            AmazonSimpleWorkflow service = new AmazonSimpleWorkflowClient(awsCredentials, config);
            service.setEndpoint("ABCDXYZ");

            String domain = "EC2-TEST";

            ActivityWorker aw = new ActivityWorker(service, domain, "TestList");
            aw.addActivitiesImplementation(new TestActivitiesImpl());
            aw.start();

        }catch (Throwable e) {
            System.out.println("Failed to execute Job");
            System.out.println(e.getMessage());
        }
    }
}

堆栈跟踪

2014年10月18日下午2:02:53

com.amazonaws.services.simpleworkflow.flow.worker.SynchronousActivityTaskPoller execute
    SEVERE: Failure processing activity task with taskId=12, workflowGenerationId=AsyncTest, activity={Name: TestActivities.testAct2,Version: 1.2}, activityInstanceId=2
    com.amazonaws.services.simpleworkflow.flow.ActivityFailureException: Failed to execute the activity : ["com.myapp.test.MyException",{"cause":null,"stackTrace":[{"methodName":"testAct2","fileName":"TestActivitiesImpl.java","lineNumber":20,"className":"com.myapp.test.TestActivitiesImpl","nativeMethod":false},{"methodName":"invoke0","fileName":"NativeMethodAccessorImpl.java","lineNumber":-2,"className":"sun.reflect.NativeMethodAccessorImpl","nativeMethod":true},{"methodName":"invoke","fileName":"NativeMethodAccessorImpl.java","lineNumber":57,"className":"sun.reflect.NativeMethodAccessorImpl","nativeMethod":false},{"methodName":"invoke","fileName":"DelegatingMethodAccessorImpl.java","lineNumber":43,"className":"sun.reflect.DelegatingMethodAccessorImpl","nativeMethod":false},{"methodName":"invoke","fileName":"Method.java","lineNumber":606,"className":"java.lang.reflect.Method","nativeMethod":false},{"methodName":"execute","fileName":"POJOActivityImplementation.java","lineNumber":64,"className":"com.amazonaws.services.simpleworkflow.flow.pojo.POJOActivityImplementation","nativeMethod":false},{"methodName":"execute","fileName":"ActivityImplementationBase.java","lineNumber":46,"className":"com.amazonaws.services.simpleworkflow.flow.generic.ActivityImplementationBase","nativeMethod":false},{"methodName":"execute","fileName":"SynchronousActivityTaskPoller.java","lineNumber":196,"className":"com.amazonaws.services.simpleworkflow.flow.worker.SynchronousActivityTaskPoller","nativeMethod":false},{"methodName":"run","fileName":"ActivityTaskPoller.java","lineNumber":92,"className":"com.amazonaws.services.simpleworkflow.flow.worker.ActivityTaskPoller$2","nativeMethod":false},{"methodName":"runWorker","fileName":"ThreadPoolExecutor.java","lineNumber":1145,"className":"java.util.concurrent.ThreadPoolExecutor","nativeMethod":false},{"methodName":"run","fileName":"ThreadPoolExecutor.java","lineNumber":615,"className":"java.util.concurrent.ThreadPoolExecutor$Worker","nativeMethod":false},{"methodName":"run","fileName":"Thread.java","lineNumber":724,"className":"java.lang.Thread","nativeMethod":false}],"message":"Failed to execute the activity","localizedMessage":"Failed to execute the activity","suppressed":["[Ljava.lang.Throwable;",[]]}]
    at com.amazonaws.services.simpleworkflow.flow.pojo.POJOActivityImplementation.throwActivityFailureException(POJOActivityImplementation.java:110)
    at com.amazonaws.services.simpleworkflow.flow.pojo.POJOActivityImplementation.execute(POJOActivityImplementation.java:67)
    at com.amazonaws.services.simpleworkflow.flow.generic.ActivityImplementationBase.execute(ActivityImplementationBase.java:46)
    at com.amazonaws.services.simpleworkflow.flow.worker.SynchronousActivityTaskPoller.execute(SynchronousActivityTaskPoller.java:196)
    at com.amazonaws.services.simpleworkflow.flow.worker.ActivityTaskPoller$2.run(ActivityTaskPoller.java:92)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

1 个答案:

答案 0 :(得分:1)

它的行为与设计一致。如果活动实现方法抛出异常,则活动工作程序不会失败。抛出的异常将传递给工作流实现,并且必须在那里处理。它在ActivityTaskFailedEvent的详细信息字段中的工作流程历史记录中也可见。