How to Build and Run Complex Java Workflows with Easy Work
This guide introduces Easy Work, a Java workflow engine, explains its core concepts, shows how to create custom Work units, assemble repeat, sequential, parallel, and conditional flows, pause execution with breakpoints, and define workflows in JSON with complete code examples.
What is Easy Work?
Easy Work is a Java workflow engine that provides a concise API and modular components for creating composable workflows.
Core Concepts
Work units are represented by the Work interface, and a workflow is represented by the WorkFlow interface. Easy Work offers six implementations of WorkFlow, including RepeatFlow, SequentialFlow, ParallelFlow, and ConditionalFlow.
Creating a 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;
}
}Building a Sample Workflow
The example workflow prints “a” three times, then prints “b c d” in order, runs “e” and “f” in parallel, chooses “g” or “h” based on the parallel result, and finally prints “z”. flow1 – RepeatFlow of “a” three times flow2 – SequentialFlow of “b c d” flow3 – ParallelFlow of “e f” flow4 – ConditionalFlow that runs flow3 then g or
h flow5– SequentialFlow that composes the previous flows and ends with “z”
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());Pausing and Resuming Workflows
Since version 1.0.5 Easy Work supports breakpoints. A breakpoint can be set on any work unit, for example on work “c”. Execution can be paused at the breakpoint and later resumed.
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
);
// pause at breakpoint
flow.execute("C_BREAK_POINT");
System.out.println("execute to the break point `C_BREAK_POINT`");
// resume
flow.execute();JSON‑Based Workflow Definition
From version 1.0.8 workflows can be described in JSON. The following snippet corresponds to the earlier example.
{
"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"}},
{"type":"work.PrintMessageWork","message":"z"}
]
}Deserialization and execution are performed with:
String json = ResourceReader.readJSON("json/example.json");
SequentialFlow sequentialFlow = (SequentialFlow) deserialize(json);
sequentialFlow.execute(new WorkContext());https://gitee.com/ifrog/easy-work
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
