使用Spring StateMachine框架实现状态机

This commit is contained in:
翟永超
2016-12-19 00:02:58 +08:00
parent cfe10f8b8f
commit 311243bbc5
7 changed files with 209 additions and 0 deletions
@@ -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);
}
}
@@ -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("用户已收货,订单完成");
}
}
@@ -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;
// }
// }
//
// };
// }
}
@@ -0,0 +1,7 @@
package com.didispace;
public enum States {
UNPAID, // 待支付
WAITING_FOR_RECEIVE, // 待收货
DONE // 结束
}
@@ -0,0 +1,2 @@
spring.application.name=statemachine