springstatemachine多个状态机实践

更新时间:2023-05-13 12:59:44 阅读: 评论:0

springstatemachine多个状态机实践
最近在做⽼系统升级改造,为了解决原来流程复杂,代码冗杂的情况,计划引⼊状态机框架来改造,为更好理解,也为记录下⾃⼰的学习过程,计划写总结博⽂。
状态机spring statemachine 概述
Spring Statemachine是应⽤程序开发⼈员在Spring应⽤程序中使⽤状态机概念的框架
Spring Statemachine旨在提供以下功能:
1. 易于使⽤的扁平单级状态机,⽤于简单的使⽤案例。
2. 分层状态机结构,以简化复杂的状态配置。
3. 状态机区域提供更复杂的状态配置。
4. 使⽤触发器,转换,警卫和操作。
5. 键⼊安全配置适配器。
6. ⽣成器模式,⽤于在Spring Application上下⽂之外使⽤的简单实例化通常⽤例的⾷谱
7. 基于Zookeeper的分布式状态机
8. 状态机事件监听器。
9. UML Eclip Papyrus建模。
10. 将计算机配置存储在永久存储中。
11. Spring IOC集成将bean与状态机关联起来。
状态机功能强⼤,因为⾏为始终保证⼀致,使调试相对容易。这是因为操作规则是在机器启动时写成的。这个想法是你的应⽤程序可能存在于有限数量的状态中,某些预定义的触发器可以将你的应⽤程序从⼀个状态转移到另⼀个状态。此类触发器可以基于事件或计时器。
在应⽤程序之外定义⾼级逻辑然后依靠状态机来管理状态要容易得多。您可以通过发送事件,侦听更改或仅请求当前状态来与状态机进⾏交互。
快速开始
已经写过状态机的简单启动,状态流转的例⼦,但实际应⽤过程不可能只⽤到⼀个machine,需多个machine处理对象流转。
下⾯的实践有以下特⾊:
1、不同线程启⽤不同statemachine实例处理
2、⽤⼯⼚模式创建statemachine,且⽤StateMachinePersist根据recruit对象不同状态反序列化statemachine
3、 ⽤message 给OnTransition注解的⽅法传递对象
@Configuration
//@EnableStateMachine(name = "ddd")
@Scope("prototype")
@EnableStateMachineFactory(name = "recruitStateMachineFactory")
public class Config1extends EnumStateMachineConfigurerAdapter<States, Events> {
/**订单状态机ID*/
public static final String orderStateMachineId = "recruitStateMachineId";
@Override
public void configure(StateMachineStateConfigurer<States, Events> states)
throws Exception {
states
.withStates()
.initial(States.STATE1)
.states(EnumSet.allOf(States.class));
}
@Override
public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
throws Exception {
transitions
.withExternal()
.source(States.STATE1).target(States.STATE2)
.event(Events.EVENT1)
.and()
.withExternal()
七一建党节.source(States.STATE2).target(States.STATE1)
.event(Events.EVENT2)
}
@Bean
public StateMachinePersister<States, Events, Recruit> persister(){
return new DefaultStateMachinePersister<States, Events, Recruit>(new StateMachinePersist<States, Events, Recruit>() {
@Override
public void write(StateMachineContext<States, Events> context, Recruit order) throws Exception {
//此处并没有进⾏持久化操作
条组词语
//order.State());
}
@Override
public StateMachineContext<States, Events> read(Recruit order) throws Exception {
//此处直接获取order中的状态,其实并没有进⾏持久化读取操作
StateMachineContext<States, Events> result =new DefaultStateMachineContext<States, Events>(States(), null, null, null, null, orderState return result;
}
});
}
}
状态转换
@WithStateMachine(name = "recruitStateMachineId")
public class MyBean {
@OnTransition(source="STATE2" ,target = "STATE1")
public void toState1(Message message) {
Recruit recruit = (Recruit) Headers().get("recruit");        System.out.println("toState1" +Thread.currentThread().toString());    }
@OnTransition(source="" ,target = "STATE1")
public void initState() {
System.out.println("toState1"+Thread.currentThread().toString());    }
课后教学反思
@OnTransition(source="STATE1",target = "STATE2")
public void toState2() throws Exception {
System.out.println("toState2"+Thread.currentThread().toString());
//throw new Exception();
}
}
@RestController
public class MyApp {
银黄胶囊ThreadLocal<StateMachine> stateMachineThreadLocal  = new ThreadLocal<StateMachine>();
@Autowired
@Qualifier("recruitStateMachineFactory" )
StateMachineFactory<States, Events> stateMachineFactory;
@Autowired
private StateMachinePersister<States, Events,Recruit> persister;
@RequestMapping("/hello")
void doSignals() {
System.out.println("start before");
Recruit recruit = new Recruit();
StateMachine stateMachinet = getStateMachine(recruit);中层干部管理
System.out.println("dosignals stateMachinet hashcode "+stateMachinet.hashCode()+"  recruit hashcode is "+recruit.hashCode());
stateMachinet.ndEvent(Events.EVENT1);
stateMachinet.ndEvent(Events.EVENT2);
}
private StateMachine getStateMachine(Recruit recruit){
StateMachine machine = ();
新年快乐儿童画if (null == machine){
machine = StateMachine("recruitStateMachineId");
}
try {
machine.start();
} catch (Exception e) {
//e.printStackTrace();
}
return machine;
}
void ndEvent(Events events,Recruit recruit){
Message message = MessageBuilder.withPayload(events).tHeader("recruit", recruit).build();
StateMachine stateMachine = getStateMachine(recruit);
stateMachine.ndEvent(message);
}
@RequestMapping("/ndEvent1")
void ndEvent(){
Recruit recruit = new Recruit();
recruit.tStates(States.STATE1);
StateMachine stateMachine = getStateMachine(recruit);
stateMachine.ndEvent(Events.EVENT1);
}
@RequestMapping("/ndEvent2")
void ndEvent2(){
Recruit recruit = new Recruit();
recruit.tStates(States.STATE2);
StateMachine stateMachine = getStateMachine(recruit);
stateMachine.ndEvent(Events.EVENT2);
}
乐观积极}
总结
解决了如何处理多⽤户多对象场景如何启⽤状态机的例⼦,但只有状态机简单的状态迁移,状态机配置可以有多种⽅法,下⼀篇⽂章分析各种配置的不同⽤法
ChoiceTransitionConfigurer<\S,E> withChoice() ChoiceTransitionConfigurer<\S,E> withChoice() EntryTransitionConfigurer<\S,E> withEntry() ExitTransitionConfigurer<\S,E> withExit()
ExternalTransitionConfigurer<\S,E> withExternal()
ForkTransitionConfigurer<\S,E> withFork()
HistoryTransitionConfigurer<\S,E> withHistory()
InternalTransitionConfigurer<\S,E> withInternal()
JoinTransitionConfigurer<\S,E> withJoin()
JunctionTransitionConfigurer<\S,\E> withJunction()
LocalTransitionConfigurer<\S,E> withLocal()
网络怎么挣钱

本文发布于:2023-05-13 12:59:44,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/613658.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:状态机   状态   配置
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图