spring boot +spring stateMachine启蒙程序

提前准备
jar包:gradle

dependencies {

    compile(
            "org.springframework.boot:spring-boot-starter-web:${springBootVersion}",
            "org.springframework.statemachine:spring-statemachine-core:1.2.0.RELEASE",
    )
}

先定义state
state 就是程序中某事物运行时的各种状态

public  enum  States {
    ADD,
    MODIFY,
    CANCEL
}

再定义触发事件
event :改变状态的触发事件

public enum Events {
    PICK_UP,
    GIVE_UP,
    DROP
}

配置stateConfig
配置事件event、状态state的关系

@Configuration
@EnableStateMachine
public class StatesConfig extends EnumStateMachineConfigurerAdapter<States, Events> {

    @Override
    public void configure(StateMachineStateConfigurer<States, Events> Statess) throws Exception {
        Statess.withStates()
                .initial(States.ADD)
                .states(EnumSet.allOf(States.class));
    }

    //
    @Override
    public void configure(StateMachineConfigurationConfigurer<States, Events> config)
            throws Exception {
        config
                .withConfiguration()
                .autoStartup(true)
                .listener(listener());
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<States, Events> transitions) throws Exception {
        transitions
                .withExternal()
                    .source(States.ADD)
                    .target(States.MODIFY)
                    .event(Events.PICK_UP)
                    .and()
                .withExternal()
                    .source(States.ADD)
                    .target(States.CANCEL)
                    .event(Events.DROP)
                    .and()
                .withExternal()
                    .source(States.MODIFY)
                    .target(States.CANCEL)
                    .event(Events.GIVE_UP);
    }

    @Bean
    public StateMachineListener<States, Events> listener() {
        return new StateMachineListenerAdapter<States, Events>() {
            @Override
            public void stateChanged(State<States, Events> from, State<States, Events> to) {
                System.out.println("state change ... " + to.getId());
            }
        };
    }
}

触发事件

@Component
public class EventSend implements CommandLineRunner {

    @Autowired
    StateMachine<States, Events> stateMachine;

    //发送事件  
    @Override
    public void run(String... args) throws Exception {
        stateMachine.start();
        stateMachine.sendEvent(Events.PICK_UP);
        stateMachine.sendEvent(Events.GIVE_UP);
        stateMachine.sendEvent(Events.DROP);
    }
}

使用spring boot 启动项目

@SpringBootApplication
public class Application{

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

期望输出结果:

state change ... ADD
state change ... MODIFY
state change ... CANCEL
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值