In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to realize the Tomcat template method pattern". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
When it comes to the template method pattern, it is widely used in Tomcat and in many object-oriented code.
The name is already more vivid.
Just as the core of the chain of responsibility is the passing of Chain objects, the core of this pattern is the template method.
The so-called template provides a default style. Like the idiom description of "exactly the same", the module here refers to the mold, through which multiple identical products are made, and the template method in our code also provides a default execution logic, but, as for the details of the methods used in the logic, can be provided to subclasses for detailed implementation.
For example, the following code is how the request handler is created in Tomcat:
Protected abstract P createProcessor ()
Protected abstract void initSsl (SocketWrapper socket
Processor processor)
Public SocketState process (SocketWrapper wrapper
SocketStatus status) {
Processor processor = connections.get (socket)
If (status = = SocketStatus.DISCONNECT & & processor = = null) {
/ / Nothing to do. Endpoint requested a close and there is no
/ / longer a processor associated with this socket.
Return SocketState.CLOSED
}
Try {
If (processor = = null) {
Processor = recycledProcessors.pop ()
}
If (processor = = null) {
Processor = createProcessor ()
} initSsl (wrapper, processor)
We see that the entire process method is complete processing logic, but inside this method, the Processor is created when the processor is judged to be empty, and if ssl is used, it is initialized. The point is here, where both the createProcessor method and the initSsl method are declared as abstract, leaving it to the subclass implementation.
Because Tomcat provides many types of Connector for request processing, such as blocking mode, non-blocking mode, supporting AJP protocol and so on, the template method mode is used to meet the different creation logic of many kinds of Connector.
For example, the following code is the Processor creation of HttpNio and Ajp Connector, respectively:
Public Http11NioProcessor createProcessor () {
Http11NioProcessor processor = new Http11NioProcessor (...)
Proto.configureProcessor (processor)
Register (processor)
Return processor
} protected AjpProcessor createProcessor () {AjpProcessor processor = new AjpProcessor (...)
Proto.configureProcessor (processor)
Register (processor)
Return processor
}
The same is true of the initSsl method.
The source code of Tomcat also includes a large number of places where the pattern is used, and you will inadvertently find it when you read it.
This is the end of the content of "how to implement the Tomcat template method pattern". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.