StateMachineBuilder 计时器崩溃
Crash on StateMachineBuilder timer
我用SpringStateMachineBuilder
创建了一个Bean。
在机器中,我有一个每秒重复一次的 Action
。
这是要演示的最小生成器代码:
@Bean
public StateMachine<State, Event> getStateMachine() throws Exception {
Action action = s -> {};
Builder<State, Event> builder = StateMachineBuilder.builder();
builder.configureConfiguration()
.withConfiguration()
.autoStartup(true)
.taskExecutor(new SyncTaskExecutor())
.taskScheduler(new ConcurrentTaskScheduler());
builder.configureStates()
.withStates()
.initial(State.STARTING)
.state(State.ACTION, action, null)
.end(State.DONE)
.states(EnumSet.allOf(State.class));
builder.configureTransitions()
.withExternal()
.source(State.STARTING).target(State.ACTION).event(Event.START)
.and().withInternal()
.source(State.ACTION)
.action(action)
.timer(1000)
.and().withExternal()
.source(State.ACTION).target(State.DONE).event(Event.STOP);
return builder.build();
}
无法创建 bean,出现此错误:
Caused by: java.lang.NullPointerException
at org.springframework.statemachine.trigger.TimerTrigger.doStart(TimerTrigger.java:51)
at org.springframework.statemachine.support.LifecycleObjectSupport.start(LifecycleObjectSupport.java:120)
at org.springframework.statemachine.support.DefaultStateMachineExecutor.registerTriggerListener(DefaultStateMachineExecutor.java:415)
at org.springframework.statemachine.support.DefaultStateMachineExecutor.<init>(DefaultStateMachineExecutor.java:116)
at org.springframework.statemachine.support.AbstractStateMachine.onInit(AbstractStateMachine.java:258)
at org.springframework.statemachine.support.LifecycleObjectSupport.afterPropertiesSet(LifecycleObjectSupport.java:67)
at org.springframework.statemachine.config.ObjectStateMachineFactory.buildStateMachineInternal(ObjectStateMachineFactory.java:79)
at org.springframework.statemachine.config.AbstractStateMachineFactory.buildMachine(AbstractStateMachineFactory.java:547)
at org.springframework.statemachine.config.AbstractStateMachineFactory.getStateMachine(AbstractStateMachineFactory.java:193)
at org.springframework.statemachine.config.StateMachineBuilder$Builder.build(StateMachineBuilder.java:128)
如果我删除 withInternal() [...] .timer(1000)
,它可以工作,但我失去了重复功能。
我的生成器代码有什么问题?
我认为这是 spring 状态机中的错误。只有当您使用 StateMachineBuilder 而不是使用 springboot 配置您的 StateMachine 时才会发生这种情况。
在 AbstractStateMachineFactory.java 中,它在创建 TimerTrigger 的新实例时不会在 TimerTrigger 上设置 taskScheduler。我能够通过添加来解决这个问题:
AbstractStateMachineFactory.java : 547
if( taskScheduler != null )
{
t.setTaskScheduler(taskScheduler);
}
我在此处向项目提交了拉取请求 #178。
我用SpringStateMachineBuilder
创建了一个Bean。
在机器中,我有一个每秒重复一次的 Action
。
这是要演示的最小生成器代码:
@Bean
public StateMachine<State, Event> getStateMachine() throws Exception {
Action action = s -> {};
Builder<State, Event> builder = StateMachineBuilder.builder();
builder.configureConfiguration()
.withConfiguration()
.autoStartup(true)
.taskExecutor(new SyncTaskExecutor())
.taskScheduler(new ConcurrentTaskScheduler());
builder.configureStates()
.withStates()
.initial(State.STARTING)
.state(State.ACTION, action, null)
.end(State.DONE)
.states(EnumSet.allOf(State.class));
builder.configureTransitions()
.withExternal()
.source(State.STARTING).target(State.ACTION).event(Event.START)
.and().withInternal()
.source(State.ACTION)
.action(action)
.timer(1000)
.and().withExternal()
.source(State.ACTION).target(State.DONE).event(Event.STOP);
return builder.build();
}
无法创建 bean,出现此错误:
Caused by: java.lang.NullPointerException
at org.springframework.statemachine.trigger.TimerTrigger.doStart(TimerTrigger.java:51)
at org.springframework.statemachine.support.LifecycleObjectSupport.start(LifecycleObjectSupport.java:120)
at org.springframework.statemachine.support.DefaultStateMachineExecutor.registerTriggerListener(DefaultStateMachineExecutor.java:415)
at org.springframework.statemachine.support.DefaultStateMachineExecutor.<init>(DefaultStateMachineExecutor.java:116)
at org.springframework.statemachine.support.AbstractStateMachine.onInit(AbstractStateMachine.java:258)
at org.springframework.statemachine.support.LifecycleObjectSupport.afterPropertiesSet(LifecycleObjectSupport.java:67)
at org.springframework.statemachine.config.ObjectStateMachineFactory.buildStateMachineInternal(ObjectStateMachineFactory.java:79)
at org.springframework.statemachine.config.AbstractStateMachineFactory.buildMachine(AbstractStateMachineFactory.java:547)
at org.springframework.statemachine.config.AbstractStateMachineFactory.getStateMachine(AbstractStateMachineFactory.java:193)
at org.springframework.statemachine.config.StateMachineBuilder$Builder.build(StateMachineBuilder.java:128)
如果我删除 withInternal() [...] .timer(1000)
,它可以工作,但我失去了重复功能。
我的生成器代码有什么问题?
我认为这是 spring 状态机中的错误。只有当您使用 StateMachineBuilder 而不是使用 springboot 配置您的 StateMachine 时才会发生这种情况。
在 AbstractStateMachineFactory.java 中,它在创建 TimerTrigger 的新实例时不会在 TimerTrigger 上设置 taskScheduler。我能够通过添加来解决这个问题:
AbstractStateMachineFactory.java : 547
if( taskScheduler != null )
{
t.setTaskScheduler(taskScheduler);
}
我在此处向项目提交了拉取请求 #178。