In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you the "Tomcat9 request processing process and start deployment process example analysis", the content is simple and easy to understand, organized clearly, I hope to help you solve doubts, let Xiaobian lead you to study and learn "Tomcat9 request processing process and start deployment process example analysis" this article bar.
Overview
When the Connector starts, it starts a set of threads for different stages of the request processing process.
Acceptor thread group. Used to accept new connections, encapsulate them, and select a Poller to add new connections to Poller's event queue.
Poller thread groups. Used to listen to Socket events. When Socket is readable or writable, etc., Socket encapsulation is added to the task queue of worker thread pool.
worker thread group. It is used to process the request, including analyzing the request message and creating the Request object, and calling the pipeline of the container for processing.
ThreadPoolExecutor where Acceptor, Poller and worker are located is maintained in NioEndpoint.
Connector Init and Start
initServerSocket(), open a ServerSocket through ServerSocketChannel.open(), bound to port 8080 by default, the default connection waiting queue length is 100, when more than 100 will refuse service. We can customize it by configuring the acceptCount property of the Connector in conf/server.xml.
createExecutor() is used to create a Worker thread pool. By default, 10 Worker threads will be started, and Tomcat can process requests with no more than 200 Wokers. We can customize these two properties by configuring minSpareThreads and maxThreads of Connector in conf/server.xml.
Pollor is used to detect ready Sockets. By default, there are no more than 2, Math.min(2,Runtime.getRuntime().availableProcessors());. We can customize this by configuring pollerThreadCount.
Acceptor is used to accept new connections. The default is 1. We can customize it by configuring acceptorThreadCount.
Request ProcessAcceptor
Acceptor blocks at ServerSocketChannel.accept(); method after startup, which returns a SocketChannel when a new connection arrives.
After configuring the Socket, encapsulate the Socket into NioChannel and register it to Poller. The value mentioned is that we started multiple Poller threads at the beginning. When registering, the connection is fairly distributed to each Poller. NioEndpoint maintains an array of pollers, and when a connection is assigned to pollers[index], the next connection is assigned to pollers[(index+1)%pollers.length].
The addEvent() method adds Socket to the Poller's PollerEvent queue. The Acceptor's mission is complete.
Poller
selector.select(1000)。When Poller is started, because there is no registered Channel in the selector, it can only block when executing this method. All Pollers share a single Selector, whose implementation class is sun.nio.ch.EPollSelectorImpl
The events() method registers Sockets added to the event queue via the addEvent() method with EPollSelectorImpl, and Poller processes Sockets only when they are readable
The createSocketProcessor() method encapsulates Socket into SocketProcessor, which implements the Runnable interface. The worker thread processes the Socket by calling its run() method.
The execute(SocketProcessor) method commits the SocketProcessor to the thread pool and places it in the workQueue of the thread pool. workQueue is an instance of BlockingQueue. Poller's mission is complete.
Worker
After the worker thread is created, it executes the runWorker() method of ThreadPoolExecutor, trying to get the pending task from the workQueue, but the workQueue is empty at first, so the worker thread blocks in the workQueue.take() method.
When a new task is added to the workQueue, the workQueue.take() method returns a Runnable, usually SocketProcessor, and the worker thread calls SocketProcessor's run() method to process the Socket.
createProcessor() creates an Http11 Processor, which parses the Socket and encapsulates the contents of the Socket into a Request. Notice that Request is a temporary class whose full class name is org.apache.coyote.Request,
The postParseRequest() method encapsulates the Request and handles the mapping relationship (from URL to corresponding Host, Context, Wrapper).
CoyoteAdapter encapsulates org.apache.coyote.Request into org.apache.catalina.connector.Request before submitting Rquest to Container processing, and passes the Request to Container processing as org.apache.catalina.connector.Request.
connector.getService().getMapper().map(), used to query the mapping relationship of URLs in Mapper. The mapping relationship will be retained in org.apache.catalina.connector.Request, and the mapping host queried in this stage is used in the Container processing stage request.getHost(), and so on. request.getContext(), request.getWrapper() are all.
connector.getService().getContainer().getPipeline().getFirst().invoke() will pass the request to Container processing, of course, Container processing is also executed in the Worker thread, but this is a relatively independent module, so separate a section.
Container
It should be noted that basically every container will have multiple registered valves on the StandardPipeline, we only pay attention to the Basic Valve of each container. All other valves are executed before Basic Valve.
request.getHost().getPipeline().getFirst().invoke() Get the corresponding StandardHost first and execute its pipeline.
request.getContext().getPipeline().getFirst().invoke() Get the corresponding StandardContext first and execute its pipeline.
request.getWrapper().getPipeline().getFirst().invoke() Get the corresponding StandardWrapper first and execute its pipeline.
Standard Wrapper Basic Valve, Standard Wrapper Valve
allocate() is used to load and initialize Servlet, the value of which is mentioned that Servlet is not all singleton, when Servlet implements SingleThreadModel interface, StandardWrapper will maintain a set of Servlet instances, which is a meta-pattern. SingleThreadModel has been deprecated since Servlet 2.4.
The createFilterChain() method fetches all filters from StandardContext, and then picks out all filters matching the Request URL and adds them to the filterChain.
doFilter() executes the filter chain, and calls the Servlet's service() method when all filters have been executed.
The above is "Tomcat9 request processing flow and start deployment process sample analysis" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to 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.
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.