Source code analysis of Tomcat processing HTTP requests (below)

Source: Internet
Author: User
Document directory
  • 4.1 enable the serversocketchannel through blocking in the init interface Implementation Method
  • 4.2 start all threads in the implementation method of the Start Interface
  • 4.3 The acceptor thread receives client requests and registers read events
  • 4.5 handle interface implementation class call servlet container through adpater to generate response data
  • Conclusion 4.6

Reprinted http://www.infoq.com/cn/articles/zh-tomcat-http-request-2? Utm_source = infoq & utm_medium = related_content_link & utm_campaign = relatedcontent_articles_clk

Many open-source application servers integrate Tomcat as Web Container, And the servlet container code of Tomcat is rarely changed. In this way, the performance of these application servers depends on the performance of the connector module that Tomcat processes HTTP requests. This article first analyzes all types and usage of connector in Tomcat at the application level, and then analyzes the position of the connector module in the entire tomcat, at last, the source code of connector is analyzed in detail. In addition, we take http11nioprotocol as an example to illustrate how Tomcat constructs connector by implementing the protocolhandler interface.

The previous article address is "Tomcat processing HTTP request source code analysis (I)". This article is a series of articles.

4. How to Implement ctor

We can see from the above introduction that implementing ctor Ctor is the process of implementing the protocolhander interface.

The implementation processes of ajpaprprotocol, ajpprotocol, http11aprprotocol, http11protocol, protocol, and memoryprotocolhandler are the same as those of http11nioprotocol. The following describes how to implement the protocolhander interface in Tomcat using the http11nioproto.

Http11nioprotocol implements the protocolhander interface, which delegates all operations to the nioendpoint class, such:

In the init method of the nioendpoint class, socketserver is started in normal blocking mode:

The start method of the nioendpoint class is critical, as follows:

We can see that two threads and one thread pool are started in the Start method:

  • The acceptor thread that receives client requests (socket. accep (), the client socket is handled by the thread pool, the thread pool needs to configure the socket into non-blocking mode (socket. configureblocking (false) and register the read event with selector. The number of threads is configurable. The default value is 1.
  • Since the acceptor delegate thread registers the read event for the client socket, when the read is ready, it will enter the loop of the poller thread, which is also done by the delegate thread pool, the thread pool adds the niochannel to the concurrent1_queue <niochannel> queue. The number of threads is configurable. The default value is 1.
  • The thread pool is what the acceptor and poller thread delegate do.
4.1 enable the serversocketchannel through blocking in the init interface Implementation Method

Enable the serversocketchannel in blocking mode in the init interface implementation method.

4.2 start all threads in the implementation method of the Start Interface

The thread pool, acceptor thread, and poller thread are started in the Start method. The number of acceptor and poller threads is generally 1. Of course, the number can also be configured.

We can see that there are two ways to implement the thread pool:

  • The common queue + wait + sort y method is used by default. It is said that the actual test is more efficient than the following one.
  • Jdk1.5 built-in Thread Pool Mode
4.3 The acceptor thread receives client requests and registers read events

The acceptor thread receives client requests and delegates the thread pool to register read events.

  1. The customer request (serversock. Accept () is received in the acceptior thread ())
  2. Delegate thread pool Processing
  3. There are several statements in the run method of the worker thread in the thread pool:

In the setsocketoptions method, configure the socket to the non-blocking mode:

In the setsocketoptions method, call getpoller0 (). Register (Channel); and register the read event for the socketchannel. The register method code is as follows (Note: This is the method of the poller thread ):

The structure of attachment is as follows, which can be viewed as a shared data structure:

4.4 poller threads read requests, generate response data, and register write events
  1. In the setsocketoptions method mentioned above, after the Register Method of the poller thread is called to register a read event, the read starts when the read is ready. The following code is located in the run method of the poller thread:
  2. As you can see, after reading the code, call the processsocket method. This method will drag the read Processing Operator to the thread pool for processing (note that the niochannel is added to the thread pool, not the socketchannel ):
  3. Part of the Code in the run method in the worker thread of the thread pool is as follows (pay attention to the sentence handler. Process (socket ):
  4. Note:

  • Call hanler. Process (socket) to generate response data)
  • After the data is generated, register the write event. The Code is as follows:

4.5 handle interface implementation class call servlet container through adpater to generate response data

The handler interface in the nioendpoint class is defined as follows:

The process method uses the adapter to call servlet iner to generate the returned results. The adapter interface is defined as follows:

Conclusion 4.6

Implementing a Tomcat Connector connector is the process of implementing the protocolhander interface. Connector receives requests from the Socket Client, calls servlet iner to generate response results through the built-in thread pool, and synchronously or asynchronously returns the response results to the Socket Client. When a third-party application integrates Tomcat as a Web Container, the servlet container code is generally not moved, so the performance of connector is the key to the performance of the entire Web container.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.