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()動作,如其轉換中定義的那樣。 –