Detailed explanation of pipeline and valve in tomcat pipeline mode

Detailed explanation of pipeline and valve in tomcat pipeline mode

Preface

In a relatively complex large system, if there is an object or data flow that needs to be processed with complex logic, we can choose to perform these complex logical processes in a large component. This method does achieve the goal, but it is simple and crude. Perhaps in some cases this simple and crude approach will bring some troubles. For example, if I want to change part of the processing logic, add some processing logic to the process, or reduce some processing logic in the process, some seemingly simple changes here make it difficult for us to start, except to change the entire component. The whole system seems to have no scalability and reusability.

Is there a model that can divide the entire processing flow into details, where each small module is independent of each other and is responsible for a piece of logic processing. These logic processing small modules are connected in sequence, with the output of the previous module as the input of the next module, and the output of the last module as the final processing result. In this way, when modifying the logic, only a certain module needs to be modified, and adding or reducing processing logic can also be refined to a certain module granularity, and each module can be reused, greatly enhancing reusability. This mode is the pipeline mode to be discussed in this chapter.

As the name suggests, the pipeline mode connects multiple objects like a pipeline. The overall look is like several valves nested in the pipeline, and the processing logic is placed on the valves. As shown in the following figure, after the object to be processed enters the pipeline, it passes through valve one, valve two, valve three, and valve four respectively. Each valve will perform some logical processing on the incoming object. After layers of processing, it is processed from the end of the pipeline. At this time, the object is the target object that has been processed.

Since the pipeline mode is so useful, we hope to consider using it appropriately in the program. In order to realize this mode, multiple objects need to collaborate. Please refer to the following class diagram. The Valve interface defines the calling method of the valve. Since the valves are connected with each other using a single linked list structure, it is necessary to provide operations on next. Just implement a valve and expand it. The Pipeline interface defines the methods for operating valves in the pipeline, including methods such as getting the first valve, getting the basic valve, and adding valves. The pipeline needs to expand it.


Let's look at how to simply implement a pipeline pattern:

① Valve interface

public interface Valve {
 public Valve getNext();
 public void setNext(Valve valve);
 public void invoke(String handling);
}

② Pipeline interface

public interface Pipeline {
public Valve getFirst();
public Valve getBasic();
public void setBasic(Valve valve);
public void addValve(Valve valve);
}

③ Basic valve , the processing logic is simply to replace "aa" with "bb" in the incoming string

public class BasicValve implements Valve {
protected Valve next = null; 
public Valve getNext() {
return next;
}
public void invoke(String handling) {
    handling = handling.replaceAll("aa", "bb");
System.out.println("After basic valve processing: " + handling);
}
public void setNext(Valve valve) {
this.next = valve;
}
}

④ The second valve replaces "11" in the incoming string with "22"

public class SecondValve implements Valve {
protected Valve next = null; 
public Valve getNext() {
return next;
}
public void invoke(String handling) {
handling = handling.replaceAll("11", "22");
System.out.println("After the Second valve is handled: " + handling);
getNext().invoke(handling);
}
public void setNext(Valve valve) {
this.next = valve;
}
}

⑤ The third valve replaces "zz" in the incoming string with "yy"

public class ThirdValve implements Valve {
protected Valve next = null; 
public Valve getNext() {
return next;
}
public void invoke(String handling) {
handling = handling.replaceAll("zz", "yy");
System.out.println("After the Third valve is handled: " + handling);
getNext().invoke(handling);
}
public void setNext(Valve valve) {
this.next = valve;
}
}

⑥ For pipelines , our general operation is to set the basic valve through setBasic first, and then add other valves in sequence. The execution order is: the first added valve is executed first, and the basic valve is executed last.

public class StandardPipeline implements Pipeline {
protected Valve first = null; 
protected Valve basic = null; 
public void addValve(Valve valve) {
if (first == null) {
first = valve;
valve.setNext(basic);
} else {
Valve current = first;
while (current != null) {
if (current.getNext() == basic) {
current.setNext(valve);
valve.setNext(basic);
break;
}
current = current.getNext();
}
}
}
public Valve getBasic() {
return basic;
}
public Valve getFirst() {
return first;
}
public void setBasic(Valve valve) {
this.basic = valve;
}
}

⑦ Test class

public class Main {
public static void main(String[] args) {
String handling="aabb1122zzyy";
StandardPipeline pipeline = new StandardPipeline();
BasicValve basicValve = new BasicValve();
SecondValve secondValve = new SecondValve();
ThirdValve thirdValve = new ThirdValve();
pipeline.setBasic(basicValve);
pipeline.addValve(secondValve);
pipeline.addValve(thirdValve);
pipeline.getFirst().invoke(handling);
}
}

The output is as follows:

After the Second valve is processed: aabb2222zzyy
After the Third valve is processed: aabb2222yyyy
After the basic valve is processed: bbbb2222yyyy

This is the pipeline mode, where one or more valves are connected in the pipeline. Each valve is responsible for a part of the logical processing, and the data flows down in the prescribed order. This mode decomposes the logical processing tasks, making it easy to install and disassemble a task unit, thereby improving the scalability, reusability, mobility and flexibility of the process.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Python uses pipeline to batch read and write redis
  • Python: Detailed explanation of the use of Item Pipeline components in the Scrapy framework
  • Detailed explanation of Java using Pipeline to read and write Redis batches (hmset & hgetall)
  • Detailed explanation of the significant performance improvement of redis using pipeline (PipeLine) and batch (Batch) operations
  • Scrapy custom pipeline class implements the method of saving collected data to MongoDB
  • A brief introduction to Tomcat's overall structure
  • A solution to the abnormal exit of Tomcat caused by semaphore

<<:  Installation process of MySQL5.7.22 on Mac

>>:  A brief discussion on the binary family of JS

Recommend

25 fresh useful icon sets for download abroad

1. E-Commerce Icons 2. Icon Sweets 2 3. Mobile Ph...

An example of refactoring a jigsaw puzzle game using vue3

Preface It took two days to reconstruct a puzzle ...

Native JS implements a very good-looking counter

Today I will share with you a good-looking counte...

Detailed explanation of Mybatis special character processing

Preface: Mybatis special character processing, pr...

MySQL index leftmost principle example code

Preface I was recently reading about MySQL indexe...

VMware Workstation 14 Pro installation and activation graphic tutorial

This article shares the installation and activati...

How to install JDK8 on Windows

1. Download: http://www.oracle.com/technetwork/ja...

Implementation of element multiple form validation

In the project, form testing is often encountered...

Is mysql a relational database?

MySQL is a relational database management system....

Automatic backup of MySQL database using shell script

Automatic backup of MySQL database using shell sc...

Mini Program to Implement Slider Effect

This article example shares the specific code for...

Detailed process of changing apt source to Alibaba Cloud source in Ubuntu 18.04

Table of contents Preface: Ubuntu 18.04 changes a...

How to optimize MySQL indexes

1. How MySQL uses indexes Indexes are used to qui...

Analysis of Mysql data migration methods and tools

This article mainly introduces the analysis of My...