场景2:计算编排
1、组件风格链配置示例
定义组件
@Slf4j
@Component("a")
public class ACom implements TaskComponent {
@Override
public void run(ChainContext context, Node node) {
log.info("ACom");
}
}
@Slf4j
@Component("b")
public class BCom implements TaskComponent {
@Override
public void run(ChainContext context, Node node) {
log.info("BCom");
}
}
@Slf4j
@Component("c")
public class CCom implements TaskComponent {
@Override
public void run(ChainContext 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");
默认并不支持从链元信息引用配置,需要扩展驱动器(替换掉默认驱动器):
@Component
public class SimpleChainDriverEx extends SimpleChainDriver implements ChainDriver {
@Override
protected void tryAsScriptTask(ChainContext context, Task task, String description) throws Throwable {
if (description.startsWith("$")) {
String metaName = description.substring(1);
description = (String) task.node().chain().meta().get(metaName);
}
super.tryAsScriptTask(context, task, description);
}
}
//flowEngine.eval("c1", new ChainContext());