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

How to implement Tomcat request processing

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article analyzes "how to implement Tomcat request processing". The content is detailed and easy to understand. Friends who are interested in "how to achieve Tomcat request processing" can follow the editor's idea to read it slowly and deeply. I hope it will be helpful to you after reading. Let's learn more about "how to implement Tomcat request processing" with the editor.

Many things are already very clear in the timing diagram, so there is no need to introduce it step by step, so this paper focuses on the diagram, and then gives a simple explanation to some of the contents.

The tool used to draw graphics is PlantUML + Visual Studio Code + PlantUML Extension

The following introduction to Tomcat is based on Tomcat-9.0.0.M22.

Tomcat-9.0.0.M22 is the latest version of Tomcat, but it has not been released yet. it implements Servlet4.0 and JSP2.3 and provides many new features, requiring JDK support of 1.8 or above, and so on.

Overview

After Connector starts, it starts a set of threads for different stages of the request processing.

Acceptor thread group. Used to accept the new connection and encapsulate the new connection, select a Poller to add the new connection to the event queue of Poller.

Poller thread group. Used to listen for Socket events and add Socket to the task queue of the worker thread pool when the Socket is readable or writable, and so on.

Worker thread group. It is used to process the request, including analyzing the request message and creating a Request object, and calling the pipeline of the container for processing.

The ThreadPoolExecutor where Acceptor, Poller, and worker are located is maintained in NioEndpoint.

Connector Init and Start

InitServerSocket (), which opens a ServerSocket through ServerSocketChannel.open (), binds to port 8080 by default, and the default connection waiting queue length is 100. when it exceeds 100, it will deny service. We can customize it by configuring the acceptCount property of Connector in conf/server.xml.

CreateExecutor () is used to create a pool of Worker threads. By default, 10 Worker threads are started, and the maximum number of Woker during Tomcat processing of requests is 200. We can customize these two properties by configuring minSpareThreads and maxThreads of Connector in conf/server.xml.

Pollor is used to detect Socket that is ready. By default, there are no more than 2, Math.min (2 Magi Runtime.getRuntime (). AvailableProcessors ());. We can customize it by configuring pollerThreadCount.

Acceptor is used to accept new connections. The default is 1. We can customize it by configuring acceptorThreadCount.

Request Process

Acceptor

Acceptor blocks at the ServerSocketChannel.accept (); method after startup, which returns a SocketChannel when a new connection arrives.

After configuring the Socket, encapsulate the Socket into the NioChannel and register it to the Poller. The value mentions that we started multiple Poller threads at the beginning, and when registering, the connection is fairly allocated to each Poller. NioEndpoint maintains an Poller array, and when one connection is assigned to pollers [index], the next connection is assigned to pollers [(index+1)% pollers.length].

The addEvent () method adds Socket to the PollerEvent queue for that Poller. At this point, the task of Acceptor is complete.

Poller

Selector.select (1000). When the Poller starts, because there is no registered Channel in the selector, it can only be blocked when the method is executed. All Poller share one Selector, and its implementation class is sun.nio.ch.EPollSelectorImpl

The events () method registers the Socket that is added to the event queue through the addEvent () method to EPollSelectorImpl, and when the Socket is readable, Poller processes it

The createSocketProcessor () method encapsulates Socket into SocketProcessor, and SocketProcessor implements the Runnable interface. The worker thread processes the Socket by calling its run () method.

The execute (SocketProcessor) method submits the SocketProcessor to the thread pool and places it in the workQueue of the thread pool. WorkQueue is an instance of BlockingQueue. At this point, the task of Poller is complete.

Worker

After the worker thread is created, it executes the runWorker () method of ThreadPoolExecutor, trying to fetch the pending task from the workQueue, but at first the workQueue is empty, so the worker thread blocks in the workQueue.take () method.

When a new task is added to workQueue, the workQueue.take () method returns a Runnable, usually SocketProcessor, and the worker thread calls the run () method of SocketProcessor to process the Socket.

CreateProcessor () creates a Http11Processor that parses the Socket and encapsulates the content in the Socket into the Request. Note that this Request is a temporary class, and its full class name is org.apache.coyote.Request

The postParseRequest () method encapsulates the Request and handles the mapping (from URL to the corresponding Host, Context, Wrapper).

Before CoyoteAdapter submits the Rquest to Container for processing, it encapsulates the org.apache.coyote.Request into org.apache.catalina.connector.Request, and the Request passed to Container for processing is org.apache.catalina.connector.Request.

Connector.getService (). GetMapper (). Map (), which is used to query the mapping of URL in Mapper. The mapping relationship is retained in org.apache.catalina.connector.Request, and the Container processing phase request.getHost () uses the mapping host queried at this stage, and so on, both request.getContext () and request.getWrapper ().

Connector.getService (). GetContainer (). GetPipeline (). GetFirst (). Invoke () passes the request to Container processing, and of course Container processing is also performed in the Worker thread, but this is a relatively independent module, so it is divided into separate sections.

Container

It is important to note that basically there will be multiple registered Valve on the StandardPipeline of each container, and we only focus on the Basic Valve of each container. Other Valve is executed before Basic Valve.

Request.getHost (). GetPipeline (). GetFirst (). Invoke () first gets the corresponding StandardHost and executes its pipeline.

Request.getContext (). GetPipeline (). GetFirst (). Invoke () first gets the corresponding StandardContext and executes its pipeline.

Request.getWrapper (). GetPipeline (). GetFirst (). Invoke () first gets the corresponding StandardWrapper and executes its pipeline.

What is most worth talking about is StandardWrapper's Basic Valve,StandardWrapperValve.

Allocate () is used to load and initialize Servlet. The value mentions that Servlet is not always singleton. When Servlet implements the SingleThreadModel interface, StandardWrapper maintains a set of Servlet instances, which is the shared meta pattern. Of course, SingleThreadModel has been abandoned since Servlet 2.4.

The createFilterChain () method fetches all the filters from the StandardContext and then picks out all the filters that match the Request URL and adds them to the filterChain.

DoFilter () executes the filter chain and calls the service () method of Servlet when all the filters have been executed.

On how to achieve Tomcat request processing to share here, I hope that the above content can make you improve. If you want to learn more knowledge, please pay more attention to the editor's updates. Thank you for following the website!

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