Master Easy Work: Build, Run, and Serialize Java Workflows with Code Examples
Easy Work is a Java workflow engine that offers a simple API and modular components, enabling developers to create, execute, pause, and serialize complex workflows using code, JSON definitions, and rich conditional predicates, with detailed examples covering repeat, sequential, parallel, and conditional flows.
What is Easy Work?
Easy Work is a Java workflow engine that provides a concise API and modular components for building composable workflows.
In Easy Work, a work unit is represented by the Work interface and a workflow by the WorkFlow interface. The engine offers six implementations of WorkFlow (RepeatFlow, SequentialFlow, ParallelFlow, ConditionalFlow, etc.).
Creating a Simple 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;
}
}Defining a Composite Workflow
The example workflow consists of:
Repeat printing "a" three times.
Sequentially printing "b", "c", "d".
Parallel execution of "e" and "f".
Conditional execution: if the parallel step completes, run "g"; otherwise run "h".
Finally print "z".
The corresponding code uses the fluent API:
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 a Workflow (Breakpoint)
Since version 1.0.5, Easy Work supports breakpoints that allow pausing execution at any work unit and later resuming.
Example: set a breakpoint on work unit c, execute up to that point, then continue.
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"); // pause
System.out.println("execute to the break point `C_BREAK_POINT`");
flow.execute(); // resumeBuilding Workflows with JSON
From version 1.0.8, workflows can be defined in JSON. The following snippet represents the same workflow as 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"}
},
{"type":"work.PrintMessageWork","message":"z"}
]
}Deserialize and execute:
String json = ResourceReader.readJSON("json/example.json");
SequentialFlow sequentialFlow = (SequentialFlow) deserialize(json);
sequentialFlow.execute(new WorkContext());Rich Conditional Predicates
Easy Work includes a variety of built‑in Predicate implementations to simplify complex conditional logic.
PrintMessageWork ageEqualTen = new PrintMessageWork("age equal than 10");
PrintMessageWork ageLessThanTen = new PrintMessageWork("age less than 10");
UserPrintWork userWork = new UserPrintWork(new User().setAge(15).setName("john"));
aNewConditionalFlow(userWork)
.when(
andPredicate(
aNewEqPredicate(WorkReport::getStatus, "COMPLETED"),
aNewGreaterEqualPredicate("$result.$age", 10),
aNewEqPredicate("$result", User::getName, "john")
),
ageEqualTen,
ageLessThanTen
).execute(new WorkContext());https://gitee.com/ifrog/easy-work/
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.
