本文首发于我的网站: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面试题、我的成长和自我管理等主题,为读者提供一线开发者的工做和成长经验,期待你能在这里有所收获。