一种无状态的状态机-cola
<!-- 状态机相关引用 -->
<dependency>
<groupId>com.alibaba.cola</groupId>
<artifactId>cola-component-statemachine</artifactId>
<version>4.0.1</version>
<type>jar</type>
</dependency>
状态机的初始化配置
支持状态流转的枚举类
public enum States {
APPLY(1, "申请"),
FIRST_TRIAL(2, "初审"),
FINAL_JUDGEMENT(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;
}
}
事件,支撑枚举类做流程流转
public enum Events {
SAVE_APPLICATION,
SUBMIT_APPLICATION,
AUDIT_AGREE,
AUDIT_REJECT;
}
状态机的id
public class BusinessConstant {
public static final String APPLY_STATION_MACHINE_START = "applyStationMachineStart";
}
值传递
@Data
public class Context {
String operator = "flw";
String entityId = "1885573359";
String name = "dlf";
private UserDo userDo;
}
这里是配置了一个流程,后续可以根据自己的需要配置多个流程
**
* 状态机的初始化配置
*
* @author dlf
* @date 2023/4/20 17:41
*/
@Configuration
public class StatemachineConfig {
@Resource
private UserService userService;
@Bean
public StateMachineBuilder<States, Events, Context> testExternalTransitionNormalApply() {
StateMachineBuilder<States, Events, Context> builder = StateMachineBuilderFactory.create();
builder.externalTransition()
.from(States.APPLY)
.to(States.FIRST_TRIAL)
.on(Events.SUBMIT_APPLICATION)
.perform(userService.action());
builder.build(BusinessConstant.APPLY_STATION_MACHINE_START);
return builder;
}
}
示例代码
public interface UserService {
public int addOne(UserDo userDo);
public Action<States, Events, Context> action();
public String actionSys();
}
示例代码
@Data
public class UserDo {
private String userName;
private String passWord;
private String process;
}
示例代码演示状态机得使用
@Service
@Slf4j
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;
@Override
public int addOne(UserDo userDo) {
return userDao.add(userDo);
}
@Override
public Action<States, Events, Context> action() {
return (from, to, event, ctx) -> {
UserDo turnsForm = ctx.getUserDo();
log.info("原来的状态为:"+turnsForm.getProcess());
System.err.println("UserDo:"+userDao);
turnsForm.setProcess("待审批");
log.info("目标状态为:"+turnsForm.getProcess());
};
}
@Override
public String actionSys() {
Context context = new Context();
UserDo userDo = new UserDo();
userDo.setUserName("machineTest");
userDo.setPassWord("dlf123");
userDo.setProcess("发起申请....");
StateMachine<States, Events, Context> stateMachine = StateMachineFactory.get(BusinessConstant.APPLY_STATION_MACHINE_START);
stateMachine.showStateMachine();
stateMachine.fireEvent(States.APPLY, Events.SAVE_APPLICATION, context);
StateMachine<States, Events, Context> contextStateMachine = StateMachineFactory.get(BusinessConstant.APPLY_STATION_MACHINE_START);
return userDo.getProcess();
}
测试流程
@RestController
@Slf4j
@RequestMapping("/test")
public class UserController {
@Value("${server.port}")
private String serverPort;
@Resource
public UserService userService;
@GetMapping("/testMachione")
public String testMachine() {
return userService.actionSys();
}
}
![状态机-cola-DSL_第1张图片](http://img.e-com-net.com/image/info8/d6ac475bd23946c8952c5c7353a5c05b.jpg)
![状态机-cola-DSL_第2张图片](http://img.e-com-net.com/image/info8/e6487011c53d4c52b8d5eefeda5e77b8.jpg)