spring-statemachine - 通过从动作触发事件来触发状态机,它不会移动到其他状态
问题描述
public class PaymentMachineConfig extends StateMachineConfigurerAdapter<PaymentState, PaymentEvent> {
@Override
public void configure(StateMachineStateConfigurer<PaymentState, PaymentEvent> states) throws Exception {
states.withStates()
.initial(PaymentState.NEW)
.state(PaymentState.VALIDATE)
.state(PaymentState.DEBIT)
.state(PaymentState.CREDIT)
.state(PaymentState.DEBIT_FAIL)
.state(PaymentState.CREDIT_FAIL)
.end(PaymentState.COMPLETE);
}
@Override
public void configure(StateMachineTransitionConfigurer<PaymentState, PaymentEvent> transitions) throws Exception {
transitions.withExternal().source(PaymentState.NEW).target(PaymentState.VALIDATE).event(PaymentEvent.NEW_PAYMENT).action(action1())
.and()
.withExternal().source(PaymentState.VALIDATE).target(PaymentState.DEBIT).event(PaymentEvent.DEBITED).action(action2())
.and()
.withExternal().source(PaymentState.DEBIT).target(PaymentState.CREDIT).event(PaymentEvent.CREDITED).action(action3())
.and()
.withExternal().source(PaymentState.CREDIT).target(PaymentState.COMPLETE).event(PaymentEvent.COMPLETED).action(action4())
.and()
.withExternal().source(PaymentState.DEBIT).target(PaymentState.DEBIT_FAIL).event(PaymentEvent.DEBIT_DECLINED)
.and()
.withExternal().source(PaymentState.CREDIT).target(PaymentState.CREDIT_FAIL).event(PaymentEvent.CREDIT_DECLINED);
}
private Action<PaymentState, PaymentEvent> action1() {
return stateContext->stateContext.getStateMachine().sendEvent(PaymentEvent.NEW_PAYMENT);
}
private Action<PaymentState, PaymentEvent> action2() {
return stateContext->stateContext.getStateMachine().sendEvent(PaymentEvent.DEBITED);
}
private Action<PaymentState, PaymentEvent> action3() {
return stateContext->stateContext.getStateMachine().sendEvent(PaymentEvent.CREDITED);
}
private Action<PaymentState, PaymentEvent> action4() {
return stateContext->stateContext.getStateMachine().sendEvent(PaymentEvent.COMPLETED);
}
@Override
public void configure(StateMachineConfigurationConfigurer<PaymentState, PaymentEvent> config) throws Exception {
StateMachineListenerAdapter adapter=new StateMachineListenerAdapter<PaymentState, PaymentEvent>(){
@Override
public void stateChanged(State from, State to) {
log.info(String.format("stateChanged(from:%s,to:%s)",from,to));
}
};
config.withConfiguration().listener(adapter);
}
}
我通过传递 NEW_PAYMENT 事件来运行上面的代码,但它只移动了 2 个状态而不经过其他状态。
完整代码请参考我的github
解决方案
推荐阅读
- linux - 如何为Android构建【openSSL-1.0.2u】,如果可能,如何合并Android Makefile?
- apache-spark - 如何在 spark scala 中更快地处理 .gz 文件?
- slack - Slack 传入 webhook 中的确认按钮
- ios - 如何在 iOS SWIFT 中显示来自 AWSS3 存储桶的完整图像?
- php - 谷歌日历 API 与 PHP 的问题
- node.js - 使用 fluent-ffmpeg 在 nodejs 上处理 ffmpeg 命令
- scala - 编译器会尽可能将实例方法的调用优化为静态方法吗?
- ios - 我可以通过 iOS Today Widget 在我的包含应用程序中调用 func 吗?
- entity-framework - 从 Entity Framework Core 2.2 升级到 3+(包含许多问题)
- javascript - 如何防止在 Web 浏览器的登录页面中保存凭据