Tomcat Server parsing (vi)--acceptor

Source: Internet
Author: User
Tags oracle documentation

Acceptor is responsible for managing the number of connections to the Tomcat server, the application of acceptor in the Tomcat server, how to implement the connection management, and after the successful socket connection is established, How to read and write content (read and write is referred to the poller mechanism to complete).
Prepare a little bit of the basics of implementing a socket connection in Java NiO: Socketchannel and Serversocketchannel
The concepts of Socketchannel and Serversocketchannel are similar to sockets and ServerSocket in the underlying block-based Java network programming. In the latter model, the server-side generates a ServerSocket object, bind binds the port number, and then the accept block waits for the client to connect to the server. Each time the connection is established successfully, a socket object is returned to indicate that the connection was established successfully. In Java NiO. The Serversocketchannel and Socketchannel models are actually corresponding to the serversocket and socket models. Server-side Open opens a serversocketchannel that will succeed at the same time as a ServerSocket object, but the resulting ServerSocket object is not port bound. So before you can listen to the network, you also need to bind the Serversocketchannel ServerSocket object to bind. Then Serversocketchannel begins to listen to the Accept network and returns a Socketchannel object after the request has been established successfully. This Socketchannel object has a corresponding socket object. Serversocketchannel is a thread-safe----at least, the Oracle documentation says so.

[Initialization of Serversocketchannel]A port number can only correspond to one socketchannel, so it can only correspond to one serversocketchannel. So this serversocketchannel is shared among multiple acceptor threads, and it is a property of Nioendpoint that is initialized in Nioendpoint. In the endpoint model, there is a bind method that is used to bind the port. The Serversocketchannel object completes initialization in this method. The part of the code that is intercepted is shown below, and the code that is not relevant to this topic is omitted. Public voidbind () throws Exception {
Serversock = Serversocketchannel.open ();socketproperties.setproperties (Serversock.socket ());inetsocketaddress addr = (getaddress () ! = NULL? Newinetsocketaddress (GetAddress (), Getport ()): Newinetsocketaddress (Getport ()));serversock.socket (). Bind (Addr,getbacklog ());serversock.configureblocking ( true);//mimic APR BehaviorServersock.socket (). Setsotimeout (Getsocketproperties (). Getsotimeout ());     } [Initialization of the acceptor thread Group]
The number of threads in the acceptor thread group is configurable, which, like the number of threads in the Poller thread group, can be configured by a configuration file, but is not required in most cases.

The initialization of the acceptor thread group is performed in Nioendpoint startinternal, which is part of the endpoint initialization.
[Connection number control]
When a new connection request arrives, how do you determine if the connection request is received?
Endpoint has a maxconnection attribute that specifies the maximum number of connections that the server can accept, which is 10000 by default.

When Nioendpoint is initialized, maxconnection is used to initialize a limitlatch, which is used to manage concurrent request connections. The understanding of Limitlatch can be referenced in the Java current package Countdownlatch.
Acceptor the running body portion of the thread, using while (true) to loop. The main is to conduct serversocket.accept monitoring. The blocking mode is used for Serversocketchannel, so when there is no connection request, the thread blocks on the Accept method.
When a new request arrives, use Countuporawaitconnection to manage the number of connections. If the number of connections has not reached the maximum value, then the value of the Limitlatch value plus 1,limitlatch indicates the current number of connections to the server. If the maximum value has been reached, let the thread wait.
The code related to connection control management is shown below.//if we have reached Max connections, waitcountuporawaitconnection ();
Socketchannel socket = NULL ;                     Try {                        //Accept The next incoming connection from the server                        //Socketsocket = serversock.accept ();                    }  Catch (IOException IoE) {                         //we didn ' t get a socketcountdownconnection ();                        //Introduce delay if necessaryErrordelay = Handleexceptionwithdelay (errordelay);                        //Re-throw                         Throw IoE;                     }                    //Successful accept, reset the error delayerrordelay = 0;
                    //Setsocketoptions () would add channel to the Poller                    //If successful                     if (Running &&!paused) {                          if (!setsocketoptions (socket)) { countdownconnection ();closesocket (socket);                        }                    }  Else {countdownconnection ();closesocket (socket);                    }When an exception occurs, use Countdownconnection to reduce the number of connections by one.
[transfer of connections]
The establishment of the management connection is almost the main work of acceptor, after the connection is established, the final job is to give poller to read and write the data. The registration of Poller is implemented in Setsocketoptions. Socketchannel.

Tomcat Server parsing (vi)--acceptor

Related Article

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.