Why a Workflow Engine Is Essential for Scalable Business Platforms
The article explains how excessive if‑else branching in multi‑business systems harms code maintainability, and demonstrates how a workflow engine combined with a plugin extension mechanism can isolate business logic, simplify testing, and enable flexible execution chains, using the open‑source MemberClub project as a concrete example.
Problem
In a business middle‑platform that integrates many heterogeneous services, adding new business features often leads to long if‑else chains. Each new scenario adds more branches, making the code hard to understand and increasing the risk of system‑wide failures.
Solution Overview
The article proposes two complementary techniques to improve code isolation and extensibility:
Use a workflow engine to configure a separate execution chain for each business scenario.
Use a plugin‑extension engine so that each business can provide its own differential implementation.
The open‑source MemberClub project (hosted on Gitee and GitHub) demonstrates these ideas in a paid‑membership transaction system.
Flow Chain Configuration
Different membership products require different purchase‑order processes. The class DemoMemberPurchaseExtension defines three distinct flow‑chain configurations. The UI for configuring the chains is shown in the screenshot below.
Flow Node Definition
A flow node implements four lifecycle methods:
process success rollback callbackExecution Algorithm
The engine executes a flow chain by providing a context object and invoking FlowChain.execute. The algorithm processes each node in order, calling process. If an exception occurs, the engine rolls back the already executed nodes by invoking rollback in reverse order. When all process calls succeed, the engine invokes success (also in reverse order) and finally callback for each node. This pattern follows the Chain‑of‑Responsibility design.
public <T> void execute(FlowChain<T> chain, T context) {
Exception exception = null;
int index = -1;
for (FlowNode<T> node : chain.getNodes()) {
try {
node.process(context);
index++;
} catch (Exception e) {
if (e instanceof SkipException) {
CommonLog.warn("Current flow:{} sent Skip request, stop later flows", node.getClass().getSimpleName());
break;
}
exception = e;
break;
}
}
if (exception != null) {
for (int i = index; i >= 0; i--) {
FlowNode<T> node = chain.getNodes().get(i);
try {
node.rollback(context, exception);
} catch (Exception e) {
CommonLog.error("Rollback execution exception, ignore name:{}", node.getClass().getSimpleName(), e);
}
}
} else {
for (int i = index; i >= 0; i--) {
FlowNode<T> node = chain.getNodes().get(i);
try {
node.success(context);
} catch (Exception e) {
CommonLog.error("Success execution exception, ignore name:{}", node.getClass().getSimpleName(), e);
}
}
}
for (int i = index; i >= 0; i--) {
FlowNode<T> node = chain.getNodes().get(i);
try {
node.callback(context, exception);
} catch (Exception e) {
CommonLog.error("Callback execution exception, ignore name:{}", node.getClass().getSimpleName(), e);
}
}
if (exception != null) {
throw exception;
}
}The complete source code, including the workflow engine and plugin‑extension mechanism, is available in the MemberClub repository:
https://gitee.com/juejinwuyang/memberclub
https://github.com/juejin-wuyang/memberclub
Technical Stack Used in MemberClub (reference only)
MemberClub integrates common enterprise components such as MyBatis‑Plus, ShardingSphere, Redis, Apollo, Spring Cloud, RabbitMQ, Swagger, Lombok, etc., providing a practical reference for building middle‑platform services.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
