Mehrdad Bozorgmehr
Mehrdad Bozorgmehr

Reputation: 55

How to catch exceptions in the processing of actions In the Spring boot State Machine?

I created a custom listener for my state machines and overridden the StateMachineListenerAdapter in this configuration, but my exceptions in actions are not caught in this listener. This is my Listener-Configuration :

    @Slf4j
public class StateMachineListener extends StateMachineListenerAdapter<OnboardingState, OnboardingEvent> {
    @Override
    public void stateChanged(State<OnboardingState, OnboardingEvent> from, State<OnboardingState, OnboardingEvent> to) {
        log.info((String.format("Transitioned from %s to %s%n", from == null ? "none" : from.getId(), to.getId())));
    }

    @Override
    public void stateMachineError(StateMachine<OnboardingState, OnboardingEvent> stateMachine, Exception exception) {
//        super.stateMachineError(stateMachine, exception); doesn't catch occur there 
        log.error((exception == null ? "" : exception.getMessage()));
    }

    @Override
    public void eventNotAccepted(Message<OnboardingEvent> event) {
        super.eventNotAccepted(event);
        log.info("State change failed: {}", event.getPayload());
    }
}

And Also I mention this listener to state machine configuration:

@Configuration
@EnableStateMachineFactory(name = "onboardingStateMachine")
@RequiredArgsConstructor
public class OnboardingStateMachineConfiguration
    extends StateMachineConfigurerAdapter<OnboardingState, OnboardingEvent> {

    private final OnboardingStateMachineAction onboardingStateMachineAction;

    @Override
    public void configure(StateMachineConfigurationConfigurer<OnboardingState, OnboardingEvent> config)
        throws Exception {
        config
            .withConfiguration()
            .autoStartup(false)
            .listener(new StateMachineListener());
    }

    @Override
    public void configure(StateMachineStateConfigurer<OnboardingState, OnboardingEvent> states)
        throws Exception {
        states
            .withStates()
            .initial(INITIALIZING_ONBOARDING)
            .end(CUSTOMER_VERIFIED)
            .end(CUSTOMER_NOT_VERIFIED)
            .state(ACCOUNT_OPENED, onboardingStateMachineAction.completeOnboardingAction())
            .end(OnboardingState.COMPLETE_ONBOARDING)
            .states(EnumSet.allOf(OnboardingState.class));
    }
}

Upvotes: 0

Views: 40

Answers (0)

Related Questions