solon-flow - 六大特色总结
1、使用 yaml 格式
配置简洁,关系清晰
# c1.yml
id: "c1"
layout:
- { id: "n1", type: "start", link: "n2"}
- { id: "n2", type: "execute", link: "n3"}
- { id: "n3", type: "end"}
还支持简化模式(能自动推断的,都会自动处理),具体参考相关说明
# c1.yml
id: "c1"
layout:
- { type: "start"}
- { task: ""}
- { type: "end"}
2、表达式与脚本自由
# c2.yml
id: "c2"
layout:
- { 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"}
3、元信息配置,为扩展提供了无限空间
# c3.yml
id: "c3"
layout:
- { id: "n1", type: "start", link: "n2"}
- { id: "n2", type: "execute", link: "n3", meta: {cc: "demo@noear.org"}, task: "@MetaProcessCom"}
- { id: "n3", type: "end"}
通过组件方式,实现元信息的抄送配置效果
@Component("MetaProcessCom")
public class MetaProcessCom implements TaskComponent {
@Override
public void run(FlowContext context, Node node) throws Throwable {
String cc = node.getMeta("cc");
if(Utils.isNotEmpty(cc)){
//发送邮件...
}
}
}
也可通过驱动定制方式,实现抄送效果(显得重一些)
public class OaFlowDriver extends SimpleFlowDriver {
@Override
public void handleTask(FlowContext context, Task task) throws Throwable {
if (Utils.isEmpty(task.getDescription())) {
String cc = task.getNode().getMeta("cc");
if(Utils.isNotEmpty(cc)){
//发送邮件
}
} else {
super.handleTask(context, task);
}
}
}
//FlowEngine flowEngine = FlowEngine.newInstance();
//flowEngine.register(new OaFlowDriver()); //替换掉默认驱动
4、事件广播与回调支持
广播(即只需要发送),回调(即发送后要求给答复)
id: f4
layout:
- task: |
//只发送
context.<String,String>eventBus().send("demo.topic", "hello"); //支持泛型(类型按需指定,不指定时为 object)
- task: |
//发送并要求响应(就是要给答复)
String rst = context.<String,String>eventBus().sendAndRequest("demo.topic.get", "hello");
System.out.println(rst);
5、支持无状态、有状态两种应用
...
6、驱动定制(是像 JDBC 有 MySql, PostgreSQL,还可能有 Elasticsearch)
这是一个定制后的,支持基于状态驱动的流引擎效果。
StatefulFlowEngine flowEngine = new StatefulFlowEngine(StatefulSimpleFlowDriver.builder()
.stateOperator(new MetaStateOperator("actor"))
.stateRepository(new InMemoryStateRepository())
.build());
var context = new StatefulFlowContext("i1").put("actor", "陈鑫");
//获取上下文用户的活动节点
var statefulNode = flowEngine.getActivityNode("f1", context);
assert "step2".equals(statefulNode.getNode().getId());
assert StateType.UNKNOWN == statefulNode.getState(); //没有权限启动任务(因为没有配置操作员)
//提交活动状态
flowEngine.postActivityState(context, "f1", statefulNode.getNode().getId(), StateType.COMPLETED);
流程配置样例:
id: f1
layout:
- {id: step1, title: "发起审批", type: "start"}
- {id: step2, title: "抄送", meta: {cc: "吕方"}, task: "@OaMetaProcessCom"}
- {id: step3, title: "审批", meta: {actor: "陈鑫", cc: "吕方"}, task: "@OaMetaProcessCom"}
- {id: step4, title: "审批", type: "parallel", link: [step4_1, step4_2]}
- {id: step4_1, meta: {actor: "陈宇"}, link: step4_end}
- {id: step4_2, meta: {actor: "吕方"}, link: step4_end}
- {id: step4_end, type: "parallel"}
- {id: step5, title: "抄送", meta: {cc: "吕方"}, task: "@OaMetaProcessCom"}
- {id: step6, title: "结束", type: "end"}
对于驱动器的定制,我们还可以:定制(或选择)不同的脚本执行器、组件容器实现等。