场景2:计算编排
1、组件风格链配置示例
定义组件
@Slf4j
@Component("a")
public class ACom implements TaskComponent {
@Override
public void run(FlowContext context, Node node) {
log.info("ACom");
}
}
@Slf4j
@Component("b")
public class BCom implements TaskComponent {
@Override
public void run(FlowContext context, Node node) {
log.info("BCom");
}
}
@Slf4j
@Component("c")
public class CCom implements TaskComponent {
@Override
public void run(FlowContext context, Node node) {
log.info("CCom");
}
}
计算编排链(以下示例,使用了简化模式)
# demo.chain.yml
id: "c1"
title: "计算编排"
layout:
- { type: "start"}
- { task: "@a"}
- { task: "@b"}
- { task: "@c"}
- { type: "end"}
2、简单脚本风格链配置示例
# demo.chain.yml
id: "c1"
title: "计算编排"
layout:
- { type: "start"}
- { task: 'System.out.println("ACom");'}
- { task: 'System.out.println("BCom");'}
- { task: 'System.out.println("CCom");'}
- { type: "end"}
3、复杂脚本风格链配置示例
使用 $
符号(或者定制其它识别符),从链的元信息里引用配置内容作为脚本。
# demo.chain.yml
id: "c1"
title: "计算编排"
layout:
- { type: "start"}
- { task: '$script1'}
- { task: '$script2'}
- { task: '$script3'}
- { type: "end"}
meta:
script1: |
if(user.id == 0) {
return;
}
System.out.println("ACom");
script2: |
System.out.println("BCom");
script3: |
System.out.println("CCom");
需要驱动定制后支持。(v3.1.3 后,将默认支持)
@Component
public class SimpleFlowDriverEx extends SimpleFlowDriver {
@Override
protected void tryAsScriptTask(FlowContext context, Task task, String description) throws Throwable {
//按脚本运行
if (description.startsWith("$")) {
String metaName = description.substring(1);
description = (String) getDepthMeta(task.getNode().getChain().getMetas(), metaName);
if (Utils.isEmpty(description)) {
throw new FlowException("Chain meta not found: " + metaName);
}
}
super.tryAsScriptTask(context, task, description);
}
}