Spring Statemachine应用实践( 二 )


实际生产中使用的是spring statemachine ,版本是2.2.0.RELEASE 。线下对比使用的是squirrel-foundation,版本是0.3.10 。这里仅供使用对比 。
从创建活动到活动下线状态流转作为示例,如下图:
 

Spring Statemachine应用实践

文章插图
 
pom<?xml versinotallow="1.0" encoding="utf-8" ?><!-- spring statemachine --><dependency><groupId>org.springframework.statemachine</groupId><artifactId>spring-statemachine-starter</artifactId><version>2.2.0.RELEASE</version></dependency><!-- spring statemachine context 序列化 --><dependency><groupId>org.springframework.statemachine</groupId><artifactId>spring-statemachine-kryo</artifactId><version>2.2.0.RELEASE</version></dependency><!-- squirrel-foundation --><dependency><groupId>org.squirrelframework</groupId><artifactId>squirrel-foundation</artifactId><version>0.3.10</version></dependency>状态&事件定义 public enum State {INIT("初始化"),DRAFT("草稿"),WAIT_VERIFY("待审核"),PASSED("审核通过"),REJECTED("已驳回"),//已发起上线操作,未到上线时间的状态WAIT_ONLIE("待上线"),ONLINED("已上线"),//过渡状态无实际意义,无需事件触发OFFLINING("下线中"),OFFLINED("已下线"),FINISHED("已结束");private final String desc;}public enum Event {SAVE("保存草稿"),SUBMIT("提交审核"),PASS("审核通过"),REJECT("提交驳回"),ONLINE("上线"),OFFLINE("下线"),FINISH("结束");private final String desc;}状态流转定义@Configuration@EnableStateMachineFactorypublic class ActivitySpringStateMachineAutoConfiguration extends StateMachineConfigurerAdapter<State, Event> {@Autowiredprivate ApplicationContext applicationContext;@Autowiredprivate StateMachineRuntimePersister<State, Event, String> activityStateMachinePersister;@Beanpublic StateMachineService<State, Event> activityStateMachineService(StateMachineFactory<State, Event> stateMachineFactory) {return new DefaultStateMachineService<>(stateMachineFactory, activityStateMachinePersister);}@Overridepublic void configure(StateMachineConfigurationConfigurer<State, Event> config) throws Exception {// @formatter:offconfig.withPersistence().runtimePersister(activityStateMachinePersister).and().withConfiguration().stateDoActionPolicy(StateDoActionPolicy.TIMEOUT_CANCEL).stateDoActionPolicyTimeout(300, TimeUnit.SECONDS).autoStartup(false);// @formatter:on}@Overridepublic void configure(StateMachineStateConfigurer<State, Event> states) throws Exception {states.withStates().initial(State.INIT).choice(State.OFFLINING).states(EnumSet.allOf(State.class));}@Overridepublic void configure(StateMach.NETransitionConfigurer<State, Event> transitions) throws Exception {// 待提交审核 --提交审核--> 待审核// @formatter:off// 现态-->事件-->次态transitions.withExternal().source(State.INIT).target(State.DRAFT).event(Event.SAVE).and().withExternal().source(State.DRAFT).target(State.WAIT_VERIFY).event(Event.SUBMIT).guard(applicationContext.getBean(SubmitCondition.class));transitions.withExternal().source(State.WAIT_VERIFY).target(State.PASSED).event(Event.PASS).action(applicationContext.getBean(PassAction.class));transitions.withExternal().source(State.WAIT_VERIFY).target(State.REJECTED).event(Event.REJECT).guard(applicationContext.getBean(RejectCondition.class));transitions.withExternal().source(State.REJECTED).target(State.WAIT_VERIFY).event(Event.SUBMIT).guard(applicationContext.getBean(SubmitCondition.class));// 审核通过-->上线-->待上线transitions.withExternal().source(State.PASSED).target(State.WAIT_ONLIE).event(Event.ONLINE);// 待上线-->上线-->已上线transitions.withExternal().source(State.WAIT_ONLIE).target(State.ONLINED).event(Event.ONLINE);// 已上线-->下线-->已下线transitions.withExternal().source(State.ONLINED).target(State.OFFLINING).event(Event.OFFLINE);// 待上线-->下线-->下线中transitions.withExternal().source(State.WAIT_ONLIE).target(State.OFFLINING).event(Event.OFFLINE).and()// 已下线-->结束-->已结束.withChoice().source(State.OFFLINING).first(State.FINISHED, new Guard<State, Event>() {@Overridepublic boolean evaluate(StateContext<State, Event> context) {return true;}}).last(State.OFFLINED);// @formatter:on}}说明: