链接: 状态机和工作流的区别.
链接: 状态机引擎选型.
链接: 彻底搞懂Spring状态机原理,实现订单与物流解耦.
链接: spring statemachine收录.
链接: spring statemachine的企业可用级开发指南.
链接: Spring Statemachine 简介.
链接: 实现一个状态机引擎,教你看清DSL的本质.
链接: COLA下的cola-statemachine状态机.
链接: 爱奇艺团队-领域驱动设计框架Axon实践.
链接: COLA 4.0:应用架构的最佳实践.
链接: 阿里高级技术专家谈开源DDD框架:COLA4.1,分离架构和组件(下).
链接: 【架构设计】无状态状态机在代码中的实践.
Spring Statemachine (重量级选手)1.2k+ star.squirrel-foundation(松鼠)1.8k+ star.cola-statemachine (可乐)6.6k+ star. springboot合cola-statemachine 4.0.1 cola-statemachine(阿里出品)
github链接: cola-components.
gitcode链接: cola-components.
com.alibaba.cola cola-component-statemachine 4.0.1 jar
package com.zm;
import com.alibaba.cola.statemachine.Action;
import com.alibaba.cola.statemachine.Condition;
import com.alibaba.cola.statemachine.StateMachine;
import com.alibaba.cola.statemachine.StateMachineFactory;
import com.alibaba.cola.statemachine.builder.StateMachineBuilder;
import com.alibaba.cola.statemachine.builder.StateMachineBuilderFactory;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
@Slf4j
public class StateMachineTest {
static String MACHINE_ID1 = "TestStateMachine1";
static String MACHINE_ID2 = "TestStateMachine2";
static String MACHINE_ID3 = "TestStateMachine3";
static String MACHINE_ID4 = "TestStateMachine4";
static String MACHINE_ID5 = "TestStateMachine5";
static enum States {
APPLY(1,"申请"),
FIRST_TRIAL(2,"初审"),
FINAL_JUDGMENT(3,"终审"),
REMOVE(4,"移出");
States(Integer value, String name) {
this.value = value;
this.name = name;
}
private final Integer value;
private final String name;
public Integer getValue() {
return value;
}
public String getName() {
return name;
}
}
static enum Events {
SAVE_APPLICATION,
SUBMIT_APPLICATION,
AUDIT_PASS,
AUDIT_REJECT
}
static class Context{
String operator = "flw";
String entityId = "7758258";
String name = "zhoumin";
}
@Test
public void test(){
testExternalTransitionNormal();
testInternalNormal();
testExternalTransitionsNormal();
testExternalTransitionNormal1();
testExternalTransitionNormal2();
StateMachine stateMachine1 = StateMachineFactory.get(MACHINE_ID1);
StateMachine stateMachine2 = StateMachineFactory.get(MACHINE_ID2);
StateMachine stateMachine3 = StateMachineFactory.get(MACHINE_ID3);
System.out.println(stateMachine1.generatePlantUML());
System.out.println(stateMachine2.generatePlantUML());
System.out.println(stateMachine3.generatePlantUML());
System.out.println(StateMachineFactory.get(MACHINE_ID4).generatePlantUML());
System.out.println(StateMachineFactory.get(MACHINE_ID5).generatePlantUML());
}
@Test
public void testExternalTransitionNormal(){
// 第一步:生成一个状态机builder
StateMachineBuilder builder = StateMachineBuilderFactory.create();
// 第二步:设置一个外部状态转移类型的builder,并设置fromtoonwhenperform
builder.externalTransition() // 外部状态流转
.from(States.APPLY) // 起始状态:申请
.to(States.FIRST_TRIAL) // 目的状态:初审
.on(Events.SUBMIT_APPLICATION) // 事件:提交申请
.when(checkCondition()) // 流转需要校验的条件,校验不通过不会进行doAction
.perform(doAction()); // 执行流转操作 这个action 我们可以按自己所需修改,比如这种Action service的方法Service::method
// 第三步:设置状态机的id和ready,并在StateMachineFactory中的stateMachineMap进行注册
builder.build(MACHINE_ID1);
// 第四步:触发状态机
StateMachine stateMachine = StateMachineFactory.get(MACHINE_ID1);
stateMachine.showStateMachine();
// 通过状态机执行 待审核状态执行审核操作,
States target1 = stateMachine.fireEvent(States.APPLY, Events.SUBMIT_APPLICATION, new Context());
}
@Test
public void testExternalTransitionNormal1(){
StateMachineBuilder builder = StateMachineBuilderFactory.create();
builder.externalTransition()
.from(States.FIRST_TRIAL)
.to(States.FINAL_JUDGMENT)
.on(Events.AUDIT_PASS)
.when(checkCondition())
.perform(doAction());
builder.build(MACHINE_ID4);
}
@Test
public void testExternalTransitionNormal2(){
StateMachineBuilder builder = StateMachineBuilderFactory.create();
builder.externalTransition()
.from(States.FINAL_JUDGMENT)
.to(States.REMOVE)
.on(Events.AUDIT_PASS)
.when(checkCondition())
.perform(doAction());
builder.build(MACHINE_ID5);
}
@Test
public void testInternalNormal(){
StateMachineBuilder builder = StateMachineBuilderFactory.create();
// 内部流转 internal transition
// 假设现在只是用户补全资料,只需要进行一些更新数据操作,不需要状态流转。这种需求可以通过内部状态流转实现
builder.internalTransition()
.within(States.APPLY)
.on(Events.SAVE_APPLICATION)
.when(checkCondition())
.perform(doAction());
StateMachine stateMachine = builder.build(MACHINE_ID2);
// 打印状态机里面的流程流转图谱
stateMachine.showStateMachine();
// 通过状态机执行 待审核状态执行审核操作,
States target = stateMachine.fireEvent(States.APPLY, Events.SAVE_APPLICATION, new Context());
}
@Test
public void testExternalTransitionsNormal(){
StateMachineBuilder builder = StateMachineBuilderFactory.create();
// external transitions 任意一个状态
builder.externalTransitions()
.fromAmong(States.FIRST_TRIAL, States.FINAL_JUDGMENT)
.to(States.APPLY)
.on(Events.AUDIT_REJECT)
.when(checkCondition())
.perform(doAction());
StateMachine stateMachine = builder.build(MACHINE_ID3);
// 打印状态机里面的流程流转图谱
stateMachine.showStateMachine();
// 通过状态机执行 待审核状态执行审核操作,
States target1 = stateMachine.fireEvent(States.FIRST_TRIAL, Events.AUDIT_REJECT, new Context());
States target2 = stateMachine.fireEvent(States.FINAL_JUDGMENT, Events.AUDIT_REJECT, new Context());
}
private Condition checkCondition() {
return (ctx) -> true; // 默认返回true
}
private Action doAction() {
return (from, to, event, ctx)-> log.info(ctx.operator+" is operating "+ctx.entityId+" from:"+from.getName()+" to:"+to.getName()+" on:"+event+";"+ctx.name);
}
}
可以生成PlantUML格式
stateMachine.generatePlantUML();springboot合Spring Statemachine 2.x
待续~
生成PlantUML图网站链接: planttext.
idea插件idea安装 PlantUML插件
idea直接安装:File -> Settings -> Plugins 搜索 PlantUML ,找到 PlantUML integration 并安装
经过安装包引入
下载插件包安装:地址http://plugins.jetbrains.com/plugin/7017-plantuml-integration 我已经下载好,能够使用:plantuml4idea.zip ctrl+ alt + s 打开setting,搜索plugins,



