Spring State Machine Transitions

时间:2017-05-15 13:33:43

标签: java spring state-machine spring-statemachine

我试图触发来自其他转换的转换来制作自动状态机(SM)但它不起作用:初始动作(findCustomer())应该改变SM的初始状态,然后每个动作触发一个事件,等到最后的状态。 这是我的配置:

    @Configuration @EnableStateMachine public class CardStateMachineConfig extends EnumStateMachineConfigurerAdapter<CardStates, CardEvents> {


    final private static Logger logger = LoggerFactory.getLogger(CardStateMachineConfig.class);

    @Override
    public void configure(StateMachineStateConfigurer<CardStates, CardEvents> states) throws Exception {
        states.withStates().initial(CardStates.ACTIVE_STATUS).state(CardStates.ACTIVE_STATUS, findCustomer(), null)
                .states(EnumSet.allOf(CardStates.class));

    }

    // @formatter:off
    @Override
    public void configure(StateMachineTransitionConfigurer<CardStates, CardEvents> transitions) throws Exception {




         transitions.withExternal().source(CardStates.ACTIVE_STATUS).target(CardStates.CUSTOMER_FOUND).event(CardEvents.FIND_CUSTOMER).action(findCustomer())
                 .and().withExternal()
         .source(CardStates.CUSTOMER_FOUND).target(CardStates.MOBILECARD_FOUND).event(CardEvents.CUSTOMER_FOUND).action(findMobileCard())
                 .and().withExternal()
         .source(CardStates.FIND_MOBILECARD_FOUND).target(CardStates.WIRECARD_LOCKED_CARD).event(CardEvents.MOBILE_CARD_FOUND).action(wirecardLockCard());


    }

    // @formatter:on
    @Override public void configure(StateMachineConfigurationConfigurer<CardStates, CardEvents> config)
            throws Exception {
        config.withConfiguration().autoStartup(true).listener(new MobileCardBlockProcessEventHandler());
    }

    @Bean public Action<CardStates, CardEvents> findCustomer() {

        return (context) -> {
            context.getStateMachine().sendEvent(CardEvents.CUSTOMER_FOUND);
        };

    }

    @Bean public Action<CardStates, CardEvents> findMobileCard() {

        return (context) -> {
            context.getStateMachine().sendEvent(CardEvents.MOBILE_CARD_FOUND);
        };
    }
}

1 个答案:

答案 0 :(得分:1)

CardEvents.FIND_CUSTOMERCardStates.ACTIVE_STATUS的活动CardStates.CUSTOMER_FOUND。从findCustomer()发送与CardEvents.CUSTOMER_FOUND不同的事件CardEvents.FIND_CUSTOMER

改变这一点,我认为它应该有效。想一想,我个人更喜欢通过匿名过渡和选择状态来驱动automatic机器。