工作流的代碼是異步的。因此從活動返回的Promise從未準備好在相同的回調中。因此,在你的代碼violations.isReady()總是返回假是正確的行爲。 你必須使用註解爲@Asynchronous或任務來回調關聯到無極的方法。所以,你的代碼應該是這樣的:
public class WorkflowImpl implements Workflow {
ActivitiesClient activities;
public Workflow(/*...*/) {
// ...
}
@Override
public void do(WorkflowInput input) {
Promise<List<String>> violations = activities.validate(input);
processValidationResult(violations);
}
@Asynchronous
private void processValidationResult(Promise<List<String>> violations) {
// As method is @Asynchronous framework guarantees violations is ready
// when its body is executed.
if (!CollectionUtils.isEmpty(violations.get())) {
return; // or throw ValidationException
}
// do other stuff
}
}
注意,AspectJ的should be configured correctly與流程框架方面的@Asynchronous註解生效。
另一種選擇是直接使用任務:
public class WorkflowImpl implements Workflow {
ActivitiesClient activities;
public Workflow(/*...*/) {
// ...
}
@Override
public void do(WorkflowInput input) {
Promise<List<String>> violations = activities.validate(input);
new Task(violations) {
@Override
public void doExecute() {
// Framework guarantees violations is ready
// (as it is Task constructor parameter)
// when execute method is executed.
if (!CollectionUtils.isEmpty(violations.get())) {
return; // or throw ValidationException
}
// do other stuff
}
};
}
}
我會建議通過SWF Recipes和samples閱讀來獲取有關編寫SWF工作流程時使用的模式更好的主意。
另外,還要確保你通過TryCatchFinally javadoc作爲AWS流程框架的錯誤處理,同時非常強大的是大多數人都習慣完全不同。
加到顯示例如從processValidationResult(...)返回值:
@Override
public void do(WorkflowInput input) {
Promise<List<String>> violations = activities.validate(input);
Promise<String> whatever = processValidationResult(violations);
processNextStep(whatever);
}
@Asynchronous
private Promise<String> processValidationResult(Promise<List<String>> violations) {
// As method is @Asynchronous framework guarantees violations is ready
// when its body is executed.
if (!CollectionUtils.isEmpty(violations.get())) {
throw new ValidationException(...);
}
// do other stuff
return Promise.asPromise("result string");
}
@Asynchronous
private void processNextStep(Promise<String> whatever) {
...
}
在你的榜樣,從異步方法返回不會導致工作流程方法的回報。我通讀了食譜和示例,但找不到一個異步方法返回的東西,並且該值在工作流中使用。 – Chaos
從異步方法您可以返回void或Promise。我爲我的帖子添加了一個示例。 –
現在這一切都很有意義。我通過使用@Asynchronous方法來獲得它的工作。嵌套的@ @Asynchronous方法似乎是處理回調的適當技術。 – Chaos