LiteFlow可配置规则引擎使用

373 阅读1分钟

liteFlow可以通过对任务的配置进行业务规则调整:官网liteflow.yomahub.com/

依赖

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

配置

组件的定义

在依赖了以上jar包后,你需要定义并实现一些组件,确保SpringBoot会扫描到这些组件并注册进上下文。

@Component("a")
public class ACmp extends NodeComponent {

    @Override
    public void process() {
    	//do your business
    }

}

以此类推再分别定义b,c组件:

@Component("b")
public class BCmp extends NodeComponent {

    @Override
    public void process() {
    	//do your business
    }

}


@Component("c")
public class CCmp extends NodeComponent {

    @Override
    public void process() {
    	//do your business
    }

}

SpringBoot配置文件

然后,在你的SpringBoot的application.properties或者application.yml里添加配置(这里以properties为例,yaml也是一样的)

liteflow.rule-source=config/flow.el.xml

规则文件的定义

同时,你得在resources下的config/flow.el.xml中定义规则:

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

执行

声明启动类:

@SpringBootApplication
//把你定义的组件扫入Spring上下文中
@ComponentScan({"com.xxx.xxx.cmp"})
public class LiteflowExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(LiteflowExampleApplication.class, args);
    }
}

然后你就可以在Springboot任意被Spring托管的类中拿到flowExecutor,进行执行链路:

@Component
public class YourClass{
    
    @Resource
    private FlowExecutor flowExecutor;
    
    public void testConfig(){
        LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
    }
}