我正在尝试使用 spring-statemachine 构建分层状态机。它应该有两个正交状态,每个状态代表两个服务的状态。为简单起见,以下代码减少了状态数,但仍会发生相同的错误。
public enum MachineState {
BUFF,BUFF_OFFLINE, BUFF_ONLINE,
CB,CB_OFFLINE,CB_ONLINE
}
public enum MachineEvent {
BUFF_OFF,BUFF_ON,
CB_OFF, CB_NORESP, BUFF_NORESP, CB_ON
}
@Configuration
@EnableStateMachine
public class StateMachineConfig extends EnumStateMachineConfigurerAdapter<MachineState, MachineEvent> {
@Override
public void configure(final StateMachineConfigurationConfigurer<MachineState, MachineEvent> config)
throws Exception {
config
.withConfiguration()
.autoStartup(true);
}
@Override
public void configure(final StateMachineStateConfigurer<MachineState, MachineEvent> states)
throws Exception {
states
.withStates()
.initial(MachineState.BUFF)
.and()
.withStates()
.parent(MachineState.BUFF)
.initial(MachineState.BUFF_OFFLINE)
.state(MachineState.BUFF_ONLINE)
.and()
.withStates()
.initial(MachineState.CB)
.and()
.withStates()
.parent(MachineState.CB)
.initial(MachineState.CB_OFFLINE)
.state(MachineState.CB_ONLINE)
.and()
;
}
@Override
public void configure(final StateMachineTransitionConfigurer<MachineState, MachineEvent> transitions)
throws Exception {
transitions
.withExternal()
.source(MachineState.BUFF_OFFLINE).target(MachineState.BUFF_ONLINE)
.event(MachineEvent.BUFF_ON)
.and()
.withExternal()
.source(MachineState.BUFF_ONLINE).target(MachineState.BUFF_OFFLINE)
.event(MachineEvent.BUFF_OFF)
.and()
.withExternal()
.source(MachineState.CB_OFFLINE).target(MachineState.CB_ONLINE)
.event(MachineEvent.CB_ON)
.and()
.withExternal()
.source(MachineState.CB_ONLINE).target(MachineState.CB_OFFLINE)
.event(MachineEvent.CB_OFF)
.and()
.withInternal()
.source(MachineState.CB)
.event(MachineEvent.CB_NORESP)
.and()
.withInternal()
.source(MachineState.BUFF)
.event(MachineEvent.BUFF_NORESP)
.and()
;
}
}
首先,我在配置中做错了吗?
我得到的错误如下
Caused by: java.lang.IllegalArgumentException: Source must be set
at org.springframework.util.Assert.notNull(Assert.java:115) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.statemachine.transition.AbstractTransition.<init>(AbstractTransition.java:63) ~[spring-statemachine-core-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at org.springframework.statemachine.transition.AbstractInternalTransition.<init>(AbstractInternalTransition.java:35) ~[spring-statemachine-core-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at org.springframework.statemachine.transition.DefaultInternalTransition.<init>(DefaultInternalTransition.java:35) ~[spring-statemachine-core-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at org.springframework.statemachine.config.AbstractStateMachineFactory.buildMachine(AbstractStateMachineFactory.java:704) ~[spring-statemachine-core-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at org.springframework.statemachine.config.AbstractStateMachineFactory.getStateMachine(AbstractStateMachineFactory.java:189) ~[spring-statemachine-core-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at org.springframework.statemachine.config.AbstractStateMachineFactory.getStateMachine(AbstractStateMachineFactory.java:126) ~[spring-statemachine-core-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at org.springframework.statemachine.config.configuration.StateMachineConfiguration$StateMachineDelegatingFactoryBean.afterPropertiesSet(StateMachineConfiguration.java:154) ~[spring-statemachine-core-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
我调试了应用程序,发现在 spring-statemachine-core AbstractStateMachineFactory buildMachine() 中,stateMap 缺少 CB 和 BUFF 状态之一。最奇怪的部分是哪个接缝是随机的,有时它实际上包含整个集合,我也不例外。
我尝试删除内部转换并调试代码,发现即使 stateMap 是不完整的(如果我从丢失的状态转换它会失败),实例化后的状态机看起来完全符合我的要求,所有那里说。
有任何想法吗?
示例项目https://www.dropbox.com/s/qlarppnma0dq9ai/statemachineerror.tar.gz?dl=0