巧用liteflow,告别if else,SpringBoot整合liteflow

假设有一个三个原子业务,吃饭、喝水、刷牙。
现在有三个场景,分别是
场景A: 吃饭->刷牙->喝水

官网地址:https://liteflow.cc/

1.添加依赖:

<dependency>
  <groupId>com.yomahub</groupId>
  <artifactId>liteflow-spring-boot-starter</artifactId>
  <version>2.11.4.2</version>
</dependency>

2.application.yml

liteflow:
  rule-source: flow.el.xml

3.resources下创建flow.el.xml

<?xml version="1.0" encoding="UTF-8"?>
<flow>
    <chain name="chain1">
        THEN(a, b, c);
    </chain>
</flow>

创建3个组件:ABC
组件A:

 @Component("a")
    public class ComponentA  extends NodeComponent {
   
        @Override
        public void process() throws Exception {
   
            //设置上下下文
            DefaultContext context = this.getContextBean(DefaultContext.class);
            context.setData("key","你好!!!!");
            System.out.println("吃饭");
        }
    }

组件B:

@Component("b")
public class ComponentB extends NodeComponent {
   
    @Override
    public void process() throws Exception {
   
        System.out.println("刷牙");
    }
}

组件C:

@Component("c")
public class ComponentC extends NodeComponent {
   
    @Override
    public void process() throws Exception {
   
        System.out.println("喝水");
    }
}

测试:

    @PostMapping("testLiteFlow")
    public void test1() {
   
        LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
        if (response.isSuccess()) {
   
            System.out.println("执行成功!");
        }
        //异常
        Exception e = response.getCause();
        DefaultContext contextBean = response.getContextBean(DefaultContext.class);
        //取出上下下文
        Object key = contextBean.getData("key");
    }

在这里插入图片描述
上下文
//设置上下下文

DefaultContext context = this.getContextBean(DefaultContext.class);
context.setData("key","你好!!!!");

取出上下文:

DefaultContext contextBean = response.getContextBean(DefaultContext.class);
 //取出上下下文
 Object key = contextBean.getData("key");
也可以自己定义Context

异常

 //异常
Exception e = response.getCause();

可以随意组合业务:
在这里插入图片描述

相关推荐

  1. 【开源视频联动物联网平台】LiteFlow

    2024-02-10 12:16:01       53 阅读
  2. count与count()

    2024-02-10 12:16:01       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-10 12:16:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-10 12:16:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-10 12:16:01       82 阅读
  4. Python语言-面向对象

    2024-02-10 12:16:01       91 阅读

热门阅读

  1. 练习:鼠标类设计之1_类内容解析

    2024-02-10 12:16:01       48 阅读
  2. MySQL 的Sql脚本是如何被编译的

    2024-02-10 12:16:01       48 阅读
  3. MySQL基础查询篇(9)-数学函数在查询中的应用

    2024-02-10 12:16:01       48 阅读
  4. dreamtalk 学习笔记

    2024-02-10 12:16:01       45 阅读
  5. 计算机网络(第六版)复习提纲30

    2024-02-10 12:16:01       45 阅读
  6. 【MIMO】

    2024-02-10 12:16:01       54 阅读