Mastering Liteflow: A Lightweight Rule Engine for Java Backend Development
This article introduces Liteflow, a lightweight yet powerful Java rule engine, explains its architecture, shows how to integrate it with Spring Boot, details its component types and EL rule syntax, and demonstrates a real‑world e‑commerce workflow using XML rule files.
1. Introduction
In everyday development, serial or parallel business processes often lack direct correlation, making them hard to manage with traditional strategy or template patterns due to code sprawl. Introducing a rule engine from a global perspective solves this problem, and Liteflow is the focus of this guide.
2. What is Liteflow?
Liteflow is a lightweight, powerful rule engine that works out‑of‑the‑box and can quickly compose complex rule flows. It supports multiple rule file formats such as XML, JSON, and YAML, and rule storage options like SQL, Zookeeper, Nacos, or Apollo.
3. Architecture Overview
Liteflow consists of a rule parser, component registry, and an executor. At startup it parses all rule files, registers components, and assembles execution information, resulting in high performance and detailed timing statistics.
4. Getting Started
Include the starter dependency in your Maven project:
<code><dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-spring-boot-starter</artifactId>
<version>2.10.6</version>
</dependency></code>Liteflow obtains a data context, parses the corresponding rule file, and executes the chain via the flowExecutor . Each node (component) can be implemented in various scripting languages such as Groovy, JavaScript, Python, or Lua.
5. Component Types
Ordinary Component – Implements NodeComponent , used in when and then logic. Override process for business logic, iaAccess to control entry, isContinueOnError for error handling, and isEnd to terminate the 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.
6. EL Rule File Syntax
Rules are written in EL format. Examples:
<code># Serial composition
THEN(a, b, c, d);
# Parallel composition
WHEN(a, b, c);
# Nested serial & parallel
THEN(a, WHEN(b, c, d), e);
# Switch composition
SWITCH(a).to(b, c, d);
# Conditional composition
THEN(IF(x, a), b);</code>7. Data Context
The data context carries parameters between nodes. Example execution code:
<code>LiteflowResponse response = flowExecutor.execute2Resp(
"chain1",
initialParams,
CustomContext.class
);</code>Usually the first node stores incoming parameters into the context for downstream nodes.
8. Parameter Configuration (YAML)
<code>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: true</code>9. Business Practice Example
An e‑commerce scenario where, after an order is completed, points are granted, a message is sent, and SMS and email are dispatched in parallel.
<code><?xml version="1.0" encoding="UTF-8"?>
<flow>
<chain name="test_flow">
THEN(
prepareTrade, grantScore, sendMq, WHEN(sendEmail, sendPhone)
);
</chain>
</flow></code>During execution, the initial parameters are transformed into a context object, then the defined chain runs, invoking each node accordingly.
10. Summary
Most of Liteflow’s work—rule parsing, component registration, and information assembly—occurs at startup, delivering high execution performance. It also provides detailed timing and statistics for each business step. This guide covered Liteflow’s core concepts, component implementation, EL rule syntax, data context handling, configuration, and a practical e‑commerce workflow.
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.