本文首發於我的網站:Spring Boot 2.x實戰之StateMachinephp
Spring StateMachine是一個狀態機框架,在Spring框架項目中,開發者能夠經過簡單的配置就能得到一個業務狀態機,而不須要本身去管理狀態機的定義、初始化等過程。今天這篇文章,咱們經過一個案例學習下Spring StateMachine框架的用法。java
假設在一個業務系統中,有這樣一個對象,它有三個狀態:草稿、待發布、發佈完成,針對這三個狀態的業務動做也比較簡單,分別是:上線、發佈、回滾。該業務狀態機以下圖所示。git
接下來,基於上面的業務狀態機進行Spring StateMachine的演示。github
<?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 }
@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()); } }
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」,這裏應該是狀態機設置初始狀態的時候觸發的。spring
如上面的實戰過程所示,使用Spring StateMachine的步驟以下:apache
爲了將狀態變動的操做都統一管理起來,咱們會考慮在項目中引入狀態機,這樣其餘的業務模塊就和狀態轉移模塊隔離開來了,其餘業務模塊也不會糾結於當前的狀態是什麼,應該作什麼操做。在應用狀態機實現業務需求時,關鍵是業務狀態的分析,只要狀態機設計得沒問題,具體的實現能夠選擇用Spring StateMachine,也能夠本身去實現一個狀態機。後端
使用Spring StateMachine的好處在於本身無需關心狀態機的實現細節,只須要關心業務有什麼狀態、它們之間的轉移規則是什麼、每一個狀態轉移後真正要進行的業務操做。框架
本文完整實例參見:https://github.com/duqicauc/Spring-Boot-2.x-In-Action/tree/master/statemachinedemomaven
本號專一於後端技術、JVM問題排查和優化、Java面試題、我的成長和自我管理等主題,爲讀者提供一線開發者的工做和成長經驗,期待你能在這裏有所收穫。