2016-01-06 52 views
0

我使用Spring StateMachineBuilder來創建一個Bean。StateMachineBuilder計時器崩潰

在機器中,我有一個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(); 
} 

不能創建豆,我得到這個錯誤:

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),它的工作原理,但我輸了重複功能。

我的構建器代碼有什麼問題?

回答

1

我相信這是一個在春天statemachine的錯誤。只有在使用StateMachineBuilder而不是使用springboot配置StateMachine時纔會發生這種情況。

在AbstractStateMachineFactory.java中,當它創建它的新實例時,它不會在TimerTrigger上設置taskScheduler。我能夠加入到解決這個問題:

AbstractStateMachineFactory.java : 547 

if(taskScheduler != null) 
{ 
    t.setTaskScheduler(taskScheduler); 
} 

我提交了拉要求項目在這裏#178