solon-flow 你好世界!

solon-flow 是一个基础级的流处理引擎(可用于业务规则、决策处理、计算编排、流程审批等......)。提供有 "开放式" 驱动定制支持,像 jdbc 有 mysql 或 pgsql 等驱动,可为不同的应用场景定制不同的驱动处理。

1、Helloworld

1.1、新建项目

可以用 Solon Initializr 新建 solon-lib 模板项目,之后添加 solon-flow 依赖(也支持在 spring 或 vert.x 或 jfinal 项目里使用)。

xml 复制代码
<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon-flow</artifactId>
</dependency>

1.2、添加配置

app.yml 里添加应用配置,指定 flow 为流处理配置存放地:

yaml 复制代码
solon.flow:
  - "classpath:flow/*"

添加流处理配置(支持 json 或 yml 格式),例: flow/demo1.chain.yml

yaml 复制代码
id: "c1"
nodes: 
  - { id: "n1", type: "start", link: "n2"}
  - { id: "n2", type: "execute", link: "n3", task: "System.out.println(\"hello world!\");"}
  - { id: "n3", type: "end"}

示意图:

1.3、代码应用

应用启动时,可以打印:hello world!

java 复制代码
@Component
public class DemoCom implements LifecycleBean {
    @Inject 
    private FlowEngine flowEngine;
    
    @Override
    public void start() throws Throwable {
        flowEngine.eval("c1");
    }
}

2、转换 drools 的规则参考

配置业务处理规则:flow/r1.chain.yml。使用 solon-flow 的简化模式,很适配转换 drools dsl 的规则:

yaml 复制代码
id: "r1"
title: "评分规则"
nodes:
  - { type: "start"}
  - { when: "order.getAmount() >= 100", task: "order.setScore(0);"}
  - { when: "order.getAmount() > 100 && order.getAmount() <= 500", task: "order.setScore(100);"}
  - { when: "order.getAmount() > 500 && order.getAmount() <= 1000", task: "order.setScore(500);"}
  - { type: "end"}

规则应用:

java 复制代码
@Component
public class DemoCom {
    @Inject 
    FlowEngine flowEngine;
    
    //评分
    public int score(Order order) {
        //执行
        ChainContext ctx = new ChainContext();
        ctx.put("order", order); //模型数据,可被规则直接使用
        
        flowEngine.eval("r1", ctx);
        
        //获取评分结果
        return order.getScore();
    }
}
相关推荐
Micro麦可乐8 分钟前
最新Spring Security实战教程(十五)快速集成 GitHub 与 Gitee 的社交登录
java·spring boot·spring·gitee·github·spring security·社交登陆
u0104058369 分钟前
企业微信会话存档API对接中的敏感信息审计日志架构(Java版)
java·架构·企业微信
Voyager First12 分钟前
ApereoCas学习系列一——从github克隆cas-server源码并启动
java
像少年啦飞驰点、12 分钟前
Spring Boot 从入门到实践:快速构建一个 RESTful API 服务
java·spring boot·后端开发·快速入门·restful api·编程小白
编程彩机12 分钟前
互联网大厂Java面试:从Spring Cloud到Kafka的技术场景深度解析
java·spring cloud·微服务·kafka·技术面试
波波00713 分钟前
每日一题:.NET 中什么是 LOH(大对象堆)?为什么频繁使用大数组或大字符串可能导致性能问题?如何优化?
java·jvm·算法
Gofarlic_OMS14 分钟前
Fluent许可证使用合规性报告自动化生成系统
java·大数据·运维·人工智能·算法·matlab·自动化
熊文豪16 分钟前
Tomcat+cpolar 让 Java Web 应用随时随地可访问
java·前端·tomcat·cpolar
南棱笑笑生17 分钟前
20260123让天启AIO-3576Q38开发板在天启Buildroot下适配摄像头模块8ms1m【预览】
java·前端·数据库·rockchip
大腿不要的腿毛17 分钟前
idea 导入tomcat项目,springMvc项目,static 文件报红,JSP文件include报红
java·tomcat·intellij-idea