2

I have a spring statemachine (version 3.0.1) with an own instance for each user. With every HTTP request the statemachine is acquired by a DefaultStateMachineService from a JpaStateMachineRepository. At the end of the HTTP request the statemachine is released again. The state machine is configured to use a StateMachineRuntimePersister.

At one special state of the statemachine there is a transition triggered by a timer:

.withExternal()
    .source( "S1" )
    .target( "S2" )
    .timerOnce( 60000 )

I want this timer to be executed only if the statemachine is still in state S1 after one minute (another cloud instance handling a later step of this statemachine instance could have changed the state in the meantime, making the timer obsolete).

  1. I tried to release the statemachine without stopping it (stateMachineService.releaseStateMachine( machineId, false );). Then the timer always triggers after one minute, because it does not refresh the statemachine from the database leading to an inconsistent state.
  2. I tried to release the statemachine stopped. Then the timer is killed and will not trigger anymore.

One solution could be to switch the trigger from timerOnce to event and to use a separate mechanism (e.g. Quartz scheduler) that reads the statemachine from database, checks the state and then explicitly fires the event. But this would make the timer feature of spring statemachine useless in a cloud environment, so I guess there should be a better solution.

What would be the best solution here?

tangens
  • 39,095
  • 19
  • 120
  • 139

1 Answers1

1

The problem you mentioned can be solved using Distributed States.

So, Spring Statemachine uses ZooKeeper to keep states of machines consistent in the app instances. In your case, ZooKeeper will let your state machine know if its state changes on the other app instance and behave accordingly with the timer.

It's worth mentioning that the feature is not mature as of now according to docs:

Distributed state functionality is still a preview feature and is not yet considered to be stable in this particular release. We expect this feature to mature towards its first official release.

You can find some technical details of the concept in the Distributed State Machine Technical Paper

Personally speaking, I would stick with the solution you described (triggering event fire with a scheduler) since it gives more control over the process.

Dauren D
  • 134
  • 1
  • 4