我想使用 timerOnce 来延迟,但它不起作用
I want to use timerOnce for delay but it doesnt work
我想从状态 REPEATED_EXPORT_TO_A 到状态 EXPORT_TO_A_AGAIN 有延迟但行动 reexportEvent() 尚未执行。我有以下状态机:
public StateMachineBuilder.Builder<StatusId, ActionId> construct(StateMachineBuilder.Builder<StatusId, ActionId> builder) throws Exception {
builder.configureStates().withStates()
.states(ImmutableSet.of(OBTAINED_BY_B, FOR_EXPORT_TO_A, EXPORTING_TO_A,
EXPORT_TO_A_ERROR, EXPORTING_TO_A_TIMEOUT,
RECEIVED_BY_A, NOT_RECEIVED_BY_A, RECEIVED_A_ERROR, REPEATED_EXPORT_TO_A))
.state(FOR_EXPORT_TO_A, checkPassedAction(), null)
.state(EXPORTING_TO_A, exportedAction(), null)
.choice(EXPORTED_TO_A_OR_NOT)
.choice(EXPORT_TO_A_AGAIN);
builder.configureTransitions().withExternal()
.source(OBTAINED_BY_B).target(FOR_EXPORT_TO_A)
.and().withExternal()
.source(FOR_EXPORT_TO_A).target(EXPORTED_TO_A_OR_NOT)
.and().withChoice()
.source(EXPORTED_TO_A_OR_NOT)
.first(EXPORTING_TO_A, exportingToAGuardSsm)
.last(REPEATED_EXPORT_TO_A)
.and().withExternal()
.source(REPEATED_EXPORT_TO_A)
.target(EXPORT_TO_A_AGAIN)
.event(REEXPORT_TO_A)
.and().withChoice()
.source(EXPORT_TO_A_AGAIN)
.first(EXPORTING_TO_A, exportingToAGuardSsm)
.then(EXPORT_TO_A_ERROR, checkRepeatExportGuard)
.then(REPEATED_EXPORT_TO_A, repeatExportToBGuardSsm)
.last(EXPORT_TO_A_ERROR)
延迟的内部转换
.and().withInternal()
.source(REPEATED_EXPORT_TO_A)
.action(reexportEvent())
.timerOnce(5000)
但是这个动作还没有执行
private Action<StatusId, ActionId> reexportEvent() {
//some code
return context -> {
Doc doc = SsmUtil.getDoc(context);
doc.setRepeatCount(doc.getRepeatCount() + 1);
context.getStateMachine().sendEvent(REEXPORT_TO_A);
};
}
对于 Spring 状态机中的触发器,有一件关键的事情需要了解 - 它们与状态相关联,您需要保持在该状态才能执行触发器。延迟从您进入该状态的那一刻开始 - 如果您在达到延迟时间之前退出该状态,则触发器不会执行。
示例:
为状态 "A" 指定触发器,延迟 5 秒
进入状态"A"
在第二次退出状态"A"
触发器将不会执行,因为您在定义的时间延迟到期之前已退出状态 "A"。
检查演示此行为的单元测试 (TestTriggersDelay) here。
我想从状态 REPEATED_EXPORT_TO_A 到状态 EXPORT_TO_A_AGAIN 有延迟但行动 reexportEvent() 尚未执行。我有以下状态机:
public StateMachineBuilder.Builder<StatusId, ActionId> construct(StateMachineBuilder.Builder<StatusId, ActionId> builder) throws Exception {
builder.configureStates().withStates()
.states(ImmutableSet.of(OBTAINED_BY_B, FOR_EXPORT_TO_A, EXPORTING_TO_A,
EXPORT_TO_A_ERROR, EXPORTING_TO_A_TIMEOUT,
RECEIVED_BY_A, NOT_RECEIVED_BY_A, RECEIVED_A_ERROR, REPEATED_EXPORT_TO_A))
.state(FOR_EXPORT_TO_A, checkPassedAction(), null)
.state(EXPORTING_TO_A, exportedAction(), null)
.choice(EXPORTED_TO_A_OR_NOT)
.choice(EXPORT_TO_A_AGAIN);
builder.configureTransitions().withExternal()
.source(OBTAINED_BY_B).target(FOR_EXPORT_TO_A)
.and().withExternal()
.source(FOR_EXPORT_TO_A).target(EXPORTED_TO_A_OR_NOT)
.and().withChoice()
.source(EXPORTED_TO_A_OR_NOT)
.first(EXPORTING_TO_A, exportingToAGuardSsm)
.last(REPEATED_EXPORT_TO_A)
.and().withExternal()
.source(REPEATED_EXPORT_TO_A)
.target(EXPORT_TO_A_AGAIN)
.event(REEXPORT_TO_A)
.and().withChoice()
.source(EXPORT_TO_A_AGAIN)
.first(EXPORTING_TO_A, exportingToAGuardSsm)
.then(EXPORT_TO_A_ERROR, checkRepeatExportGuard)
.then(REPEATED_EXPORT_TO_A, repeatExportToBGuardSsm)
.last(EXPORT_TO_A_ERROR)
延迟的内部转换
.and().withInternal()
.source(REPEATED_EXPORT_TO_A)
.action(reexportEvent())
.timerOnce(5000)
但是这个动作还没有执行
private Action<StatusId, ActionId> reexportEvent() {
//some code
return context -> {
Doc doc = SsmUtil.getDoc(context);
doc.setRepeatCount(doc.getRepeatCount() + 1);
context.getStateMachine().sendEvent(REEXPORT_TO_A);
};
}
对于 Spring 状态机中的触发器,有一件关键的事情需要了解 - 它们与状态相关联,您需要保持在该状态才能执行触发器。延迟从您进入该状态的那一刻开始 - 如果您在达到延迟时间之前退出该状态,则触发器不会执行。
示例:
为状态 "A" 指定触发器,延迟 5 秒
进入状态"A"
在第二次退出状态"A"
触发器将不会执行,因为您在定义的时间延迟到期之前已退出状态 "A"。
检查演示此行为的单元测试 (TestTriggersDelay) here。