Introducing LiteFlow: A Lightweight Rule Engine for Java Backend Development
This article introduces the LiteFlow rule engine, explaining its architecture, supported rule file formats, component types, EL rule definitions, data context handling, configuration options, and demonstrates its application in an e‑commerce order processing scenario, highlighting hot‑deployment and performance benefits for Java backend development.
In daily development, serial or parallel business processes often arise without direct correlation. To address this, the article presents LiteFlow, a lightweight yet powerful rule engine that can be used out‑of‑the‑box to orchestrate complex workflows across Java backend services.
1. Introduction
The author, a senior architect, explains that traditional coding approaches generate many files and obscure the overall flow. LiteFlow provides a global perspective by allowing rule definitions to be managed centrally.
2. LiteFlow Rule Engine
LiteFlow supports multiple rule file formats (XML, JSON, YAML) and storage options (SQL, Zookeeper, Nacos, Apollo). Its overall architecture is illustrated with diagrams (omitted here). The engine can be integrated with Spring Boot, MyBatis‑Plus, Vue & Element to build a full‑stack backend management system.
Key features include:
Support for serial ( THEN ) and parallel ( WHEN ) execution.
Switching logic via SWITCH and conditional logic via IF .
Hot‑deployment: rule files can be updated at runtime without restarting the application.
3. Usage of LiteFlow
3.1 Components
Components correspond to nodes in rule files. Types include:
Ordinary Component : Implements NodeComponent , used in when and then logic. Methods such as iaAccess , isContinueOnError , and isEnd control execution flow.
Switch Component : Extends NodeSwitchComponent and overrides processSwitch to decide the next node, similar to a Java switch .
Condition Component : Extends NodeIfComponent and overrides processIf to return a boolean result.
3.2 EL Rule Files
Rule files are typically written in XML. Examples of rule expressions:
# 文件编排, then 代表串行执行 when 表示并行执行
# 串行编排示例
THEN(a, b, c, d);
# 并行编排示例
WHEN(a, b, c);
# 串行和并行嵌套结合
THEN( a, WHEN(b, c, d), e);
# 选择编排示例
SWITCH(a).to(b, c, d);
# 条件编排示例
THEN(IF(x, a),b );3.3 Data Context
The data context carries parameters between nodes. Execution typically looks like:
LiteflowResponse response = flowExecutor.execute2Resp("chain1", initialParams, CustomContext.class);Parameters are set in the first node and propagated through the context.
3.4 Parameter Configuration
Configuration options (in application.yml ) include rule source location, retry count, logging, monitoring, request‑id generator, slot size, thread pool settings, and hot‑load flags:
liteflow:
ruleSource: liteflow/*.el.xml
retry-count: 0
print-execution-log: true
monitor:
enable-log: true
period: 300000
request-id-generator-class: com.platform.orderserver.config.AppRequestIdGenerator
slot-size: 10240
main-executor-works: 64
when-max-wait-seconds: 15
when-max-workers: 16
when-queue-limit: 5120
parse-on-start: true
enable: true4. Business Practice
Using an e‑commerce scenario, the article demonstrates a rule chain that, after order completion, grants points, sends messages, and concurrently sends SMS and email:
<flow>
<chain name="test_flow">
THEN(
prepareTrade, grantScore, sendMq, WHEN(sendEmail, sendPhone)
);
</chain>
</flow>Diagrams (omitted) illustrate data preprocessing, node execution, and context handling.
5. Summary
Most of LiteFlow’s work—rule parsing, component registration, and assembly—is performed at startup, resulting in high execution performance. The engine can log execution time per node and provide statistics. The article covered LiteFlow’s core concepts and practical usage for Java backend developers.
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.