SpringStateMachine结合UML

springStateMachine2.X开始支持与UML结合,大大方便了对其的应用,在1.X的版本中大量繁杂的定义状态、action等等内容都得到了很好的解决;

step1 eclipse安装Papyrus插件

安装完成后:

官方说明:https://docs.spring.io/spring-statemachine/docs/2.0.4.RELEASE/reference/htmlsingle/

Defining a state machine configuration with UI modeling is supported via Eclipse Papyrus framework.From eclipse wizard create a new Papyrus Model with UML Diagram Language. In this example it’s named as simple-machine. Then you’ve given an option to choose various diagram kind’s and a StateMachine Diagram must be chosen.We want to create a machine having two states, S1 and S2 where S1 is initial state. Then event E1 is created to do a transition from S1 to S2. In papyrus a machine would then look like something shown below.

<?xml version="1.0" encoding="UTF-8"?>
<uml:Model xmi:version="20131001" xmlns:xmi="http://www.omg.org/spec/XMI/20131001" xmlns:uml="http://www.eclipse.org/uml2/5.0.0/UML" xmi:id="_AMP3IP8fEeW45bORGB4c_A" name="RootElement"><packagedElement xmi:type="uml:StateMachine" xmi:id="_AMRFQP8fEeW45bORGB4c_A" name="StateMachine"><region xmi:type="uml:Region" xmi:id="_AMRsUP8fEeW45bORGB4c_A" name="Region1"><transition xmi:type="uml:Transition" xmi:id="_chgcgP8fEeW45bORGB4c_A" source="_EZrg4P8fEeW45bORGB4c_A" target="_FAvg4P8fEeW45bORGB4c_A"><trigger xmi:type="uml:Trigger" xmi:id="_hs5jUP8fEeW45bORGB4c_A" event="_NeH84P8fEeW45bORGB4c_A"/></transition><transition xmi:type="uml:Transition" xmi:id="_egLIoP8fEeW45bORGB4c_A" source="_Fg0IEP8fEeW45bORGB4c_A" target="_EZrg4P8fEeW45bORGB4c_A"/><subvertex xmi:type="uml:State" xmi:id="_EZrg4P8fEeW45bORGB4c_A" name="S1"/><subvertex xmi:type="uml:State" xmi:id="_FAvg4P8fEeW45bORGB4c_A" name="S2"/><subvertex xmi:type="uml:Pseudostate" xmi:id="_Fg0IEP8fEeW45bORGB4c_A"/></region></packagedElement><packagedElement xmi:type="uml:Signal" xmi:id="_L01D0P8fEeW45bORGB4c_A" name="E1"/><packagedElement xmi:type="uml:SignalEvent" xmi:id="_NeH84P8fEeW45bORGB4c_A" name="SignalEventE1" signal="_L01D0P8fEeW45bORGB4c_A"/>
</uml:Model>

自动生成xml,不需要书写其余代码,但是要将生成的UML.xml文件加载;

step2 应用Papyrus创建UML

在工程内创建UML文件目录 resources/model,并new一个Papyrus Model >model目录右键>new>other>

创建完成后打开XX.di文件,运行Create View;

开始绘制:

step3 配置状态config

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineModelConfigurer;
import org.springframework.statemachine.config.model.StateMachineModelFactory;
import org.springframework.statemachine.uml.UmlStateMachineModelFactory;@Configuration
@EnableStateMachine
public class StateMachineSimpleConfig extends StateMachineConfigurerAdapter<String, String> {//配置@Overridepublic void configure(StateMachineModelConfigurer<String, String> model) throws Exception {model.withModel().factory(modelFactory());}//通过Resource创建model对象,传入UML文件地址@Beanpublic StateMachineModelFactory<String, String> modelFactory() {Resource model = new ClassPathResource("model/sm-simple.uml");return new UmlStateMachineModelFactory(model);}}

step4 创建监听

import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.OnTransitionEnd;
import org.springframework.statemachine.annotation.OnTransitionStart;
import org.springframework.statemachine.annotation.WithStateMachine;@WithStateMachine
public class StateMachineSimpleListener {//监听执行器@OnTransition(target = "T1")public void init() {System.err.println("--------- INIT T1 -----------");}//监听执行器@OnTransition(source = "T1", target = "T2")public void create() {System.err.println("---------T1 -> T2-----------");}//监听执行器@OnTransition(source = "T2", target = "T3")public void S2toS5() {System.err.println("---------T2 -> T3---------");}}

step5 测试

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.test.context.junit4.SpringRunner;import com.bfxy.statemachine.constant.Events;
import com.bfxy.statemachine.constant.States;@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {@Testpublic void contextLoads() {}//注入的stateMachineSimple名字必须与UML文件名字一致@Autowiredprivate StateMachine<String, String> stateMachineSimple;@Testpublic void testSimple() throws Exception {stateMachineSimple.start();stateMachineSimple.sendEvent(MessageBuilder.withPayload("K1").build());stateMachineSimple.sendEvent(MessageBuilder.withPayload("K2").build());}}

004 电商平台核心链路_StateMachine(二)相关推荐

  1. 001 电商平台核心链路_整体架构设计

    应用微服务构建电商平台,用户在业务入口提交订单请求 ,请求进入订单平台处理并落地入库,然后发送创建消息给下游服务,MQ服务将创建通知发送至物流平台处理(每层服务链路都是如此流程),物流通过MQ进入调度 ...

  2. 国家出台电子商务法,解决电商平台“二选一”问题

    近日,电子商务法草案第三次提交全国人大常委会进行审议.在三审稿中,对电商平台要求商家"二选一"的行为进行了规范. 电商平台"二选一"具体是指,在电商促销活动中, ...

  3. 电商平台不是电商,商家应如何看待电商的崛起?

    也许你总会刷到一些电商平台水军,他们喜欢把实体不行归咎于高铺租和电商平台的冲击,仿佛有了电商平台就可以绕过铺租,就是未来的救世主一样.为什么我会说这些人是电商平台的水军呢,因为这些鼓吹手们总是会有意无 ...

  4. 工业制造行业B2B电商平台解决方案

    揭示工业制造业的发展趋势 -- 数据显示,近年来,我国工业增加值与增速出现了几次不同程度的上涨,一改往常持续下降的局面.国内整个工业制造业正在逐渐复苏,同时随着国家在政策层面持续加码促进大型工业制造业 ...

  5. 数商云工业制造行业B2B电商平台解决方案

    揭示工业制造业的发展趋势: 数据显示,自从进入2017年,我国工业增加值与增速出现了几次不同程度的上涨,一改往常持续下降的局面.国内整个工业制造业正在逐渐复苏,同时随着国家在政策层面持续加码促进大型工 ...

  6. 传统企业的移动电商平台实践

    本文首发在 freshmanTechnology .感谢中生代的邀请,也就促成了本文. 作者 郝振明 摘要:随着电商领域在传统企业的热度不断上升,在"互联网+"的背景下,许多传统企 ...

  7. 禁止电商平台二选一、遛狗必栓绳!5月起有这些新规定

    5月起,有一批新的法律法规即将落地,这将如何影响我们的生活,一起来看看吧. "直播带货"主播应满十六周岁 国家互联网信息办公室.公安部.商务部.文化和旅游部.国家税务总局.国家市场 ...

  8. 电商平台“二选一” 最后买单的却是商家和消费者

     TechWeb 4月27日 文/小渔 拼多多日前对外发布上市后的首份年报,各项数据仍是保持着高速增长的势头.与此同时,创始人黄峥也发布了上市后首封致股东信,言语中透露出对市场中存在的"二选 ...

  9. 前端网页设计内容二《电商平台网站》

    讲重点.展示页面效果.代码编译. 看页面效果: 今天的 重点在最后一个页面. 看代码: <!DOCTYPE html> <html lang="zh">&l ...

最新文章

  1. LeetCode 76. Minimum Window Substring / 567. Permutation in String
  2. 计算机原理解读图,详细讲解仪器仪表测试系统,结构原理图拿走不谢
  3. object-c 队列
  4. android 照片多选,Android: 关于系统相册多选图片的问题
  5. 权限分配之权限的展示
  6. android json字符串转成json对象_C++ 两行代码实现json与类对象互转
  7. 操作指令详解_爱码小士丨 APP稳定性测试(附视频详解)
  8. 高通driver模块编译方法
  9. 多项式拟合(polyfit和polyval)
  10. CentOS防火墙放行端口(以 8080 端口为例)
  11. 第三方支付回调地址代理转发
  12. iOS之支持https与ssl双向验证(包含:解决UIWebView加载不了https网页的图片,css,js等外部资源)
  13. 深入了解JVM之垃圾回收(二)
  14. 电脑开机只显示计算机界面,电脑开机只显示主板logo进不去系统是什么原因
  15. 利用 clip-path 绘制不规则的图形
  16. JAVA基础Day01
  17. 如何查询计算机com口使用
  18. 关于微信微博等二维码问题
  19. WebGoat 网安攻击模拟操训
  20. MUR6060PT-ASEMI快恢复二极管MUR6060PT

热门文章

  1. 基于云平台的智能变电站远程监控系统
  2. 恢复账套提示文件上的媒体簇结构不正确_2012年初级会计电算化试题二及答案...
  3. 数据库概论-MySQL的数据表的基本操作
  4. 【小趴菜STM32开发笔记】---- 01输入输出端口GPIO
  5. 孝心至上自强不息南阳何志强感动济源
  6. 智慧校园人脸识别系统源码
  7. 单片机 模拟电子闹钟 c语言,单片机可调数字钟闹钟整点报时功能(带Proteus仿真电路图+源码)...
  8. PMP 报名有什么条件?容易满足吗?
  9. 好文章,《李录:现代化十六讲》
  10. 31岁拿了阿里P6的offer,还有必要去吗?