Easy Work 是什么?
Easy Work 是一个用于 Java 的工作流引擎。它提供了简洁的 API 和 构建模块,便于创建和运行可组合的工作流。
在Easy Work中,工作单元由Work接口表示,工作流则由WorkFlow接口表示。Easy Work 提供了 WorkFlow 接口的6种实现方式:
这些是使用Easy Work创建工作流时所需了解的唯一基本流程。
你无需学习复杂的符号或概念,只需掌握几个易于理解的自然API即可。
如何使用 ?
首先,让我们创建一个 Work:
public class PrintMessageWork implements Work {
private final String message;
public PrintMessageWork(String message) {
this.message = message;
}
@Override
public String execute(WorkContext workContext) {
System.out.println(message);
return message;
}
}
此 Work 将指定消息打印至标准输出。现在假设我们想要创建如下工作流程:
- 打印 a 三次
- 顺序打印 b c d
- 并行执行 e f
- 如果并行执行的结果成功,执行 g, 否则执行 h
- 最后执行 z
此工作流程说明如下:
flow1是一个 打印 a 的RepeatFlow,连续执行三次flow2是一个 依次打印 b c d 的SequentialFlow, 按照顺序依次执行flow3是一个 并行打印 e f 的ParallelFlow, 同时执行flow4是一个 基于条件判断的ConditionalFlow,首先执行flow3,如果执行成功(状态为 COMPLETE)则执行 g,否则执行 hflow5是一个 顺序执行流程SequentialFlow,保证 顺序执行flow1flow2flow4,最后执行 z
使用Easy Work,此工作流可以通过以下代码段实现:
PrintMessageWork a = new PrintMessageWork("a");
PrintMessageWork b = new PrintMessageWork("b");
PrintMessageWork c = new PrintMessageWork("c");
PrintMessageWork d = new PrintMessageWork("d");
PrintMessageWork e = new PrintMessageWork("e");
PrintMessageWork f = new PrintMessageWork("f");
PrintMessageWork g = new PrintMessageWork("g");
PrintMessageWork h = new PrintMessageWork("h");
PrintMessageWork z = new PrintMessageWork("z");
WorkFlow flow = aNewSequentialFlow(
aNewRepeatFlow(a).times(3),
aNewSequentialFlow(b,c,d),
aNewConditionalFlow(
aNewParallelFlow(e,f).withAutoShutDown(true)
).when(
WorkReportPredicate.COMPLETED,
g,
h
),
z
);
aNewWorkFlowEngine().run(flow, new WorkContext());
暂停工作流
从 v1.0.5开始 EasyWork工作流支持 断点, 通过断点 你可以暂停执行工作流,做一些其他事情然后恢复执行工作流。
你可以在工作流的任何位置配置断点。
例如,你可以将断点设置在c工作单元上,然后恢复执行。
此工作流说明如下:
此工作流可以通过以下代码段实现:
SequentialFlow flow = aNewSequentialFlow(
aNewRepeatFlow(a).times(3),
aNewSequentialFlow(b,aNamePointWork(c).point("C_BREAK_POINT"),d),
aNewConditionalFlow(
aNewParallelFlow(e,f).withAutoShutDown(true)
).when(
WorkReportPredicate.COMPLETED,
g,
h
),
z
);
//执行到改断点并暂停
flow.execute("C_BREAK_POINT");
System.out.println("execute to the break point `C_BREAK_POINT`");
//从断点处继续执行,直到完成
flow.execute();
JSON构建工作流
从 V1.0.8开始 EasyWork工作流支持基于 JSON 的构建,你可以通过 JSON 的方式构建任意复杂的工作流。
基于以上图示中的例子,通过 JSON 构建工作流的片段为(example.json):
{
"type": "sequential",
"works": [{
"type": "repeat",
"times": 3,
"work": {
"type": "work.PrintMessageWork",
"message": "a"
}
},{
"type": "sequential",
"works": [{
"type": "work.PrintMessageWork",
"message": "b"
},{
"type": "work.PrintMessageWork",
"message": "c"
},{
"type": "work.PrintMessageWork",
"message": "d"
}]
},{
"type": "conditional",
"decide": {
"type": "parallel",
"autoShutdown": true,
"works": [{
"type": "work.PrintMessageWork",
"message": "e"
},{
"type": "work.PrintMessageWork",
"message": "f"
}]
},
"predicate": {
"left" : "$status",
"operator": "eq",
"right": "COMPLETED"
},
"trueWork": {
"type": "work.PrintMessageWork",
"message": "g"
},
"falseWork": {
"type": "work.PrintMessageWork",
"message": "h"
}
}],
"then": {
"type": "work.PrintMessageWork",
"message": "z"
}
}
你可以通过以下代码反序列化工作流,并进行执行(更多例子请参考test/java/DeserializeTest)
String json = ResourceReader.readJSON("json/example.json");
SequentialFlow sequentialFlow = (SequentialFlow) deserialize(json);
sequentialFlow.execute(new WorkContext());
这不是一个非常有用的工作流,只是为了让你了解如何使用Easy Work而编写工作流。
你可以在 test/java 中 查看更多的测试用例。
更详细的信息,请参考 EasyWork