How Tomcat works-V. tomcat startup (4), tomcatworks

Source: Internet
Author: User

How Tomcat works-V. tomcat startup (4), tomcatworks

The preceding three steps show that the tomcat listening port finally receives the request.

Directory
  • Connector
  • Http11Protocol
  • JIoEndpoint
  • Summary

After the previous Initialization is complete, initialize ctor, and also execute some lifecycle methods.

Connector

The main function of this class during startup is to initialize and start CoyoteAdapter and Http11Protocol:

InitInternal
  • Create a CoyoteAdapter and call protocolHandler. setAdapter to set the adapter (the adapter. service method will be called after protocol creates processor)
  • Initialize protocolHandler (this instance is obtained in Connector constructor). Actually, the super-class init method is called.
  • Initialize mapperListener

StartInternal

Easy to use

  • Start protocolHandler
  • Start mapperListener

Http11Protocol

As mentioned in section 1 above, Connector draws a class diagram about the protocol in tomcat. Different protocols have different connectors. Here we mainly talk about http11, and ajp is similar. The main function of this class is to start a JIoEndpoint (its internal class Acceptor is the class for the final startup thread to receive requests ).

Note the following constructor:

  • New JIoEndpoint ()
  • New Http11ConnectionHandler (), which is an internal class of Http11Protocol and obtains processor to process requests.
  • (JIoEndpoint) endpoint). setHandler sets handler in the endpoint
Init

Although it is an instance of Http11Protocol, The init method of the parent class is executed. The main operation is to initialize actprotocol. init In the init method of the parent class.

  • Register MBean
  • Call JIoEndpoint. init (this class is in) to initialize JIoEndpoint
Start
  • Endpoint. start () start JIoEndpoint
JIoEndpoint

At the end of tomcat startup, the thread listening port needs to be started to receive requests. Inheritance relationship

Therefore, many parent methods are used. This function is to create a socket and bind it to the corresponding port.

After binding to the port, you can call socket. accept () to accept the request.

The startInternal method performs the following operations:

  • If Executor (used to execute the processor thread) is null, a new
  • Start the Acceptor thread and accept the request in this thread.
  • Start a request timeout detection thread JIoEndpoint $ AsyncTimeout
Protected class Acceptor extends actendpoint. acceptor {@ Override public void run () {int errorDelay = 0; // Loop until we receive a shutdown command while (running) {// Loop if endpoint is paused while (paused & running) {state = AcceptorState. PAUSED; try {Thread. sleep (50);} catch (InterruptedException e) {// Ignore} if (! Running) {break;} state = AcceptorState. RUNNING; try {// if we have reached max connections, wait countUpOrAwaitConnection (); Socket socket = null; try {// Accept the next incoming connection from the server // socket = serverSocketFactory. acceptSocket (serverSocket);} catch (IOException ioe) {countDownConnection (); // Introduce delay if necessary errorDelay = handleExceptionWithDelay (er RorDelay); // re-throw ioe;} // Successful accept, reset the error delay errorDelay = 0; // Configure the socket if (running &&! Paused & setSocketOptions (socket) {// Hand this socket off to an appropriate processor if (! ProcessSocket (socket) {countDownConnection (); // Close socket right away closeSocket (socket) ;}} else {countDownConnection (); // Close socket right away closeSocket (socket) ;}} catch (IOException x) {if (running) {log. error (sm. getString ("endpoint. accept. fail "), x) ;}} catch (NullPointerException npe) {if (running) {log. error (sm. getString ("endpoint. accept. fail "), npe) ;}} catch (Throwable t) {ExceptionUtils. handleThrowable (t); log. error (sm. getString ("endpoint. accept. fail "), t) ;}} state = AcceptorState. ENDED ;}}View Code

I finally saw the coveted socket. accept! Although it is only for the last step, it is very important for tomcat to waste so many twists and turns:

  • The Lifecycle used to manage the Lifecycle provides the standard and basic implementation for the Lifecycle of the container.
  • The container architecture is easy to expand. Multiple hosts can be distributed. Multiple webapps are deployed in multiple contexts. Wrapper is used as the servlet's direct container (for example, if it is SingleThreadModel, multiple servlet instances are required)
  • Connectors that can handle multiple protocols
Summary

Here, the tomcat startup process is complete. Of course, there are still a lot of details in it, but it is worth studying carefully, for example: server. xml parsing, web. xml parsing (this can also understand the functions of configuration at ordinary times), how is tomcat's hot loading function implemented (right, a thread is started during the Startup Process) and so on. These are all taken notes and I will sort them out later.

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.