28. Monitoring State Machine

StateMachineMonitor can be used to get more information about durations of how long transitions and actions takes to execute. Below you can see how this interface is implemented.

public class TestStateMachineMonitor extends AbstractStateMachineMonitor<String, String> {

    @Override
    public void transition(StateMachine<String, String> stateMachine, Transition<String, String> transition, long duration) {
    }

    @Override
    public void action(StateMachine<String, String> stateMachine, Action<String, String> action, long duration) {
    }
}

Once you have StateMachineMonitor implementation it can be added to a state machine via configuration as shown below.

@Configuration
@EnableStateMachine
public class Config1 extends StateMachineConfigurerAdapter<String, String> {

    @Override
    public void configure(StateMachineConfigurationConfigurer<String, String> config)
            throws Exception {
        config
            .withMonitoring()
                .monitor(stateMachineMonitor());
    }

    @Override
    public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
        states
            .withStates()
                .initial("S1")
                .state("S2");
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
        transitions
            .withExternal()
                .source("S1")
                .target("S2")
                .event("E1");
    }

    @Bean
    public StateMachineMonitor<String, String> stateMachineMonitor() {
        return new TestStateMachineMonitor();
    }
}
[Tip]Tip

Check sample Chapter 49, Monitoring for detailed usage.