Skip to content

Flow engine usage

liyuliang5 edited this page Oct 31, 2024 · 5 revisions

Integration description

There are quickstart test cases in test folder of sources. You can run or debug to learn the usage and principle.

Basic

See https://github.com/jd-opensource/jd-easyflow/blob/master/README.md

Spring

Inject FlowEngine and use as normal bean. Demo are as follows:

    @Bean
    public FlowEngine flowEngine() {
        FlowEngineImpl flowEngineImpl = new FlowEngineImpl();
        flowEngineImpl.setFlowPath("classpath*:flow/**/*.json");
        return flowEngineImpl;
    }

Invoke demo:

    @Autowired
    private FlowEngine flowEngine;

// java code...

    FlowParam param = new FlowParam(flowId, startNodeId, bizData);
    FlowResult result = flowEngine.execute(param);

Flow definition description

Overall description

First level Second level Third level Name Description
id Flow ID Global unique
name Flow name
nodes Node list
id Node ID Unique in flow
name Node name
action Node action
exp Spel expression to execute
createExp Spel expression of creating NodeAction return value should be NodeAction type
flow Subflow definition
flowId Subflow id
startNodeId Start node(s) of subflow
post Node post handler
listeners Listener list
createExp Create expression of listener
filters Filter list
nodePreHandlerFilters Node PreHandler filter list
nodeFilters Node filter list
nodePostHandlerFilters Node PostHandler filter list
nodeActionFilters Node action filter list
runner Flow runner Default is single thread executor
properties Flow properties

'post' configuration

'post' has several configuration types as follows:

A、Fixed

1、{"to":XXX},to XXX node

B、Conditional

2、{"when":XXX, "to":XXX}

3、{"conditions":[{"when":XXX, "to":XXX},{"when":XXX, "to":XXX}]}

'when' is expression lanaguage

'conditionType' is optional: exclusive/inclusive,default is exclusive.

'defaultTo' is also optional.

C、Create expression.

4、{"createExp":XXX},execute when flow parsing. The result should be an implementation of NodePostHandler.

D、Expression

5、{"exp":XXX},execute when flow running

'to' configuration

String:If it starts with '$', it is an index: $first, $last, $previous or $next, or else it is a fix node id. Integer: Node index List:String list or Integer list Map: "exp":"XXX"

You can see configuration example in test folder.

SPEL expression variables.

Varialbles when flow running

Name Type Description
context FlowContext Flow context
param FlowParam Flow param
bizParam depend on invoker param in FlowParam
paramData Map dataMap in FlowParam
result FlowResult Flow result
bizResult depend on invoker result in FlowResult
nodeContext NodeContext node context
actionResult depend on implementation Return value of NodeAction

Other

Usage of filter and listener

Filer and listener all are pointcut control manner.

Exception process

By default, Flow engine don't catch or wrap exception, it throws original exception of business code.

Key naming conversion.

All keys of flow engine have no ':", The customize keys can start with 'XXX:' or ':'. 'XXX' is namespace.