我正在尝试执行SWF工作流程。我遇到了关于Promise
对象状态的问题。我的代码结构如下:
WorkflowClientImpl.java中的方法:
@Override
public void doSomething() {
new TryCatch() {
@Override
protected void doTry() throws Throwable {
System.out.println("Workflow Started");
Promise<SomeObject> someObject = activityClient.doAction(param1);
if(someObject.isready()) {
boolean reDo = shouldRestartWorkflow(someObject);
if(reDo) {
Promise<Void> timer = decisionContextProvider.getDecisionContext().getWorkflowClock()
.createTimer(TimeUnit.MINUTES.toSeconds(5));
continueAsNew(timer, param1);
}
}
}
@Override
protected void doCatch(Throwable e) throws Throwable {
System.err.printlnt("Error occured while workflow");
throw new RuntimeException(e);
}
};
}
@Asynchronous
private boolean shouldRestartWorkflow(@Wait Promise<SomeObject> someObject) {
if(someObject.get().getVariable() > 1)
return true;
return false;
}
@Asynchronous
public void continueAsNew(Promise<Void> timer, String param1) {
selfClient.execute(param1);
// SelfClient is instance of TempWorkflowSelfClient
}
上述代码应该在满足某些条件时重新启动工作流程。条件取决于activity方法返回的SomeObject实例中填充的值。但是,代码shouldRestartWorkflow
似乎永远不会被调用。
我试着为此写一个单元测试。以下是代码:
@Before
public void setUp() throws Exception {
trace = new ArrayList<String>();
// Register activity implementation to be used during test run
TempActivitiesImpl activitiesImpl = new TempActivitiesImpl(null, null) {
@Override
public SomeObject doAction(String randomString) {
trace.add("Test Case - " + randomString);
SomeObject testObject = new SomeObject();
testObject.setVariable(true);
return testObject;
}
};
workflowTest.addActivitiesImplementation(activityImpl); //Instance to activity class
workflowTest.addWorkflowImplementationType(WorkflowImpl.class);
}
@Test
public void testWorkflowExecutionCall() throws Throwable {
WorkflowClient workflow = workflowFactory.getClient("RandomString");
Promise<Void> promise = workflow.execute("RandomString");
List<String> expected = new ArrayList<String>();
expected.add("Test Case - RandomString");
AsyncAssert.assertEqualsWaitFor("Unexpected Result", expected, trace, promise);
}
上述测试用例有效。但是,如果我要删除if(someObject.isready())
条件。我收到错误IllegalStateException: Not Ready
。我能够确定在尝试执行shouldRestartWorkflow()
调用时发生错误。
我做错了吗?据我所知,shouldRestartWorkflow()
应该等到SomeObject被填充并由activity方法返回,然后继续。
答案 0 :(得分:0)
SWF注释未正确设置。由于这个问题,@ Asynchronous无法正常工作。
将AspectJ添加为Java代理
在Linux,OS X或Unix上使用:
-javaagent:/your_path/aspectj/lib/aspectjweaver.jar
在Windows上,请使用标准的Windows样式路径:
-javaagent:C:\your_path\aspectj\lib\aspectjweaver.jar
要为AWS Flow Framework for Java配置AspectJ,请将aop.xml文件添加到项目中。
<aspectj> <aspects> <!-- declare two existing aspects to the weaver --> <aspect name="com.amazonaws.services.simpleworkflow.flow.aspectj.AsynchronousAspect"/> <aspect name="com.amazonaws.services.simpleworkflow.flow.aspectj.ExponentialRetryAspect"/> </aspects> <weaver options="-verbose"> <include within="<replaceable>MySimpleWorkflow.*</replaceable>"/> </weaver> </aspectj>
值取决于您如何命名项目的包。上面的示例假定项目的包遵循模式MySimpleWorkflow。*。使用适合您自己项目包的值。