Springboot 集成 SpringState 状态机

Springboot 集成 SpringState 状态机

  • 1.SpringState 简介
  • 2.状态机示例
    • 2.1 项目结构和依赖包
    • 2.2 定义事件类和状态类
    • 2.3 Spring 事件监听器
    • 2.4 状态机持久化类
      • 2.4.1 Redis 状态机持久化容器
      • 2.4.2 Redis 配置
      • 2.4.3 状态机监听器
    • 2.5 装机器容器
    • 2.6 状态机事件发送器
    • 2.7 状态机配置
    • 2.8 接口类
    • 2.9 实现类
    • 2.10 状态机上下文
    • 2.11 配置文件
  • 3.状态机测试
    • 3.1创建订单
    • 3.2持久化结果
    • 3.3 支付订单
    • 3.4 发货
    • 3.5 确认收货

1.SpringState 简介

状态机核心概念​​

项目 说明
状态(State)​​ 对象生命周期中的特定条件(如订单的待支付、已发货)
事件(Event)​​ 触发状态转换的动作(如支付成功、取消订单)
转换(Transition)​​ 定义事件如何驱动状态迁移(如待支付 → 支付事件 → 待发货)
守卫(Guard)​​ 条件检查,决定是否允许转换(如“仅未超时订单可支付”)
​​动作(Action)​​ 条件检查,决定是否允许转换(如“仅未超时订单可支付”)

应用场景

  • 订单生命周期管理​​
    管理订单从创建到完成的完整流程(如待支付 → 待发货 → 已完成)

  • 工作流引擎​​
    审批流程的状态控制(如提交 → 审核中 → 已批准)

  • ​​游戏状态流转​​
    角色状态切换(如空闲 → 战斗 → 死亡)

  • 物联网设备监控​​
    设备状态跟踪(如离线 → 在线 → 故障)

2.状态机示例

2.1 项目结构和依赖包

Springboot 集成 SpringState 状态机_第1张图片


<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.0modelVersion>

    <groupId>org.examplegroupId>
    <artifactId>spring-state-martifactId>
    <version>1.0-SNAPSHOTversion>

    <properties>
        <maven.compiler.source>21maven.compiler.source>
        <maven.compiler.target>21maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <spring-boot.version>3.5.3spring-boot.version>
    properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>${spring-boot.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.statemachinegroupId>
            <artifactId>spring-statemachine-starterartifactId>
            <version>4.0.0version>
        dependency>
        
        <dependency>
            <groupId>org.springframework.statemachinegroupId>
            <artifactId>spring-statemachine-data-redisartifactId>
            <version>4.0.0version>
        dependency>
    dependencies>
project>

启动类

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@EnableAsync
@SpringBootApplication
public class SpringStateMachine {
   
   
    public static void main(String[] args) {
   
   
        SpringApplication.run(SpringStateMachine.class, args);
    }
}

2.2 定义事件类和状态类

事件用于驱动状态转移,状态用于记录事件进度

事件类

package org.example.common;

/**
 * @Author zhx && moon
 * @Since 21
 * @Date 2025-06-18 PM 6:38
 */
public enum OrderEvent {
   
   
    PAY,            // 支付操作
    SHIP,           // 发货操作
    CONFIRM,        // 确认收货
    CANCEL          // 取消订单
}

状态类

package org.example.common;

/**
 * @Author zhx && moon
 * @Since 21
 * @Date 2025-06-18 PM 6:37
 */
public enum OrderState {
   
   
    UNPAID,         // 待支付
    PAID,           // 已支付
    SHIPPED,        // 已发货
    CONFIRMED,      // 已确认收货
    CANCELLED       // 已取消
}

2.3 Spring 事件监听器

Spring 事件监听器,用于异步处理事件流,当状态机结束时,推送当前状态机到监听器,监听器则从持久化中删除该状态机

package org.example.config;

import org.example.entity.OrderSMContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @Author zhx && moon
 * @Since 21
 * @Date 2025-06-20 AM 10:18
 */
@Component
public class AsyncEventListener {
   
   

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    SMContainer smContainer;

    @Async
    @EventListener
    public void handleAsyncEvent(OrderSMContext context) {
   
   
        logger.info("order id {} has delete {}", context.getOrderId(), smContainer.delete(context.getOrderId()));
    }

}

2.4 状态机持久化类

利用 Redis 做状态机的持久化存储

2.4.1 Redis 状态机持久化容器

package org.example.config;

import org.example.common.OrderEvent;
import org.example.common.OrderState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.

你可能感兴趣的:(JavaWeb,服务框架,spring,boot,java,spring,state,machine,状态机,jdk21)