Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of tomcat Pipeline Mode pipeline and valve

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the tomcat pipeline mode pipeline and valve example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Preface

In a large and complex system, if there is an object or data stream that needs complicated logical processing, we can choose to do these complicated logical processing in a large component, which does achieve its goal, but it is simple and rough. Maybe in some cases this simple and rude approach will cause some trouble, for example, I want to change some of the processing logic, I want to add some processing logic to the process, and when I want to reduce some processing logic in the process, there are some seemingly simple changes that make it impossible for us to start, except to make changes to the entire component. The whole system does not seem to have any scalability and reusability.

Whether there is a mode that can divide the whole processing flow in detail, each small module is independent of each other and is responsible for a section of logical processing, and these logical processing modules are connected according to order. The former takes the output of the module as the input of the latter module, and the output of the last module is the final processing result. In this way, the logic can only be modified for a certain module, and the addition or reduction of processing logic can also be refined to a certain module granularity, and each module can be reused, and the reusability is greatly enhanced. This model is the pipeline model to be discussed in this chapter.

As the name implies, the pipeline mode is like a pipe that connects multiple objects, and the whole looks like several valves nested in the pipeline, while the processing logic is placed on the valve. As shown in the figure below, after the objects that need to be processed enter the pipeline, they go through valve 1, valve 2, valve 3, and valve 4 respectively. Each valve will carry out some logical processing on the incoming object, and after layer by layer of processing, it will be processed from the end of the pipe. The object at this time is the target object that has completed the processing.

Since the pipeline mode is so useful, we hope that it can be properly considered in the program. In order to achieve this pattern, multiple objects need to work together, please refer to the following class diagram. The Valve interface defines the method of calling the valve. Because the valve and the valve are connected with a single linked list structure, it is necessary to provide the operation of next to achieve the expansion of a valve. The Pipeline interface defines the method for the pipeline to operate the valve, including obtaining the first valve, obtaining the base valve, adding the valve, etc., and the pipeline needs to expand it.

Look down 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);}

② pipe 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 simply replaces "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 handling:" + handling);} public void setNext (Valve valve) {this.next = valve;}}

④ the second valve, replacing "11" with "22" in the passed-in string

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 Second valve handling:" + handling); getNext (). Invoke (handling);} public void setNext (Valve valve) {this.next = valve;}}

The third valve of ⑤, replacing "zz" with "yy" in the passed string

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 Third valve handling:" + handling); getNext (). Invoke (handling);} public void setNext (Valve valve) {this.next = valve;}}

⑥ pipeline, our general operation is to first set the base valve through setBasic, and then add other valves in order, the order of execution is: add in first, execute first, and then execute the base valve.

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 Second valve treatment: after aabb2222zzyyThird valve treatment: after aabb2222yyyy basic valve treatment: bbbb2222yyyy

This is the pipeline mode, in which one or more valves are connected in the pipe, each valve is responsible for part of the logical processing, and the data flows down in a specified order. This mode decomposes the logical processing task, which is convenient for the installation and disassembly of a task unit, and improves the expansibility, reusability, mobility and flexibility of the process.

The above is all the content of the article "sample Analysis of tomcat Pipeline pattern pipeline and valve". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report