0

Since I must support concurrent access to the state machine, I can't use the usual Autowired singleton approach. Instead, I am trying to build the state machine as described in the docs, here:

http://docs.spring.io/spring-statemachine/docs/1.0.0.BUILD-SNAPSHOT/reference/htmlsingle/#state-machine-via-builder

Here is my very modest code:

    Builder<SessionState, SessionEvent> builder = StateMachineBuilder.builder();
    builder.configureStates().withStates().initial(SessionState.INITIAL).states(EnumSet.allOf(SessionState.class));
    StateMachine<SessionState, SessionEvent> stateMachine = builder.build();
    stateMachine.start();
    System.out.println(stateMachine.getState());  // null !!!

This fails immediately, as the state of the machine is surprisingly null, whereas I expect SessionState.INITIAL.

What am I doing wrong here??

I've also tried exactly the code shown in the docs, namely:

    Builder<String, String> builder = StateMachineBuilder.builder();
    builder.configureStates().withStates().initial("SI").end("SF").states(new HashSet<String>(Arrays.asList("S1", "S2", "S3", "S4")));
    final StateMachine<String, String> stateMachine = builder.build();
    stateMachine.start();
    System.out.println(stateMachine.getState());

.... same result.

Marc
  • 1,812
  • 4
  • 23
  • 36

1 Answers1

1

That example was a very simplified to get the idea. You need to configure transitions for config not to be ill-formed. You need to use builder.configureTransitions() which returns same configuration interface than the annotation based config model. Check from http://docs.spring.io/spring-statemachine/docs/1.0.0.M3/reference/htmlsingle/#configuring-transitions howto do it.

It's on my todo list to add a config verifier so that you'd fail fast when machine is build if config is wrong or not complete.

Also some examples in tests https://github.com/spring-projects/spring-statemachine/blob/master/spring-statemachine-core/src/test/java/org/springframework/statemachine/config/ManualBuilderTests.java

Janne Valkealahti
  • 2,552
  • 1
  • 14
  • 12
  • 2
    OK, I see it now. The trick to make it work was this line: builder.configureConfiguration().withConfiguration().autoStartup(true).taskExecutor(new SyncTaskExecutor()) .taskScheduler(new ConcurrentTaskScheduler()); – Marc Aug 05 '15 at 07:21