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); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpfszz.html