Spring Boot 2.x實戰之StateMachine

  • 2019 年 11 月 11 日
  • 筆記

本文首發於個人網站:Spring Boot 2.x實戰之StateMachine

Spring StateMachine是一個狀態機框架,在Spring框架項目中,開發者可以通過簡單的配置就能獲得一個業務狀態機,而不需要自己去管理狀態機的定義、初始化等過程。今天這篇文章,我們通過一個案例學習下Spring StateMachine框架的用法。

案例介紹

假設在一個業務系統中,有這樣一個對象,它有三個狀態:草稿、待發布、發布完成,針對這三個狀態的業務動作也比較簡單,分別是:上線、發布、回滾。該業務狀態機如下圖所示。

img

實戰

接下來,基於上面的業務狀態機進行Spring StateMachine的演示。

  • 創建一個基礎的Spring Boot工程,在主pom文件中加入Spring StateMachine的依賴:
<?xml version="1.0" encoding="UTF-8"?>  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <parent>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-parent</artifactId>      <version>2.2.1.RELEASE</version>      <relativePath/> <!-- lookup parent from repository -->    </parent>    <groupId>online.javaadu</groupId>    <artifactId>statemachinedemo</artifactId>    <version>0.0.1-SNAPSHOT</version>    <name>statemachinedemo</name>    <description>Demo project for Spring Boot</description>      <properties>      <java.version>1.8</java.version>    </properties>      <dependencies>      <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter</artifactId>      </dependency>        <dependency>        <groupId>org.projectlombok</groupId>        <artifactId>lombok</artifactId>        <optional>true</optional>      </dependency>      <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>        <scope>test</scope>        <exclusions>          <exclusion>            <groupId>org.junit.vintage</groupId>            <artifactId>junit-vintage-engine</artifactId>          </exclusion>        </exclusions>      </dependency>        <!--加入spring statemachine的依賴-->          <dependency>          <groupId>org.springframework.statemachine</groupId>          <artifactId>spring-statemachine-core</artifactId>          <version>2.1.3.RELEASE</version>        </dependency>    </dependencies>      <build>      <plugins>        <plugin>          <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-maven-plugin</artifactId>        </plugin>      </plugins>    </build>    </project>

定義狀態枚舉和事件枚舉,程式碼如下:

/**  * 狀態枚舉  **/  public enum States {      DRAFT,      PUBLISH_TODO,      PUBLISH_DONE,  }    /**  * 事件枚舉  **/  public enum Events {      ONLINE,      PUBLISH,      ROLLBACK  }
  • 完成狀態機的配置,包括:(1)狀態機的初始狀態和所有狀態;(2)狀態之間的轉移規則
@Configuration  @EnableStateMachine  public class StateMachineConfig extends EnumStateMachineConfigurerAdapter<States, Events> {        @Override      public void configure(StateMachineStateConfigurer<States, Events> states) throws Exception {          states.withStates().initial(States.DRAFT).states(EnumSet.allOf(States.class));      }        @Override      public void configure(StateMachineTransitionConfigurer<States, Events> transitions) throws Exception {          transitions.withExternal()              .source(States.DRAFT).target(States.PUBLISH_TODO)              .event(Events.ONLINE)              .and()              .withExternal()              .source(States.PUBLISH_TODO).target(States.PUBLISH_DONE)              .event(Events.PUBLISH)              .and()              .withExternal()              .source(States.PUBLISH_DONE).target(States.DRAFT)              .event(Events.ROLLBACK);      }  }
  • 定義一個測試業務對象,狀態機的狀態轉移都會反映到該業務對象的狀態變更上
@WithStateMachine  @Data  @Slf4j  public class BizBean {        /**       * @see States       */      private String status = States.DRAFT.name();        @OnTransition(target = "PUBLISH_TODO")      public void online() {          log.info("操作上線,待發布. target status:{}", States.PUBLISH_TODO.name());          setStatus(States.PUBLISH_TODO.name());      }        @OnTransition(target = "PUBLISH_DONE")      public void publish() {          log.info("操作發布,發布完成. target status:{}", States.PUBLISH_DONE.name());          setStatus(States.PUBLISH_DONE.name());      }        @OnTransition(target = "DRAFT")      public void rollback() {          log.info("操作回滾,回到草稿狀態. target status:{}", States.DRAFT.name());          setStatus(States.DRAFT.name());      }    }
  • 編寫測試用例,這裡我們使用CommandLineRunner介面代替,定義了一個StartupRunner,在該類的run方法中啟動狀態機、發送不同的事件,通過日誌驗證狀態機的流轉過程。
public class StartupRunner implements CommandLineRunner {        @Resource      StateMachine<States, Events> stateMachine;        @Override      public void run(String... args) throws Exception {          stateMachine.start();          stateMachine.sendEvent(Events.ONLINE);          stateMachine.sendEvent(Events.PUBLISH);          stateMachine.sendEvent(Events.ROLLBACK);      }  }

在運行上述程式後,我們可以在控制台中獲得如下輸出,我們執行了三個操作:上線、發布、回滾,在下圖中也確實看到了對應的日誌。不過我還發現有一個意料之外的地方——在啟動狀態機的時候,還列印出了一個日誌——「操作回滾,回到草稿狀態. target status:DRAFT」,這裡應該是狀態機設置初始狀態的時候觸發的。

image-20191110162618938

分析

如上面的實戰過程所示,使用Spring StateMachine的步驟如下:

  1. 定義狀態枚舉和事件枚舉
  2. 定義狀態機的初始狀態和所有狀態
  3. 定義狀態之間的轉移規則
  4. 在業務對象中使用狀態機,編寫響應狀態變化的監聽器方法

為了將狀態變更的操作都統一管理起來,我們會考慮在項目中引入狀態機,這樣其他的業務模組就和狀態轉移模組隔離開來了,其他業務模組也不會糾結於當前的狀態是什麼,應該做什麼操作。在應用狀態機實現業務需求時,關鍵是業務狀態的分析,只要狀態機設計得沒問題,具體的實現可以選擇用Spring StateMachine,也可以自己去實現一個狀態機。

使用Spring StateMachine的好處在於自己無需關心狀態機的實現細節,只需要關心業務有什麼狀態、它們之間的轉移規則是什麼、每個狀態轉移後真正要進行的業務操作。

本文完整實例參見:https://github.com/duqicauc/Spring-Boot-2.x-In-Action/tree/master/statemachinedemo

參考資料

  1. http://blog.didispace.com/spring-statemachine/
  2. https://projects.spring.io/spring-statemachine/#quick-start

本號專註於後端技術、JVM問題排查和優化、Java面試題、個人成長和自我管理等主題,為讀者提供一線開發者的工作和成長經驗,期待你能在這裡有所收穫。
javaadu