Liteflow component process engine framework

Time:2022-1-3

preface

Product requirements:

Take Jingdong of Dongge’s house as an example. Now I have a group of good guys who only need 998 Every 618, there will be a big promotion 668, double 11 888, double 12 180

Primary development

See the demand shout, so simple look down on me? So the meal was as fierce as a tiger Wrote:

      if ("618".equals(day)){
            System.out.println("668");
        }else if ("11.11".equals(day)){
            System.out.println("888");
        }else if ("12.12".equals(day)){
            System.out.println("180");
        }else{
            System.out.println("998");
        }

Intermediate development

Liteflow component process engine framework

image.png

Good guy, you are brother ifelse, so you come up with a strategy

public interface NodeComponent {
  void  process() ;
}

@Component
public class AComponent extends NodeComponent {
    @Override
    public void process() {
        /**
         *Logical omission here
         * 
         */
        System.out.println("618");
    }
}
@Component
public class BComponent extends NodeComponent {
    @Override
    public void process() {
        /**
         *Logical omission here
         * 
         */
        System.out.println("11.11");
    }
}
//12.12 is omitted below

//Mainstream process
@Component
public class TestFlow {

    @Resource
    private Map<String, NodeComponent> map;

    public void run(String name) throws Exception {
        for (Map.Entry<String, NodeComponent> entry : map.entrySet()) {
            String key = entry.getKey();
            if (StringUtils.equals(name,key)){
                entry.getValue().process();
            }
        }
    }
}

Something’s wrong with the product again

I also have a system requirement as follows:
You need to perform verification before query. First perform a verification – > b verification – > C verification – > d verification. What should I do at this time Chain of responsibility?
So if the process changes
B check – > a check – > C check – > d check
perhaps
B check – > d check – > a check – > C
Think about it? Do you want to make a meal? Don’t worry, here comes the new thing Look down > > > > > > > >

Liteflow comes on stage

Liteflow component process engine framework

image.png

About liteflow

Liteflow is designed to decouple complex logic. If you want to write or reconstruct complex business logic, liteflow is the most appropriate. It is a lightweight and fast component-based process engine framework, which helps decouple business code, make each business segment a component, and support hot loading rule configuration to realize immediate modification.

Using liteflow, you need to split the complex business logic into small components according to code fragments, and define a rule process configuration. In this way, all components can be configured according to your rules for complex flow.

advantage:

  • Editing weapon
  • Regular lightweight
  • Elegant and stable
  • Flexible expansion

characteristic:

  • It is a powerful tool for decoupling complex business and provides a unified implementation protocol for all components
  • The process is arranged based on the rule file and can be hot arranged
  • The framework supports zookeeper process configuration and push the modified content immediately
  • It can freely extend and configure persistent sources and provide extension interfaces
  • It supports the automatic assembly of spring boot, spring configuration and non spring projects
  • It provides two modes: serial and parallel, and provides common expression statements
  • Provide stepless nested sub process mode
  • Data slot high concurrency isolation mechanism
  • With simple monitoring, you can know the running time of each component

architecture design

Liteflow component process engine framework

image.png

What scenarios does liteflow apply to

Liteflow is applicable to businesses with complex logic, such as price engine, order placing process, rule verification, etc. these businesses often have many steps. These steps can be divided into independent components according to business granularity for assembly reuse and change. Using liteflow, you will get a system with high flexibility and strong scalability. Because the components are independent of each other, the risk of moving the whole body by changing one place can also be avoided.

Gitee warehouse of liteflow:https://gitee.com/dromara/liteFlow
GitHub warehouse in liteflow:https://github.com/dromara/liteflow

Use document address

Liteflow integration in spring boot

  • Introduce dependency
     <dependency>
          <groupId>com.yomahub</groupId>
            <artifactId>liteflow-spring-boot-starter</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
  • Write process node
@Component("a")
public class AComponent extends NodeComponent {
    @Override
    public void process() {
        String str = this.getSlot().getRequestData();
        System.out.println(str);
        System.out.println("Acomponent executed!");

        this.getSlot().setOutput(this.getNodeId(), "A component output");
    }

}

@Component("b")
public class BComponent extends NodeComponent {
    @Override
    public void process() {
        try {
            Thread.sleep(400L);
            String[] temp = new String[1000];
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Bcomponent executed!");

    }

}
  • Process test
@Component
public class TestFlow{

    @Resource
    private FlowExecutor flowExecutor;

    @Override
    public void run(String... args) throws Exception {
        Slot slot = flowExecutor.execute("chain3", "it's a request");
        System.out.println(slot);
    }
}

  • Create flow XML file Src / main / resources / config / flow xml

    Liteflow component process engine framework

    image.png
<?xml version="1.0" encoding="UTF-8"?>
<flow>
    <chain name="chain1">
        <then value="a,cond(b|d)"/> <!--  Cond is a conditional node. It determines whether to route to node B or node d according to the logic in cond. Multiple -- > can be configured
        <then value="e,f,g"/>
    </chain>

    <chain name="chain2">
        <then value="a,c"/> <!--  Then stands for serial -- >
        <when value="b,d"/> <!--  When means serial -- >
        <then value="e,f,g"/>
    </chain>
</flow>
  • application.properties
liteflow.rule-source=config/flow.xml
#------The following are not required-------
#The number of slots. The default value is 1024
liteflow.slot-size=2048
#The maximum waiting time of an asynchronous thread is seconds (when only), and the default value is 15
liteflow.when-max-wait-second=20
#Whether to enable monitoring log printing. The default value is false
liteflow.monitor.enable-log=true
#The storage size of the monitoring queue is 200 by default
liteflow.monitor.queue-limit=300
#Monitor how much execution is delayed at the beginning. The default value is 300000 milliseconds, that is, 5 minutes
liteflow.monitor.delay=10000
#Monitoring log printing is executed every time. The default value is 300000 milliseconds, that is, 5 minutes
liteflow.monitor.period=10000

summary

1) Introduce dependency
2) Define process node inheritance nodecomponent 
3) Injection:   
      @Resource 
      private FlowExecutor flowExecutor;
4)application.properties:
      liteflow.rule-source=config/flow.xml

Usage recommendations:

  • When you run out of it, you can only say the big guy’s beef batch Thank you for this framework Some cumbersome business processes are finally not afraid of change
  • When using the responsibility chain mode and strategy mode in daily development, you can also try to introduce the current framework, which is relatively simple and elegant

After using up, the boss shouted for everyone to stand up

Liteflow component process engine framework

image.png