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();
    }
}
相关推荐
Wang's Blog38 分钟前
Java框架快速入门: Spring Security+OAuth2之JWT核心概念与实战
java·spring·log4j
Dreams_l41 分钟前
死信队列和延迟队列介绍
java·开发语言
Bs_MoneyMagnet1 小时前
基于springboot+vue的生态果园采摘预约系统的设计与实现 源码+文档
java·vue.js·spring boot·后端·毕业设计·计算机毕业设计
麻瓜code1 小时前
【JUC】AQS enq() 自旋入队
java
Wang's Blog1 小时前
Java框架快速入门: Spring Security+OAuth2之核心角色与授权流程
java·spring·github
hai_android2 小时前
Kotlin Flow combine 源码剖析:一个 Channel 如何优雅合并多条流
android·java·kotlin
caoerzhong2 小时前
JeeWMS 开源仓库管理系统权限与域验证解析:Java WMS 如何把数据权限管到仓、货主与人
java·python·开源·vue
君顾12 小时前
西安 24 小时自助健身房系统开发实战指南
java·开发语言·健身房
用户094248568032 小时前
第9章:OpenJDK堆分代与 Serial/Parallel GC 基础
java·jvm
Wang's Blog2 小时前
Java框架快速入门: Spring Security+OAuth2之RBAC与角色分层实践
java·网络·spring