首页 > 其他分享 >LitFlow教程(一)

LitFlow教程(一)

时间:2022-10-13 17:03:06浏览次数:67  
标签:教程 LitFlow 流程 class person PersonEntity liteflow public

目录

LiteFlow是什么?

简单来说,就是对于复杂的业务,LiteFlow可以把流程进行模块化拆分,使其变为一个个组件;通过定义一个规则来对组件进行编排,使每个组件可以根据实际需求来进行流转。

快速入门

打个比方,每天下班回到家以后,通常的操作有洗手、吃饭、追剧;

导入maven

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

定义一个性别枚举类

public enum SexEnum {
    GIRL,
    BOY
}

定义人的实体类 UserInfoEntity

@Data
public class UserInfoEntity {
    private String name;
    private SexEnums sex;
}

分别定义洗手、吃饭、看电视三个流程模块

@LiteflowComponent("xxx") 注解的参数xxx,代表的是要编写规则时用到的组件id

洗手流程 WashHandsComponent

@Slf4j
@LiteflowComponent("washHandsComponent")
public class WashHandsComponent extends NodeComponent {
    @Override
    public void process() throws Exception {
        PersonEntity person = this.getRequestData();
        log.info("我的名字是{},我爱干净,我在洗手!",person.getName());
    }
}

吃饭流程 HavingDinnerComponent

@Slf4j
@LiteflowComponent("havingDinnerComponent")
public class HavingDinnerComponent extends NodeComponent {

    @Override
    public void process() throws Exception {
        PersonEntity person = this.getRequestData();
        log.info("我的名字是{},我在吃饭!", person.getName());
    }
}

看电视流程 WatchTVComponent

@Slf4j
@LiteflowComponent("watchTVComponent")
public class WatchTVComponent extends NodeComponent {

    @Override
    public void process() throws Exception {
        PersonEntity person = this.getRequestData();
        log.info("我的名字是{},我在看电视!", person.getName());
    }
}

编写EL规则文件

在resource文件夹下的 config/flow.xml 中定义规则,chain的name参数代表这个流程的名称

<?xml version="1.0" encoding="UTF-8"?>
<flow>
    <chain name="goHome">
        <then value="washHandsComponent"/>
        <then value="havingDinnerComponent"/>
        <then value="watchTVComponent"/>
    </chain>
</flow>

在application.properties配置文件中指定EL规则文件

我这边用的springboot项目,其他场景和可配置项可以参考官方的文档 Springboot下的配置项

liteflow.rule-source=config/flow.xml
//关闭执行中过程中的日志打印
liteflow.print-execution-log=false
//关闭liteflow的banner打印
liteflow.print-banner=false

运行代码

创建一个测试类运行,不知道怎么创建测试类的自行百度;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApp.class, webEnvironment = NONE)
public class ComponentTest {

    @Autowired
    private FlowExecutor flowExecutor;

    @Test
    public void componentTest(){
        //创建一个人
        PersonEntity personEntity=new PersonEntity();
        personEntity.setName("zhf");
        personEntity.setSex(SexEnum.BOY);
        //执行回家流程
        flowExecutor.execute2Resp("goHome",personEntity);
    }
}

结果
在这里插入图片描述
可以看到打印出来的日志是按照我们的规则来执行每个组件的,如果有需求想要变更执行流程,只要修改xml文件中每个节点的顺序就好了,不需要变更代码。

本节教程简单介绍liteflow的使用,其他比如并行,选择等复杂功能的使用看后续章节。

标签:教程,LitFlow,流程,class,person,PersonEntity,liteflow,public
From: https://www.cnblogs.com/sggll/p/16788646.html

相关文章