我想从状态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 State Machine中的触发器 - 它们与状态相关联,您需要保持该状态才能执行触发器。延迟从您进入状态开始 - 如果您在达到延迟时间之前退出状态,则触发器不会执行。
例:
触发器不会执行,因为您在定义的时间延迟到期之前退出了状态“A”。
签出一个单元测试(TestTriggersDelay)来演示这种行为here。