spring-statemachine icon indicating copy to clipboard operation
spring-statemachine copied to clipboard

Support for Reactive Interceptors or Reactive Operations inside Interceptors

Open vedantshetty opened this issue 2 years ago • 0 comments

I have been playing around with the state machine and have managed to utilise it to run as a workflow management system, where the state machine would persist the extended state to a database after a state transition using a preStateChange hook

    private class StateChangeInterceptor extends StateMachineInterceptorAdapter<S, E> {

        @Override
        public void preStateChange(State<S, E> state, Message<E> message,
                                   Transition<S, E> transition, StateMachine<S, E> stateMachine,
                                   StateMachine<S, E> rootStateMachine) {
          var data = getData(state, message);
           reactiveRepo.save(data).subscribe();
        }
    }

However, I noticed that the DB write needs to be blocking otherwise actions which are supposed to run after state change tend to trigger

        .state(ServiceRequestState.APPROVED,  triggerActions())

// This will run before the state change leading to weird issues like the state context being in an inconsistent stage
  private Collection<? extends Action<WorkflowState, WorkflowEvent>> triggerActions() {
    return List.of(
        Actions.errorCallingAction(notifyCustomer, handleNotificationFailure),
        Actions.errorCallingAction(logAnalytics, loggingFailure)
    );
  }

Currently my way around this is to provide an indication of the DB write being completed using the Workflow Extended State but would love to understand if there was a better way

vedantshetty avatar Oct 11 '24 02:10 vedantshetty