squirrel-foundation-demo

一个简单的squirrel-foundation-demo
利用状态机模拟一个订单的支付过程。
squirrel-foundation没有任何严重的依赖关系,因此基本上它应该是高度可嵌入的。squirrel-foundation没有整合spring框架,所以首先要用spring集成squirrel-foundation。
spring集成squirrel-foundation
(1).通过Spring创建StateMachineBuilder实例(单例);

(2).业务函数中通过(1)的StateMachineBuilder实例创建StateMachine实例,并向StateMachine暴露SpringApplicationContext,以便于StateMachine通过ApplicationContext获取数据层的对象。

public class AbstractStateMachineEngine <T extends UntypedStateMachine> implements ApplicationContextAware {private ApplicationContext applicationContext;protected UntypedStateMachineBuilder stateMachineBuilder = null;@SuppressWarnings("unchecked")public AbstractStateMachineEngine() {//识别泛型参数Class<T> genericType = (Class<T>) GenericTypeResolver.resolveTypeArgument(getClass(),AbstractStateMachineEngine.class);stateMachineBuilder = StateMachineBuilderFactory.create(genericType, ApplicationContext.class);}//注入applicationContext,并在创建StateMachine实例时注入@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext;}public void fire(OrderEvent event, OrderContext context) {T stateMachine = stateMachineBuilder.newUntypedStateMachine(context.orderDTO.getState(),//暂时开启debug进行日志traceStateMachineConfiguration.create().enableDebugMode(true).enableAutoStart(true),//注入applicationContextapplicationContext);stateMachine.fire(event, context);}
}
  •  

继承AbstractStateMachineEngine,并加入@Service注解,便可以在项目中注入。(单例)

@Service
public class OrderStateMachineEngine extends AbstractStateMachineEngine<SubmitOrderStateMachine>{}
  •  

基于注解的方式定义状态机
@States 定义状态列表,里面可以包含多个状态
@State定义每个状态,name状态名称,entryStateInit进入状态时调用的方法,exitCallMethod 离开状态是调用的方法,initialState 为true时,为默认状态。

@States({@State(name = "INIT", entryCallMethod = "entryStateInit", exitCallMethod = "exitStateInit", initialState = true),@State(name = "WAIT_PAY", entryCallMethod = "entryStateWaitPay", exitCallMethod = "exitStateWaitPay"),@State(name = "WAIT_SEND", entryCallMethod = "entryStateWaitSend", exitCallMethod = "exitStateWaitSend"),@State(name = "PART_SEND", entryCallMethod = "entryStatePartSend", exitCallMethod = "exitStatePartSend"),@State(name = "WAIT_RECEIVE", entryCallMethod = "entryStateWaitReceive", exitCallMethod = "exitStateWaitReceive"),@State(name = "COMPLETE", entryCallMethod = "entryStateComplete", exitCallMethod = "exitStateComplete")
})
@Transitions({@Transit(from = "INIT", to = "WAIT_PAY", on = "SUBMIT_ORDER", callMethod = "submitOrder"),@Transit(from = "WAIT_PAY", to = "WAIT_SEND", on = "PAY", callMethod = "pay"),@Transit(from = "WAIT_SEND", to = "PART_SEND", on = "PART_SEND", callMethod = "partSend"),@Transit(from = "PART_SEND", to = "WAIT_RECEIVE", on = "SEND", callMethod = "send"),@Transit(from = "WAIT_RECEIVE", to = "COMPLETE", on = "COMPLETE", callMethod = "complete")
})
@StateMachineParameters(stateType = OrderState.class, eventType = OrderEvent.class, contextType = OrderContext.class)
public class SubmitOrderStateMachine extends AbstractStateMachine<UntypedStateMachine, Object, Object, Object> implements UntypedStateMachine {private OrderService orderService;protected ApplicationContext applicationContext;//定义构造函数接受ApplicationContext注入([参看New State Machine Instance](http://hekailiang.github.io/squirrel/))public SubmitOrderStateMachine(ApplicationContext applicationContext) {this.applicationContext = applicationContext;// 通过applicationContext注入orderServiceorderService = (OrderService) this.applicationContext.getBean(OrderService.class);}public void submitOrder(OrderState fromState, OrderState toState, OrderEvent orderEvent, OrderContext orderContext) {orderService.submitOrder(toState);}public void pay(OrderState fromState, OrderState toState, OrderEvent orderEvent, OrderContext orderContext) {System.out.println("pay");}public void partSend(OrderState fromState, OrderState toState, OrderEvent orderEvent, OrderContext orderContext) {System.out.println("partSend");}public void send(OrderState fromState, OrderState toState, OrderEvent orderEvent, OrderContext orderContext) {System.out.println("send");}public void complete(OrderState fromState, OrderState toState, OrderEvent orderEvent, OrderContext orderContext) {System.out.println("complete");}public void entryStateInit(OrderState fromState, OrderState toState, OrderEvent orderEvent, OrderContext orderContext) {System.out.println("entryStateInit");}public void exitStateInit(OrderState fromState, OrderState toState, OrderEvent orderEvent, OrderContext orderContext) {System.out.println("exitStateInit");}public void entryStateWaitPay(OrderState fromState, OrderState toState, OrderEvent orderEvent, OrderContext orderContext) {System.out.println("entryStateWaitPay");}public void exitStateWaitPay(OrderState fromState, OrderState toState, OrderEvent orderEvent, OrderContext orderContext) {System.out.println("exitStateWaitPay");}public void entryStateWaitSend(OrderState fromState, OrderState toState, OrderEvent orderEvent, OrderContext orderContext) {System.out.println("entryStateWaitSend");}public void exitStateWaitSend(OrderState fromState, OrderState toState, OrderEvent orderEvent, OrderContext orderContext) {System.out.println("exitStateWaitSend");}public void entryStatePartSend(OrderState fromState, OrderState toState, OrderEvent orderEvent, OrderContext orderContext) {System.out.println("entryStatePartSend");}public void exitStatePartSend(OrderState fromState, OrderState toState, OrderEvent orderEvent, OrderContext orderContext) {System.out.println("exitStatePartSend");}public void entryStateWaitReceive(OrderState fromState, OrderState toState, OrderEvent orderEvent, OrderContext orderContext) {System.out.println("entryStateWaitReceive");}public void exitStateWaitReceive(OrderState fromState, OrderState toState, OrderEvent orderEvent, OrderContext orderContext) {System.out.println("exitStateWaitReceive");}public void entryStateComplete(OrderState fromState, OrderState toState, OrderEvent orderEvent, OrderContext orderContext) {System.out.println("entryStateComplete");}public void exitStateComplete(OrderState fromState, OrderState toState, OrderEvent orderEvent, OrderContext orderContext) {System.out.println("exitStateComplete");}}
  •  

Service的实现,更新订单状态

@Service
public class OrderService {@AutowiredOrderDTOMapper orderDTOMapper;public int submitOrder(OrderState state) {OrderDTO orderDTO = new OrderDTO();orderDTO.setState(state);orderDTOMapper.insert(orderDTO);return 1;}}
  •  

mapper


@Mapper
public interface OrderDTOMapper {int insert(OrderDTO orderDTO);}
  •  

Controller类测试
注入OrderStateMachineEngine ,初始化状态为INIT,通过OrderStateMachineEngine 触发SUBMIT_ORDER事件,发生状态变化,并将转换后的状态插入数据库。

@RestController
@RequestMapping(value = "/order/")
public class OrderController {// 注入状态机@AutowiredOrderStateMachineEngine orderStateMachineEngine;@RequestMapping("/test")public void test(){OrderDTO orderDTO = new OrderDTO(OrderState.INIT);OrderContext orderContext = new OrderContext(orderDTO);orderStateMachineEngine.fire(OrderEvent.SUBMIT_ORDER,orderContext);}}
  •  

以下是所用到的实体类定义和状态及事件
订单的实体

public class OrderDTO {private Integer id;private OrderState state;private Date createTime;private Date updateTime;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public OrderState getState() {return state;}public void setState(OrderState state) {this.state = state;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}public Date getUpdateTime() {return updateTime;}public void setUpdateTime(Date updateTime) {this.updateTime = updateTime;}@Overridepublic String toString() {return "OrderDTO{" +"id=" + id +", state='" + state + '\'' +", createTime=" + createTime +", updateTime=" + updateTime +'}';}public OrderDTO(OrderState state) {this.state = state;}public OrderDTO() {}
}
  •  

定义订单的状态

public enum OrderState {INIT,WAIT_PAY,WAIT_SEND,PART_SEND,WAIT_RECEIVE,COMPLETE,CANCELED;public static OrderState getState(String state) {for (OrderState orderState : OrderState.values()) {if (orderState.name().equalsIgnoreCase(state)) {return orderState;}}return null;}
}
  •  

定义订单的事件

public enum OrderEvent {SUBMIT_ORDER,PAY,PART_SEND,SEND,COMPLETE}
  •  

定义上下文

public class OrderContext {public OrderContext(OrderDTO orderDTO) {this.orderDTO = orderDTO;}public OrderContext() {}public OrderDTO orderDTO;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/386569.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MongoDB学习目录

MongoDB基础篇 MongoDB 之 $ 关键字 python操作MongoDB 转载于:https://www.cnblogs.com/yanzhi-1996/p/11095016.html

讲的真透彻!还有人不知道什么是AndroidX的吗?已拿offer入职

前言 春招已经接近尾声了&#xff0c;不知道各位小伙伴有没有收获自己心仪的offer呢。笔者疫情被裁后在家LeetCode狂刷了800多题&#xff0c;加之自己以为工作总结的知识、经验&#xff0c;系统化的整理了一下。在五一期间已经收获了字节的offer。废话不多说&#xff0c;下面是…

docker 启动的 jenkins 中调用宿主机docker进行build

前言 期初有这个需求感觉就跟套娃一样&#xff0c;你在docker 中调用docker&#xff0c;笑哭……这个也太逗了。 不过的确遇到了&#xff0c;因为jenkins 容器中没有docker &#xff0c;所以在编译 docker build 的时候 会出现 docker command 不存在。 好吧&#xff0c;解决他…

Codeforces 773D Perishable Roads 最短路 (看题解)

Perishable Roads 智商题&#xff0c; 不会啊。。 贴个官方题解 https://codeforces.com/blog/entry/51883 #include<bits/stdc.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_p…

Rancher中的服务升级实验

创建一个空的应用myAPP&#xff0c;在myAPP 应用中&#xff0c;创建一个服务nginx-test&#xff0c;包含2个容器副本&#xff0c;使用nginx:1.13.0镜像。假设使用一段时期以后&#xff0c;nginx的版本升级到1.13.1了&#xff0c;如何将该服务的镜像版本升级到新的版本&#xff…

该如何高效实用Kotlin?看这一篇就够了!

前言 说起程序员人们的第一印象就是工资高、加班凶、话少钱多头发少。再加上现在科技互联网公司太吃香&#xff0c;bat、华为小米等公司程序员加班情况被广泛传播&#xff0c;程序员用生命在敲代码的印象刻在了很多人的心里。 与其它行业一样&#xff0c;凡是有高级和普通&…

apply()与call()

JavaScript中的每一个Function对象都有一个apply()方法和一个call()方法&#xff0c;它们的语法分别为&#xff1a; /*apply()方法*/ function.apply(thisObj[, argArray])/*call()方法*/ function.call(thisObj[, arg1[, arg2[, [,...argN]]]]); 它们各自的定义&#xff1a; a…

Java基于redis实现分布式锁(SpringBoot)

前言 分布式锁&#xff0c;其实原理是就是多台机器&#xff0c;去争抢一个资源&#xff0c;谁争抢成功&#xff0c;那么谁就持有了这把锁&#xff0c;然后去执行后续的业务逻辑&#xff0c;执行完毕后&#xff0c;把锁释放掉。 可以通过多种途径实现分布式锁&#xff0c;例如…

请谈下Android消息机制,复习指南

谈起Android框架体系架构&#xff0c;我先提个问&#xff1a;什么是Android框架体系架构 &#xff1f; Android系统构架是安卓系统的体系结构&#xff0c;android的系统架构和其操作系统一样&#xff0c;采用了分层的架构&#xff0c;共分为四层&#xff0c;从高到低分别是And…

SVN Cannot merge into a working copy that has local modifications

我尝试了 主支&#xff0c;分支都提交&#xff0c;但是依然无法合并。 最终&#xff0c;我在服务器上将分支删除&#xff0c;然后主支在拷贝过去。 一&#xff0c;打开服务器资源 二&#xff0c;删除分支 三&#xff0c;拷贝主支到分支 四&#xff0c;刷新分支&#xff0c;就能…

资深Android开发带你入门Framework,再不刷题就晚了!

想要成为一名优秀的Android开发&#xff0c;你需要一份完备的知识体系&#xff0c;在这里&#xff0c;让我们一起成长为自己所想的那样。 本文参考了目前大部分 Android 应用启动优化的方案&#xff0c;将大家的方案做一个汇总&#xff0c;如果你有这方面的需求&#xff0c;只…

K8S相关内容

常用工具&#xff1a;docker linux k8s kubeadm 概念 etcd 数据库 类似redis api server 接口对外提供api 调用 可以命令 kubectl 或者 kube-proxy&#xff0c;能访问etcd&#xff0c;事件总线 scheduler 调度决策的组件 掌握新的情况&#xff0c;进行决策及分布pod放在哪些n…

资深Android开发带你入门Framework,架构师必备技能

开头 先说一下我大概的情况吧。渣本毕业&#xff0c;工作已经有快两年了&#xff0c;从高中就开始玩小破站。无论是学习还是日常放松都是在b站。大学主学的软件技术专业&#xff0c;所以&#xff0c;进大学校门那一刻起&#xff0c;去上海bilibili工作就在心里埋下了种子。在学…

Java——线程锁,死锁,等待唤醒机制

一、线程锁 线程安全问题 其实&#xff0c;线程安全问题都是由全局变量及静态变量引起的。若每个线程中对全局变量、静态变量只有读操作&#xff0c;而无写操作&#xff0c;一般来说&#xff0c;这个全局变量是线程安全的&#xff1b;若有多个线程同时执行写操作&#xff0c;…

资深大牛带你了解源码!关于Android程序员最近的状况,大厂内部资料

前言 回顾一下自己这段时间的经历&#xff0c;因公司突然通知裁员&#xff0c;我匆匆忙忙地出去面了几家&#xff0c;但最终都没有拿到offer&#xff0c;我感觉今年的寒冬有点冷。公司开始第二波裁员&#xff0c;我决定主动拿赔偿走人。后续的面试过程我做了一些准备&#xff…

AE 新建项目(一)(持续更新,做到哪算哪)

开发环境 工具&#xff1a;Visual Studio 2012、ArcEngine10.4.1 语言&#xff1a;C# 开发步骤 1、打开Visual Studio 2012&#xff0c;选择新建项目&#xff0c;创建一个.NET Framework4的&#xff0c;Windows窗体应用程序。取名Demo 2、工具箱中&#xff0c;拖一个splitConta…

基于redis分布式锁实现的多线程并发程序

前两个版本的代码 都或多或少存在一定的问题&#xff0c;虽然可能微乎其微&#xff0c;但是程序需要严谨再严谨&#xff0c; 第一个版本问题&#xff1a; 局限于单机版&#xff0c;依赖于 Jvm的锁 第二个版本问题&#xff1a; 极端情况下&#xff0c;解锁逻辑的问题&#xf…

day15 Ui自动化元素的定位

day15 元素的定位Ui自动化元素的定位1、火狐浏览器安装try xpath2、元素定位思路&#xff1a;&#xff08;1&#xff09;查看页面元素&#xff0c;确认能够唯一定位到元素的属性&#xff0c;比如id&#xff0c;文案3、学习xpath cssSelector 手写定位方式xpath&#xff08;xpat…

几张图可以理解GC JVM调优的内容

public class ApiPurchaseOrderServiceApp {public static void main(String[] args) throws Exception {ApiPurchaseOrderServiceApp mnew ApiPurchaseOrderServiceApp();m.compute();//栈System.out.println("ok");//方法出口}public int compute(){int a1;//局部…

灵魂拷问!一起刷完了这份1307页的安卓面试宝典吧,不吃透都对不起自己

前言 每个程序员都有一个梦想&#xff0c;那就是进一线互联网公司深造&#xff0c;不要跟我说你不想进去&#xff0c;如果给你一个这样的平台&#xff0c;不管是薪资待遇还是接触的高度来说&#xff0c;对我们程序员来说都是一个机会&#xff0c;我以前有一个同事&#xff0c;…