COLA框架的状态机组件是一种轻量级的、无状态的、基于注解的状态机实现,可以方便地管理订单等业务对象的状态转换。COLA框架的状态机使用了连贯接口(Fluent Interfaces)来定义状态和事件,以及对应的动作和检查。COLA框架的状态机是COLA 4.0应用架构的一部分,旨在控制复杂度,提高开发效率。
COLA框架的状态机的优势有以下几点:
我的配置:
Maven: 3.6.3
SDK: 17
Language level:8
引入依赖:
<dependency>
<groupId>com.alibaba.colagroupId>
<artifactId>cola-component-statemachineartifactId>
<version>4.3.1version>
dependency>
StateMachineBuilder<States, Events, Context> builder = StateMachineBuilderFactory.create();
//external transition
builder.externalTransition()
.from(States.STATE1)
.to(States.STATE2)
.on(Events.EVENT1)
.when(checkCondition())
.perform(doAction());
//internal transition
builder.internalTransition()
.within(States.STATE2)
.on(Events.INTERNAL_EVENT)
.when(checkCondition())
.perform(doAction());
//external transitions
builder.externalTransitions()
.fromAmong(States.STATE1, States.STATE2, States.STATE3)
.to(States.STATE4)
.on(Events.EVENT4)
.when(checkCondition())
.perform(doAction());
builder.build(machineId);
StateMachine<States, Events, Context> stateMachine = StateMachineFactory.get(machineId);
stateMachine.showStateMachine();
StateMachineBuilder方法 | 说明 |
---|---|
ExternalTransitionBuilder |
用于一个流转的构建器 |
ExternalTransitionsBuilder |
用于多个流转的构建器 |
InternalTransitionBuilder |
开始构建内部流转 |
StateMachine |
对状态机开始构建,并在StateMachineFactory中注册 |
StateMachine方法 | 说明 |
---|---|
boolean verify(S sourceStateId, E event) | 验证一个事件E是否可以从当前状态S触发 |
S fireEvent(S sourceState, E event, C ctx) | 向状态机发送一个事件E,触发状态机,并返回目标状态 |
String getMachineId() | 获取状态机的标识符MachineId |
void showStateMachine() | 使用访问者模式来显示状态机的结构 |
// 订单状态(States)
enum OrderState {
INIT,
PAID,
DELIVERED,
REFUNDED;
}
// 订单事件(Events)
enum OrderEvent {
PAY_SUCCESS,
PAY_FAIL,
DELIVERED_SUCCESS,
REFUND_SUCCESS;
}
// 订单(Context)
static class Order {
String operator = "frank";
String orderId = "123465";
}
static String MACHINE_ID = "orderStateMachine";
外部过程描述:起始状态INIT,结束状态PAID,当发生PAY_SUCCESS时执行状态流转,当满足checkCondition()时,执行doAction,执行成功则返回状态PAID,否则返回INIT
@Test
public void testExternalNormal(){
// 第一步:生成一个状态机builder
StateMachineBuilder<OrderState, OrderEvent, Order> builder = StateMachineBuilderFactory.create();
// 第二步:设置一个外部状态转移类型的builder,并设置from\to\on\when\perform
builder.externalTransition()
.from(OrderState.INIT)
.to(OrderState.PAID)
.on(OrderEvent.PAY_SUCCESS)
.when(checkCondition())
.perform(doAction());
// 第三步:设置状态机的id,并在StateMachineFactory中的stateMachineMap进行注册
StateMachine<OrderState, OrderEvent, Order> stateMachine = builder.build(MACHINE_ID);
// 第四步:触发状态机
OrderState target = stateMachine.fireEvent(OrderState.INIT, OrderEvent.PAY_SUCCESS, new Order());
assertEquals(OrderState.PAID, target);
}
private Condition<Order> checkCondition() {
return order -> {
System.out.println("Check condition : " + order);
return true;
};
}
方法体中使用了lambda表达式来创建一个Condition的实例,并返回它
private Action<OrderState, OrderEvent, Order> doAction() {
return (from, to, event, ctx) -> {
System.out.println(
ctx.operator + " is operating " + ctx.orderId + " from:" + from + " to:" + to + " on:" + event));
};
}
这段代码定义了一个私有方法,返回一个Action对象,该对象是一个函数式接口,接受四个参数:from, to, event, ctx,并执行一些逻辑。
这个Action对象的逻辑是打印出一条信息,显示操作者、实体ID、源状态、目标状态和事件。例如:
frank is operating 123456 from:INIT to:PAID on:PAY_SUCCESS
这样可以方便地跟踪和调试状态机的运行情况。
内部过程描述:这个内部转换发生在PAID状态下,当发生DELIVERED_SUCCESS时执行状态流转,当满足checkCondition()时,执行doAction,执行成功则返回状态PAID
@Test
public void testInternalNormal(){
StateMachineBuilder<OrderState, OrderEvent, Order> builder = StateMachineBuilderFactory.create();
builder.internalTransition()
.within(OrderState.PAID)
.on(OrderEvent.DELIVERED_SUCCESS)
.when(checkCondition())
.perform(doAction());
StateMachine<OrderState, OrderEvent, Order> stateMachine = builder.build(MACHINE_ID + "1");
OrderState target = stateMachine.fireEvent(OrderState.PAID, OrderEvent.DELIVERED_SUCCESS, new Order());
assertEquals(OrderState.PAID, target);
}
外部过程描述:起始状态PAID或DELIVERED,结束状态REFUNDED,当发生REFUND_SUCCESS时执行状态流转,当满足checkCondition()时,执行doAction,执行成功则返回状态REFUNDED,否则返回对应起始状态
@Test
public void testExternalTransitionsNormal(){
StateMachineBuilder<OrderState, OrderEvent, Order> builder = StateMachineBuilderFactory.create();
builder.externalTransitions()
.fromAmong(OrderState.PAID, OrderState.DELIVERED)
.to(OrderState.REFUNDED)
.on(OrderEvent.REFUND_SUCCESS)
.when(checkCondition())
.perform(doAction());
StateMachine<OrderState, OrderEvent, Order> stateMachine = builder.build(MACHINE_ID + "2");
OrderState target = stateMachine.fireEvent(OrderState.PAID, OrderEvent.REFUND_SUCCESS, new Order());
assertEquals(OrderState.REFUNDED, target);
}
@Test
public void testExternalInternalNormal(){
StateMachineBuilder<OrderState, OrderEvent, Order> builder = StateMachineBuilderFactory.create();
builder.externalTransition()...
builder.internalTransition()...
builder.externalTransition()...
builder.externalTransitions()...
StateMachine<OrderState, OrderEvent, Order> stateMachine = builder.build(MACHINE_ID + "3");
}