How LiteFlow Simplifies Complex Business Logic with a Component‑Based Flow Engine
This article introduces LiteFlow, a lightweight, fast, and stable component‑based rule engine, explains its design principles, shows step‑by‑step usage in both non‑Spring and Spring Boot environments, and dives into the core source code that drives parsing, chain execution, and node processing.
Introduction
LiteFlow is a lightweight, fast, and stable component‑based rule engine designed to decouple complex business logic. It allows developers to split intricate processes into reusable components and configure their execution order via rule files, supporting hot‑loading for instant changes.
LiteFlow’s Role
By turning each business fragment into a component, LiteFlow eliminates hard‑coded branching, reduces coupling, and makes the system easier to maintain and evolve.
Design Principles
LiteFlow follows a "workbench" model: multiple workers (components) pick resources from a shared workbench (context) and produce parts that are assembled into a final product (business outcome). This model provides decoupling, stability, reusability, and real‑time reconfiguration.
Usage – Non‑Spring Environment
Add the Maven dependency:
<code><dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-core</artifactId>
<version>2.6.13</version>
</dependency></code>Create a custom NodeComponent by extending it and overriding process . Place the XML rule file under resources and configure nodes, chains, and conditions ( <when> , <then> , etc.). Build a LiteflowConfig with the XML path, obtain a FlowExecutor , and call execute2Resp with the chain name to run the flow.
Usage – Spring Boot Environment
Add the Spring‑Boot starter dependency:
<code><dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-spring-boot-starter</artifactId>
<version>2.6.13</version>
</dependency></code>Define business nodes as Spring beans annotated with @LiteflowComponent . No explicit <node> tags are required in the XML. Configure the rule file path in application.properties , then run the same FlowExecutor call as in the non‑Spring case.
Core Components
Parser : Parses rule files (XML/JSON/YAML) into Java objects ( Node , Chain , Condition ). Supports multiple file types and storage backends (local, ZK, custom).
FlowBus : Stores parsed Node and Chain definitions.
FlowExecutor : Executes a Chain by retrieving it from FlowBus , then sequentially runs PreCondition , Then/When , and FinallyCondition nodes.
Slot : Shared context for a flow execution; holds parameters and intermediate results.
DataBus : Manages Slot instances.
Source Code Walkthrough
FlowExecutor Construction : In non‑Spring mode, a FlowExecutor is instantiated with a rule path, then init() parses the rules at startup for performance.
FlowParser Workflow : The parser loads rule file content via PathContentParser , converts XML to a DOM document, and builds Node , Condition , and Chain objects. Spring integration automatically registers beans as nodes.
Chain Execution : FlowExecutor.execute2Resp obtains a Slot , fetches the target Chain , and calls executePre , execute , and executeFinally . ThenCondition runs nodes serially, while WhenCondition runs them in parallel.
Node Execution : Each Node holds a NodeComponent . The executor checks isAccess , obtains a NodeExecutor , and finally invokes the component’s process method. Retry logic is supported, and the shared Slot captures results.
Through these steps, LiteFlow translates declarative rule files into executable Java code, enabling flexible, maintainable, and hot‑reloadable business workflows.
Sanyou's Java Diary
Passionate about technology, though not great at solving problems; eager to share, never tire of learning!
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.