Mastering Order State Management with Spring State Machine
This guide demonstrates how to use Spring State Machine to elegantly manage order status changes by defining states, events, configuring transitions, and implementing listeners, providing Maven dependencies and Java code examples for building extensible, event‑driven workflows in backend applications.
In many real projects we encounter state‑change problems across multiple processes, such as order status transitions. When an event is triggered, the order state changes. To manage these event‑driven state changes with high extensibility, we can use Spring State Machine.
1. Understanding Spring State Machine
Spring State Machine, a module of the Spring Framework, provides an implementation of a finite state machine that describes a system’s transitions between states and the events that trigger those transitions. It offers a clear way to define states and transitions and handles event‑driven state migrations.
2. Practical Implementation of Order Status Changes
(1) Add Maven Dependencies
<code><dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.statemachine</groupId>
<artifactId>spring-statemachine-starter</artifactId>
<version>2.2.0.RELEASE</version>
</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>
</dependencies></code>(2) Define Order States
<code>public enum OrderStatusEnum {
WAIT_PAY(1,"待支付"),
WAIT_DELIVER_GOODS(3, "待发货"),
WAIT_RECEIVE_GOODS(5,"待收货"),
TRANSACTION_SUCCESS(7, "交易完成"),
;
private Integer status;
private String desc;
OrderStatusEnum(Integer status, String desc) {
this.status = status;
this.desc = desc;
}
public Integer getStatus() { return status; }
public String getDesc() { return desc; }
}</code>(3) Define Order Events
<code>public enum OrderEventEnum {
USER_PAY("用户支付"),
WAREHOUSE_DELIVER_GOODS("仓库发货"),
USER_RECEIVE_GOODS("用户收货"),
;
private String desc;
OrderEventEnum(String desc) { this.desc = desc; }
public String getDesc() { return desc; }
}</code>(4) Configure State‑Event Relationships
<code>@Configuration
@EnableStateMachine(name = "springStateMachineConfig")
public class SpringStateMachineConfig extends EnumStateMachineConfigurerAdapter<OrderStatusEnum, OrderEventEnum> {
@Override
public void configure(StateMachineStateConfigurer<OrderStatusEnum, OrderEventEnum> state) throws Exception {
state.withStates()
.initial(OrderStatusEnum.WAIT_PAY)
.states(Stream.of(OrderStatusEnum.values()).collect(Collectors.toSet()));
}
@Override
public void configure(StateMachineTransitionConfigurer<OrderStatusEnum, OrderEventEnum> transitions) throws Exception {
transitions
.withExternal()
.source(OrderStatusEnum.WAIT_PAY).target(OrderStatusEnum.WAIT_DELIVER_GOODS)
.event(OrderEventEnum.USER_PAY)
.and()
.withExternal()
.source(OrderStatusEnum.WAIT_DELIVER_GOODS).target(OrderStatusEnum.WAIT_RECEIVE_GOODS)
.event(OrderEventEnum.WAREHOUSE_DELIVER_GOODS)
.and()
.withExternal()
.source(OrderStatusEnum.WAIT_RECEIVE_GOODS).target(OrderStatusEnum.TRANSACTION_SUCCESS)
.event(OrderEventEnum.USER_RECEIVE_GOODS);
}
}</code>(5) Listen to Specific Events
<code>@Component
@WithStateMachine(name = "springStateMachineConfig")
public class SpringStateMachineEventListener {
@OnTransition(target="WAIT_PAY")
public boolean create(Message<OrderEntity> order) {
System.out.println("订单创建,待支付");
return true;
}
@OnTransition(source = "WAIT_PAY", target="WAIT_DELIVER_GOODS")
public boolean userPay(Message<OrderEntity> order) {
System.out.println("用户支付");
return true;
}
@OnTransition(source = "WAIT_DELIVER_GOODS", target="WAIT_RECEIVE_GOODS")
public boolean warehourseDeliver(Message<OrderEntity> order) {
System.out.println("仓库发货");
return true;
}
@OnTransition(source = "WAIT_RECEIVE_GOODS", target="TRANSACTION_SUCCESS")
public boolean userReceive(Message<OrderEntity> order) {
System.out.println("用户收货");
return true;
}
}</code>With these core pieces—state definitions, event definitions, configuration of their relationships, and listeners—we have a complete, extensible workflow for order status management. Spring State Machine also offers additional features such as actions and guards for more complex scenarios.
Summary: In workflow‑like business scenarios (e.g., order processing), binding states to events via Spring State Machine enables clean, event‑driven state transitions. Other mature state‑machine solutions (e.g., Cola) exist, and adding new states like order cancellation simply requires defining the new state, event, and listener.
Lobster Programming
Sharing insights on technical analysis and exchange, making life better through technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.