我正在评估 Spring Statemachine,并想了解如何从转换错误中恢复。
我为过渡期间执行的操作定义了错误操作。的执行S1_TO_S2_ACTION
会导致在 中处理的异常S1_TO_S2_ERROR_HANDLING
。
我可以处理操作中的错误,但如何从错误中恢复?我试图在错误处理程序 ( context.getStateMachine().sendEvent(Events.RECOVER)
) 中发送一个事件,但没有任何效果。
@Configuration
@EnableStateMachine
class StateMachineConfig
extends EnumStateMachineConfigurerAdapter<States, Events> {
Action<States, Events> S1_TO_S2_ERROR_HANDLING = context -> {
System.out.println(BO + " ERROR!!!");
System.out.println("E: " + context.getException());
System.out.println("S: " + context.getSource().getId());
System.out.println("T: " + context.getTarget().getId());
};
@Override
public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
throws Exception {
transitions
.withExternal()
.source(States.SI).target(States.S1).event(Events.E1)
.action(TRANS_ACTION)
.and()
.withExternal()
.source(States.S1).target(States.S2).event(Events.E2)
.action(S1_TO_S2_ACTION, S1_TO_S2_ERROR_HANDLING)
.and()
.withExternal()
.source(States.S2).target(States.SE).event(Events.E3)
.action(TRANS_ACTION);
}
}
令人惊讶的调用stateMachine.hasStateMachineError()
之后返回false
。
我可以在错误操作中从错误中恢复谁?如果在转换期间抛出异常,为什么会hasStateMachineError()
返回?false