Can Spring State machine support multiple initial States and parallel executions (multi-threaded environment)?

173 Views Asked by At

We've recently integrated the Spring State Machine into our workflow. Here's how we're using it: We're receiving events from an SQS queue, and then we're processing these events using the state machine. We have different states in our state machine, and after transitioning to the next target stage, we're also pushing the event back to the SQS queue for persistence. For each event in the SQS queue, we initialize the state machine and execute the necessary transitions.

When it comes to executing the Spring State Machine, we're wondering if it's possible for the process to start at multiple initial stages and then halt after pushing the event to the SQS queue. Is the state machine capable of this kind of partial execution, or does it need to run through all stages in sequence?

We've noticed that Spring State Machines appear to be singular, which implies that only one event can be consumed from the SQS queue at a time, and the corresponding state machine is executed for that event. Is there any way to handle multiple events concurrently? For instance, could we create a new state machine instance each time an event is triggered, allowing us to process multiple events simultaneously?

Here is my code:

Configuring the states and transitions:

public void configure(StateMachineStateConfigurer<RedactionStates, RedactionEvents> states)
    throws Exception {
states.withStates()
        .initial(RedactionStates.S1) // default initial state
        .state(RedactionStates.S2)
        .state(RedactionStates.S3)
        .state(RedactionStates.S4)
        .end(RedactionStates.S5);
}

Transitions:

public void configure(
    StateMachineTransitionConfigurer<RedactionStates, RedactionEvents> transitions)
    throws Exception {
transitions
        .withInternal()
        .source(RedactionStates.S1)
        .event(RedactionEvents.E1)
        .and()
        .withExternal()
        .source(RedactionStates.S1)
        .target(RedactionStates.S2)
        .event(RedactionEvents.E2)
       }
0

There are 0 best solutions below