天天看點

Spring Statemachine使用入門1、程式設計式2、java類配置式

1、程式設計式

下面以官網的示例稍加改造舉例來體會一下Spring Statemachine使用。

添加下面maven坐标到pom中。

<dependencies>
    <dependency>
        <groupId>org.springframework.statemachine</groupId>
        <artifactId>spring-statemachine-core</artifactId>
        <version>2.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <!-- 對應spring-statemachine-core 2.1.3.RELEASE -->
        <version>5.1.5.RELEASE</version>
    </dependency>
</dependencies>
           

下面的示例說明如何配置和使用statemachine。假設我們有狀态STATE1,STATE2和事件EVENT1,EVENT2。

Spring Statemachine使用入門1、程式設計式2、java類配置式
enum States {
    STATE1, STATE2
}

enum Events {
    EVENT1, EVENT2
}

public static StateMachine<States, Events> buildMachine() throws Exception {
    StateMachineBuilder.Builder<States, Events> builder = StateMachineBuilder.builder();
    //設定狀态機初始狀态為STATE1、共有EnumSet.allOf(States.class)種狀态
    builder.configureStates()
            .withStates()
            .initial(States.STATE1)
            .states(EnumSet.allOf(States.class));
    //設定狀态轉移 原始狀态STATE1觸發EVENT1事件狀态轉移為STATE2、原始狀态STATE2觸發EVENT2事件狀态轉移為STATE1
    builder.configureTransitions()
            .withExternal()
            .source(States.STATE1).target(States.STATE2)
            .event(Events.EVENT1)
            .and()
            .withExternal()
            .source(States.STATE2).target(States.STATE1)
            .event(Events.EVENT2);
    //配置狀态機監聽器,可監聽多種事件、具體看接口StateMachineListener方法
    builder.configureConfiguration()
            .withConfiguration()
            .listener(new StateMachineListenerAdapter<States, Events>() {
                /**
                 * 狀态變更後調用此方法
                 */
                @Override
                public void stateChanged(State<States, Events> from, State<States, Events> to) {
                    System.out.println("State change to " + to.getId());
                }
            });
    return builder.build();
}

public static void main(String[] args) throws Exception {
    //建構狀态機對象
    StateMachine<States, Events> stateMachine = buildMachine();
    //初始狀态為STATE1、觸發stateChanged方法列印State change to STATE1
    stateMachine.start();
    //列印State change to STATE2
    stateMachine.sendEvent(Events.EVENT1);
    //列印State change to STATE1
    stateMachine.sendEvent(Events.EVENT2);
}
           

執行結果:

Spring Statemachine使用入門1、程式設計式2、java類配置式

2、java類配置式

public class JavaConfigExample {

    static enum States {
        STATE1, STATE2
    }

    static enum Events {
        EVENT1, EVENT2
    }

    @Configuration
    @EnableStateMachine
    static class Config1 extends EnumStateMachineConfigurerAdapter<States, Events> {

        @Override
        public void configure(StateMachineStateConfigurer<States, Events> states)
                throws Exception {
            states
                    .withStates()
                    .initial(States.STATE1)
                    .states(EnumSet.allOf(States.class));
        }

        @Override
        public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
                throws Exception {
            transitions
                    .withExternal()
                    .source(States.STATE1).target(States.STATE2)
                    .event(Events.EVENT1)
                    .and()
                    .withExternal()
                    .source(States.STATE2).target(States.STATE1)
                    .event(Events.EVENT2);
        }
    }

    @WithStateMachine
    static class MyBean {
        @OnTransition(target = "STATE1")
        void toState1() {
        }
        @OnTransition(target = "STATE2")
        void toState2() {
        }
    }

    static class MyApp {
        @Autowired
        StateMachine<States, Events> stateMachine;

        void doSignals() {
            stateMachine.start();
            stateMachine.sendEvent(Events.EVENT1);
            stateMachine.sendEvent(Events.EVENT2);
        }
    }
}