【发布时间】:2015-08-30 10:42:13
【问题描述】:
我是 AWS SWF 的新手,我的任务是仅在第一次尝试活动失败时发出通知(通过电子邮件)。我使用Settable<Boolean> 作为我的标志,但该值不可靠,因为工作流正在异步运行。这是我的代码:
final AsyncExecutor asyncExecutor = new AsyncRetryingExecutor(retryPolicy, workflowClock);
final Settable<Boolean> notifyException = new Settable<>();
new TryCatch() {
@Override
protected void doTry() throws Throwable {
asyncExecutor.execute(() -> new TryCatch() {
@Override
protected void doTry() throws Throwable {
Promise<ActivityOne> activityOne = activityOneClient.performAction(activityOneRequest);
}
@Override
protected void doCatch(Throwable e) throws Throwable {
if (!notifyException.isReady()) {
// PERFORM notification service here!!
notifyException.set(true);
} else {
// DO NOTHING. Notification is already sent the first time :)
}
throw e;
}
});
}
@Override
protected void doCatch(Throwable e) throws Throwable {
System.out.println("======");
System.out.println("CATCH ALL!! " + e.getMessage());
System.out.println("======");
}
};
notifyException 的值始终在变化,即使我在 if statement 内将其值明确设置为 true。我的代码的结果是它将执行超过 1 个notification service。
====更新===
final AsyncExecutor asyncExecutor = new AsyncRetryingExecutor(retryPolicy, workflowClock);
final Settable<Boolean> notifyException = new Settable<>();
new TryCatch() {
@Override
protected void doTry() throws Throwable {
asyncExecutor.execute(() -> new TryCatch() {
@Override
protected void doTry() throws Throwable {
Promise<ActivityOne> activityOne = activityOneClient.performAction(activityOneRequest);
}
@Override
protected void doCatch(Throwable e) throws Throwable {
if (!notifyException.isReady()) {
activityOneClient.notify();
notifyException.set(true);
}
throw e;
}
});
}
@Override
protected void doCatch(Throwable e) throws Throwable {
System.out.println("======");
System.out.println("CATCH ALL!! " + e.getMessage());
System.out.println("======");
}
};
当我重新抛出异常时,activityOneClient.notify() 只会在asyncExecutor 的最后一次重试时执行,所以如果我不重新抛出,activityOneClient.notify() 将自动执行。我只需要在第一次发生异常时通知。
【问题讨论】:
标签: amazon-web-services spring-boot amazon-swf