0
我试图从其他触发器转换到自动状态机(SM),但它不工作:初始操作(findCustomer())应该改变SM的初始状态,然后每个操作触发事件等等到最终状态。 这里是我的配置:Spring State Machine Transitions
@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);
};
}
}
出了什么问题?请提供[最小,完整且可验证的示例](https://stackoverflow.com/help/mcve)。 – Obenland
1- SM使用初始状态CardStates.ACTIVE_STATUS实例化,但不触发findCustomer()操作。所以没有任何反应:SM不会进化。 2-当我通过使用来自测试类的cardStateMachine.sendEvent(CardEvents.FIND_CUSTOMER)来触发事件CardEvents.FIND_CUSTOMER时,调用findCustomer()动作+ SM状态更改,但是context.getStateMachine()。sendEvent(CardEvents。 CUSTOMER_FOUND);没有考虑到:它应该调用findMobileCard()动作,如其转换中定义的那样。 –