開發與維運

Spring boot 2.x 整合Spring-Statemachine

狀態機簡介

狀態機是一種用來進行對象行為建模的工具,其作用主要是描述對象在它的生命週期內所經歷的狀態序列,以及如何響應來自外界的各種事件,摘錄自這個大神的文章狀態機引擎選型

示例說明

網上能找到的示例大多都過於複雜,所以寫了一個簡單的示例下面的代碼定義了一個極簡的狀態機,僅包含兩個狀態(上班,下班),一個事件(通勤),通過web頁面操作添加狀態機及狀態轉換,集成了spring-statemachine-data-jpa進行持久化.

SpringBoot2.x配置說明

  • Pom.xml
<dependency>
            <groupId>org.springframework.statemachine</groupId>
            <artifactId>spring-statemachine-starter</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.statemachine</groupId>
            <artifactId>spring-statemachine-data-jpa</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

java配置

  • 狀態機持久化
@Configuration
public class StateMachineJpaConfig {
    /**
     * StateMachineRuntimePersister為狀態機運行時持久化配置
     * @param jpaStateMachineRepository
     * @return
     */
    @Bean
    public StateMachineRuntimePersister<String, String, String> stateMachineRuntimePersister(
            JpaStateMachineRepository jpaStateMachineRepository) {
        return new JpaPersistingStateMachineInterceptor<>(jpaStateMachineRepository);
    }
    /**
     * StateMachineService為狀態狀態機持久化控制,用於獲取或關閉狀態機
     * @param stateMachineFactory
     * @param stateMachineRuntimePersister
     * @return
     */
    @Bean
    public StateMachineService<String, String> stateMachineService(
            StateMachineFactory<String, String> stateMachineFactory,
            StateMachineRuntimePersister<String, String, String> stateMachineRuntimePersister) {
        return new DefaultStateMachineService<String, String>(stateMachineFactory, stateMachineRuntimePersister);
    }
}
  • 狀態機監聽 繼承StateMachineListenerAdapter重寫需監聽的方法即可,通過方法名可推測監聽的事件,就不貼代碼了.
  • 狀態機配置,@EnableStateMachine為單例模式並不適合此示例所以使用@EnableStateMachineFactory
@Configuration
@EnableStateMachineFactory
public class StateMachineConfig extends StateMachineConfigurerAdapter<String, String> {
    @Autowired
    private StateMachineRuntimePersister<String, String, String> stateMachineRuntimePersister;
    @Autowired
    private StateMachineLogListener logListener;
    @Override
    public void configure(StateMachineConfigurationConfigurer<String, String> config)
            throws Exception {
        config
                .withPersistence()
                .runtimePersister(stateMachineRuntimePersister)
                .and()
                .withConfiguration().listener(logListener);
    }
    @Override
    public void configure(StateMachineStateConfigurer<String, String> states)
            throws Exception {
        states
                .withStates()
                .initial("宿舍")
                .state("公司");
    }
    @Override
    public void configure(StateMachineTransitionConfigurer<String, String> transitions)
            throws Exception {
        transitions
                .withExternal()
                .source("宿舍").target("公司")
                .event("通勤")
                .and()
                .withExternal()
                .source("公司").target("宿舍")
                .event("通勤");
    }
}

示例代碼

https://gitee.com/MeiJM/spring-cram/tree/master/statemachine

參考資料

https://docs.spring.io/spring-statemachine/docs/2.2.0.RELEASE/reference/#preface

https://segmentfault.com/a/1190000009906317

Leave a Reply

Your email address will not be published. Required fields are marked *