Backend Development 12 min read

Introducing LiteFlow: A Lightweight Rule Engine for Java Backend Applications

This article presents LiteFlow, a lightweight yet powerful rule engine built on Spring Boot, explains its architecture, component types, EL rule file syntax, data context handling, configuration options, and demonstrates a practical e‑commerce workflow example with full code snippets and deployment tips.

Top Architect
Top Architect
Top Architect
Introducing LiteFlow: A Lightweight Rule Engine for Java Backend Applications

In daily development, serial or parallel business processes often lack direct correlation, making rule orchestration challenging; LiteFlow is introduced as a lightweight rule engine that addresses this by providing a global, configurable solution.

LiteFlow is a compact and powerful rule engine that can be used out‑of‑the‑box to quickly compose complex rule flows. It supports multiple rule file formats (XML, JSON, YAML) and various storage backends such as SQL, Zookeeper, Nacos, and Apollo.

The overall architecture of LiteFlow is shown in the diagram below:

Using LiteFlow starts with obtaining a context, parsing the corresponding rule file, and executing the chain via the FlowExecutor. Each chain contains business nodes (components) that can be written in various scripting languages such as Groovy, JavaScript, Python, or Lua, and nodes are independent of each other.

The official Maven dependency is:

<dependency>
    <groupId>com.yomahub</groupId>
    <artifactId>liteflow-spring-boot-starter</artifactId>
    <version>2.10.6</version>
</dependency>

LiteFlow can express complex flows, for example:

LiteFlow also supports hot‑deployment, allowing rule files to be updated in real time without restarting the application.

Component types in LiteFlow include:

Ordinary Component : Implement NodeComponent , used in when and then logic. Override iaAccess , isContinueOnError , and isEnd as needed.

Switch Component : Extend NodeSwitchComponent and implement processSwitch to decide the next node, similar to a Java switch .

Condition Component : Extend NodeIfComponent and override processIf to return true/false and select the subsequent node.

Example switch and condition expressions:

# flow rule expression – switch component
SWITCH(a).to(b, c);
# processSwitch must return "b" or "c"
# flow rule expression – condition component
IF(x, a, b);

EL rule files are typically written in XML. Sample snippets:

# Serial composition example
THEN(a, b, c, d);
# Parallel composition example
WHEN(a, b, c);
# Nested serial and parallel
THEN(a, WHEN(b, c, d), e);
# Switch composition
SWITCH(a).to(b, c, d);
# Conditional composition
THEN(IF(x, a), b);

Data context is crucial in LiteFlow for passing parameters between nodes. Execution example:

LiteflowResponse response = flowExecutor.execute2Resp("chain1", initialParams, CustomContext.class);

Configuration options (application.yml) include rule source location, retry count, execution 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: true

A practical e‑commerce scenario demonstrates LiteFlow in action: after an order is completed, the flow grants points, sends messages, and concurrently sends SMS and email. The XML chain definition looks like:

<flow>
    <chain name="test_flow">
        THEN(
            prepareTrade, grantScore, sendMq, WHEN(sendEmail, sendPhone)
        );
    </chain>
</flow>

Each business step is implemented as a node component, with context objects handling input/output parameters, and nodes can be hot‑replaced by updating the rule file.

In summary, LiteFlow performs most initialization at startup—parsing rules, registering components, and assembling metadata—resulting in high execution performance while providing detailed timing and statistics for each business step.

Javarule engineWorkflowbackend developmentSpring BootLiteFlow
Top Architect
Written by

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.

0 followers
Reader feedback

How this landed with the community

login 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.