使用Spring StateMachine框架实现状态机
This commit is contained in:
46
Chapter6-1-1/pom.xml
Normal file
46
Chapter6-1-1/pom.xml
Normal file
@@ -0,0 +1,46 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.didispace</groupId>
|
||||
<artifactId>statemachine</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>statemachine</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.4.2.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.statemachine</groupId>
|
||||
<artifactId>spring-statemachine-core</artifactId>
|
||||
<version>1.2.0.RELEASE</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
26
Chapter6-1-1/src/main/java/com/didispace/Application.java
Normal file
26
Chapter6-1-1/src/main/java/com/didispace/Application.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application implements CommandLineRunner {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private StateMachine<States, Events> stateMachine;
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
stateMachine.start();
|
||||
stateMachine.sendEvent(Events.PAY);
|
||||
stateMachine.sendEvent(Events.RECEIVE);
|
||||
}
|
||||
|
||||
}
|
||||
40
Chapter6-1-1/src/main/java/com/didispace/EventConfig.java
Normal file
40
Chapter6-1-1/src/main/java/com/didispace/EventConfig.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.statemachine.annotation.*;
|
||||
|
||||
/**
|
||||
* 该配置实现了com.didispace.StateMachineConfig类中定义的状态机监听器实现。
|
||||
*/
|
||||
@WithStateMachine
|
||||
public class EventConfig {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@OnTransition(target = "UNPAID")
|
||||
public void create() {
|
||||
logger.info("订单创建,待支付");
|
||||
}
|
||||
|
||||
@OnTransition(source = "UNPAID", target = "WAITING_FOR_RECEIVE")
|
||||
public void pay() {
|
||||
logger.info("用户完成支付,待收货");
|
||||
}
|
||||
|
||||
@OnTransitionStart(source = "UNPAID", target = "WAITING_FOR_RECEIVE")
|
||||
public void payStart() {
|
||||
logger.info("用户完成支付,待收货: start");
|
||||
}
|
||||
|
||||
@OnTransitionEnd(source = "UNPAID", target = "WAITING_FOR_RECEIVE")
|
||||
public void payEnd() {
|
||||
logger.info("用户完成支付,待收货: end");
|
||||
}
|
||||
|
||||
@OnTransition(source = "WAITING_FOR_RECEIVE", target = "DONE")
|
||||
public void receive() {
|
||||
logger.info("用户已收货,订单完成");
|
||||
}
|
||||
|
||||
}
|
||||
6
Chapter6-1-1/src/main/java/com/didispace/Events.java
Normal file
6
Chapter6-1-1/src/main/java/com/didispace/Events.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package com.didispace;
|
||||
|
||||
public enum Events {
|
||||
PAY, // 支付
|
||||
RECEIVE // 收货
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.statemachine.config.EnableStateMachine;
|
||||
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
|
||||
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
|
||||
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
|
||||
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
|
||||
import org.springframework.statemachine.listener.StateMachineListener;
|
||||
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
|
||||
import org.springframework.statemachine.transition.Transition;
|
||||
|
||||
import java.util.EnumSet;
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableStateMachine
|
||||
public class StateMachineConfig extends EnumStateMachineConfigurerAdapter<States, Events> {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineStateConfigurer<States, Events> states)
|
||||
throws Exception {
|
||||
states
|
||||
.withStates()
|
||||
.initial(States.UNPAID)
|
||||
.states(EnumSet.allOf(States.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
|
||||
throws Exception {
|
||||
transitions
|
||||
.withExternal()
|
||||
.source(States.UNPAID).target(States.WAITING_FOR_RECEIVE)
|
||||
.event(Events.PAY)
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(States.WAITING_FOR_RECEIVE).target(States.DONE)
|
||||
.event(Events.RECEIVE);
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public void configure(StateMachineConfigurationConfigurer<States, Events> config)
|
||||
// throws Exception {
|
||||
// config
|
||||
// .withConfiguration()
|
||||
// .listener(listener());
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// public StateMachineListener<States, Events> listener() {
|
||||
// return new StateMachineListenerAdapter<States, Events>() {
|
||||
//
|
||||
// @Override
|
||||
// public void transition(Transition<States, Events> transition) {
|
||||
// if(transition.getTarget().getId() == States.UNPAID) {
|
||||
// logger.info("订单创建,待支付");
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// if(transition.getSource().getId() == States.UNPAID
|
||||
// && transition.getTarget().getId() == States.WAITING_FOR_RECEIVE) {
|
||||
// logger.info("用户完成支付,待收货");
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// if(transition.getSource().getId() == States.WAITING_FOR_RECEIVE
|
||||
// && transition.getTarget().getId() == States.DONE) {
|
||||
// logger.info("用户已收货,订单完成");
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// };
|
||||
// }
|
||||
|
||||
}
|
||||
7
Chapter6-1-1/src/main/java/com/didispace/States.java
Normal file
7
Chapter6-1-1/src/main/java/com/didispace/States.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package com.didispace;
|
||||
|
||||
public enum States {
|
||||
UNPAID, // 待支付
|
||||
WAITING_FOR_RECEIVE, // 待收货
|
||||
DONE // 结束
|
||||
}
|
||||
2
Chapter6-1-1/src/main/resources/application.properties
Normal file
2
Chapter6-1-1/src/main/resources/application.properties
Normal file
@@ -0,0 +1,2 @@
|
||||
spring.application.name=statemachine
|
||||
|
||||
Reference in New Issue
Block a user