How to use Spring State Machine on the dynamic events

448 Views Asked by At

I would like to build a work flow where the state transitions are based on the contents of input events, which means the event can't be defined statically like enum. All input events have the same event type.

The most examples of Spring state machine I saw use enum or String as the event. I ran some tests like the following:

transitions.withExternal().source("s1").target("s2").action(ctx -> {
    // here, ctx.getEvent() returns null
})

transitions.withExternal().source("s1").target("s2").event("static_event").action(ctx -> {
    // here, ctx.getEvent() returns "static_event"
})

In my case, i need to retrieve the event passed in at runtime, then in action lambda expression, parse the event to get some keys which determine if the state should be changed into another. So I expect something like the following:

transitions.withExternal().source("s1").target("s2").action(ctx -> {
        // i expect ctx.getEvent() returns MyEvent(...)
})

stateMachine.sendEvent(new MyEvent(...));

So how can I use Spring state machine to model this kind of flow?

0

There are 0 best solutions below