Master Easy Work: Build and Control Java Workflows with Code, Breakpoints, and JSON

This article introduces Easy Work, a Java workflow engine, explaining its core concepts, showing how to define work units, compose complex sequential, parallel, and conditional flows, use breakpoints to pause execution, and construct workflows via JSON with complete code examples and repository links.

Architecture Digest
Architecture Digest
Architecture Digest
Master Easy Work: Build and Control Java Workflows with Code, Breakpoints, and JSON

Overview

Easy Work is a lightweight Java workflow engine. It defines a Work interface for a single unit of work and a WorkFlow interface for a composition of works. Six built‑in workflow implementations are provided: RepeatFlow, SequentialFlow, ParallelFlow, ConditionalFlow, plus additional helpers.

Creating a Work

Implement the Work interface and override String execute(WorkContext). Example:

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;
    }
}

Composing a Workflow

Using the fluent factory methods the example workflow performs:

Repeat work “a” three times.

Sequentially print “b”, “c”, “d”.

Execute “e” and “f” in parallel, automatically shutting down the parallel executor when the branch finishes.

Based on the parallel result (predicate WorkReportPredicate.COMPLETED) run “g” if successful, otherwise “h”.

Finally run “z”.

Code:

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());

Pause and Resume (Breakpoints)

Since version 1.0.5 the engine supports breakpoints. A breakpoint can be set on any work unit by name. Execution stops at the breakpoint, allowing external processing, and can be resumed later.

// pause at work identified by "C_BREAK_POINT"
flow.execute("C_BREAK_POINT");
System.out.println("paused at C_BREAK_POINT");

// resume execution until the workflow finishes
flow.execute();

JSON‑Based Workflow Definition

From version 1.0.8 a workflow can be described in JSON and deserialized at runtime. The same example expressed in 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"}
  ]
}

Deserialization and execution:

String json = ResourceReader.readJSON("json/example.json");
SequentialFlow sequentialFlow = (SequentialFlow) deserialize(json);
sequentialFlow.execute(new WorkContext());

Repository

The source code, test cases and documentation are hosted at:

https://gitee.com/ifrog/easy-work

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentJSONopen-sourcecode-exampleworkflow engine
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.