The following introduction is directly quoted from the official website, written very easy to understand, the address: yomahub.com/liteflow/do…
website
preface
In each of the company’s system, there are some complex business logic system, the system carries the core business logic, almost every requirements related to the core business, the core business logic, involving internal logic operations, caching operations, persistence operation, external resources, to be obtained within other systems RPC calls, etc. Over time, projects change hands several times, and maintenance costs get higher and higher. All kinds of hard code judgments, branching conditions more and more. The code abstraction, reuse rate is lower and lower, the coupling degree between each module is very high. The change of a small piece of logic will affect other modules, requiring a complete regression test to verify. If you want to change the order of business processes flexibly, you need to abstract and rewrite the methods with major code changes. Real-time hot change business processes are almost impossible to implement.
The role of LiteFlow framework
LiteFlow is designed to decouple complex logic, and is the best use if you’re writing or refactoring complex business logic. It is a lightweight, fast component process engine framework. Component choreography helps decouple business code so that each business fragment is a component, and supports hot load rule configuration for instant modification.
With LiteFlow, you need to break complex business logic into pieces of code and define a regular flow configuration. In this way, all components can be configured to perform complex flows according to your rules.
LiteFlow’s design principles
LiteFlow is designed based on workbench mode. What is workbench mode?
N workers in a certain order around a table, in order to produce parts, the production of parts can eventually be assembled into a machine, each worker only need to complete the production of their own parts, without knowing the content of other workers’ production. Each worker takes the resources needed to produce from the workbench, if the necessary resources are available on the workbench, and if not, wait until they are available. The parts made by each worker are also placed on the workbench.
This model has several benefits:
- Each worker does not have to communicate with other workers. Workers only need to care about their work content and the resources on the bench. This allows for decoupling and no differentiation between each worker.
- Even when workers switch positions, the content of their work and the resources they care about remain unchanged. This ensures the stability of each worker.
- If a worker is assigned to another workstation, the work content of the worker and the resources required remain unchanged, thus achieving worker reusability.
- Since each worker does not need to communicate with other workers, it is possible to change positions in real time as a production task is in progress: some workers can be replaced, inserted or removed so that the production task can be changed in real time. This ensures the flexibility of the entire production task.
This pattern maps to the LiteFlow framework, where the worker is the component, the order in which the worker sits is the process configuration, the workbench is the context, the resource is the parameter, and the final assembled machine is the business. Because of these features, LiteFlow can achieve unified decoupled components and flexible assembly.
The actual scene
Scenario 1
When we do the online education system, there are two ways to place an order. One is to place an order to buy the teacher’s class directly, and the other is to buy the class first and then assign the teacher. In fact, the final orders are the same, but there are no assigned teachers, but different business processes interspersed, specific:
Order process 1: place an order to the teacher 1. Obtain teacher details 2. Obtain class details 3. 4. Test class inventory 5. Create order process - fill teacher information into order form 6. Create an order process - fill the class information into the order form 7. Pay 8. Update the teacher's schedule 9. Send a message to the teacherCopy the code
Ordering process 2: placing orders for class hours 1. Obtaining class details 2. Detecting class inventory 3. Create order process - fill class information into order form 4. Pay 5. Send message to inform teaching management to arrange teachersCopy the code
You can see, although it is the same order, but the process is not the same, and some of the steps of independence can be decoupled, we developed at ordinary times meet such requirements are usually written two long, smelly code method, and the code is coupled together, poor maintainability, so far may also see the power of Liteflow, If there is another requirement: if the administrator places an order in the background system without detecting the inventory; Are we going to write a third long, smelly piece of code?
The advantages of using Liteflow to componentize each step for assembly are obvious:
Order process 3: Place order for class hours && do not test inventory 1. Obtain class details 2. Create order process - fill class information into order form 3. Pay 4. Send message to inform teaching management to arrange teachersCopy the code
All I had to do was arrange out the components to check the inventory, and the code was completely reusable. You already know what Liteflow is for
Simple example
PS: The example here is just for a simple demonstration, Liteflow features much more than that, specific to the official website documentation and sample project more comprehensive
- Let’s start by defining a data slot
/** * Define a slot (illustrated with a simple String) *@author yejunxi
* @date2021/8/28 * /
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@Data
public class OrderSlot extends AbsSlot {
/**
* 课时详情
*/
private String courseInfo;
/** * Teacher details */
private String teacherInfo;
/** * inventory */
private Integer stock;
}
Copy the code
- Define 4 components: get teacher request, get course details, check inventory, place an order
/** * Order component - Supplementary order teacher details **@author yejunxi
* @date2021/8/28 * /
@LiteflowComponent("OrderTeacherCmp")
public class OrderTeacherCmp extends NodeComponent {
@Override
public void process(a) throws Exception {
OrderSlot orderSlot = this.getSlot();
orderSlot.setTeacherInfo("Teacher Details");
System.out.println("Access to Teacher information"); }}Copy the code
/** * Order component - Supplementary order hour details **@author yejunxi
* @date2021/8/28 * /
@LiteflowComponent("OrderCourseCmp")
public class OrderCourseCmp extends NodeComponent {
@Override
public void process(a) throws Exception {
OrderSlot orderSlot = this.getSlot();
orderSlot.setCourseInfo("Class Details");
orderSlot.setStock(1);
System.out.println("Get class information"); }}Copy the code
/** * Order component - test hour inventory **@author yejunxi
* @date2021/8/28 * /
@LiteflowComponent("CheckCourseStockCmp")
public class CheckCourseStockCmp extends NodeComponent {
@Override
public void process(a) throws Exception {
OrderSlot orderSlot = this.getSlot();
String courseInfo = orderSlot.getCourseInfo();
}
/** * indicates whether the error continues to the next component. The default is false **@return* /
@Override
public boolean isEnd(a) {
System.out.println("Test inventory");
// Check whether the inventory is sufficient
OrderSlot orderSlot = this.getSlot();
return orderSlot.getStock() <= 0; }}Copy the code
/** * Order component - Create order **@author yejunxi
* @date2021/8/28 * /
@LiteflowComponent("OrderCreateCmp")
public class OrderCreateCmp extends NodeComponent {
@Override
public void process(a) throws Exception {
OrderSlot orderSlot = this.getSlot();
System.out.println("We have got the complete slot here to order:"+ orderSlot.toString()); }}Copy the code
- Defining component assembly
<flow>
<! -- Order and test inventory -->
<chain name="createOrder">
<! -- Read the teacher and course information -->
<then value="OrderTeacherCmp,OrderCourseCmp"/>
<! -- Check inventory -->
<then value="CheckCourseStockCmp"/>
<! Orders -- - >
<then value="OrderCreateCmp"/>
</chain>
<! -- Order -- don't check stock -->
<chain name="createOrderNotCheckStock">
<! -- Read the teacher and course information -->
<then value="OrderTeacherCmp,OrderCourseCmp"/>
<! Orders -- - >
<then value="OrderCreateCmp"/>
</chain>
</flow>
Copy the code
- call
String courseId = "Course ID";
// Check the inventory order
LiteflowResponse<OrderSlot> response1 = flowExecutor.execute2Resp("createOrder", courseId, OrderSlot.class);
// Do not detect inventory orders
LiteflowResponse<OrderSlot> response2= flowExecutor.execute2Resp("createOrderNotCheckStock", courseId, OrderSlot.class);
Copy the code