首页 > 解决方案 > 通过从动作触发事件来触发状态机,它不会移动到其他状态

问题描述

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

标签: spring-statemachine

解决方案


推荐阅读