Socket Communication for Tomcat source code analysis

Source: Internet
Author: User

 

Socket Communication for Tomcat source code analysis

All articles in this series are Tomcat 7.0 code analysis.

 

1. socket communication:

Tomcat can handle sockets in the following ways:

 

  1. Bio method: use Java to block socket communication.
  2. NIO method: Previously, bio (blocking method) was used. Now, NiO is introduced after java1.4 to provide NiO implementation.
  3. APR: for better integration with local machines and higher performance, such as some advanced system Io functions (sendfile, epoll and OpenSSL) and Local Operation Processing (shared memory, NT pipes and Unix sockets and OS level functions (Random Number Generation, system status, etc). Tomcat uses JNI to call and process socket links.
  4. The combination of AJP and ARP.
  5. AJP mode: communication through the AJP Protocol: AJP is mainly used for communication between the HTTP server of Apache and the servlet Web container. It is packet_oriented, in other words, it is sent to the browser (other Web servers) the data is packet (s), and the response from the servlet container is packet (s). In this case, if the data sent by the servlet container is binary, it is directly sent to the browser. In addition, AJP can reuse the socket connection between servlet containers to reduce the creation overhead. For details, see http://httpd.apache.org/docs/2.2/mod/mod_proxy_ajp.html.
2. Model Introduction

Connector consists of protocolhandler and a connection port. protocolhandler uses the methods described above to process sockets.

The code for selecting different protocolhandler implementation classes based on the configuration is as follows:

 

Java code
  1. /**
  2. * Set the coyote protocol which will be used by the connector.
  3. *
  4. * @ Param Protocol the coyote protocol name
  5. */
  6. Public void setprotocol (string protocol ){
  7. If (aprlifecyclelistener. isapravailable ()){
  8. If ("HTTP/1.1". Equals (Protocol )){
  9. Setprotocolhandlerclassname
  10. ("Org. Apache. Coyote. http11.http11aprprotocol ");
  11. } Else if ("AJP/1.3". Equals (Protocol )){
  12. Setprotocolhandlerclassname
  13. ("Org. Apache. Coyote. AJP. ajpaprprotocol ");
  14. } Else if (protocol! = NULL ){
  15. Setprotocolhandlerclassname (Protocol );
  16. } Else {
  17. Setprotocolhandlerclassname
  18. ("Org. Apache. Coyote. http11.http11aprprotocol ");
  19. }
  20. } Else {
  21. If ("HTTP/1.1". Equals (Protocol )){
  22. Setprotocolhandlerclassname
  23. ("Org. Apache. Coyote. http11.http11protocol ");
  24. } Else if ("AJP/1.3". Equals (Protocol )){
  25. Setprotocolhandlerclassname
  26. ("Org. Apache. Coyote. AJP. ajpprotocol ");
  27. } Else if (protocol! = NULL ){
  28. Setprotocolhandlerclassname (Protocol );
  29. }
  30. }
  31. }

 

 

The corresponding configuration example is as follows:

 

Java code
  1. <Connector Port = "8080" protocol = "HTTP/1.1"
  2. Connectiontimeout = "20000"
  3. Redirectport = "8443" type = "regxph" text = "yourobjectname"/>

 

 

Connector calls the protocolhandler object to process the socket. The main code is in startinternal () of the connector class, as follows:

 

Java code
  1. /**
  2. * Begin processing requests via this connector.
  3. *
  4. * @ Exception lifecycleexception if a fatal startup error occurs
  5. */
  6. @ Override
  7. Protected void startinternal () throws lifecycleexception {
  8. Setstate (lifecyclestate. Starting );
  9. Try {
  10. Protocolhandler. Start ();
  11. } Catch (exception e ){
  12. String errprefix = "";
  13. If (this. Service! = NULL ){
  14. Errprefix + = "service. getname (): \" "+ this. Service. getname () + "\";";
  15. }
  16. Throw new lifecycleexception
  17. (Errprefix + "" + SM. getstring
  18. ("Coyoteconnector. protocolhandlerstartfailed"), e );
  19. }
  20. Mapperlistener. Start ();
  21. }

 

The protocolhandler object starts a corresponding abstractendpoint object to create a serversocket, listen to the corresponding port of the service, and start the thread pool to process messages.

The code for the protocolhandler object to start the abstractendpoint object is in the org. Apache. Coyote. abstractprotocolhandler class, as follows:

 

Java code
  1. @ Override
  2. Public void start () throws exception {
  3. If (getlog (). isinfoenabled ())
  4. Getlog (). Info (Sm. getstring ("abstractprotocolhandler. Start ",
  5. Getname ()));
  6. Try {
  7. Endpoint. Start ();
  8. } Catch (exception ex ){
  9. Getlog (). Error (Sm. getstring ("abstractprotocolhandler. starterror ",
  10. Getname (), Ex );
  11. Throw ex;
  12. }
  13. }

 

The abstractendpoint corresponding to different protocolhandler is as follows:

Protocolhandler

Abstractendpoint

Ajpaprprotocol

Aprendpoint

Ajpprotocol

Jioendpoint

Http11aprprotocol

Aprendpoint

Http11nioprotocol

Nioendpoint

Http11protocol

Jioendpoint

See the implementation of this class for different protocol processing methods: aprendpoint, jioendpoint, and nioendpoint.

Jioendpoint adopts bio processing and nioendpoint adopts NiO processing. aprendpoint calls a large number of native methods of poll to process sockets. I will not introduce them one by one.

Finally, we draw a simple model for the three components, as shown below:

3. Relationship between server, service, and connector in Tomcat:

A server contains multiple services, and a service consists of multiple ctor.

A server corresponds to an instance of a servlet container, and a service can be composed of multiple ctor INS, but these conneins must be an engine. The engine represents an actual physical or virtual machine, because Tomcat can implement clusters, the configuration snippets are as follows:

 

Java code
  1. <Service name = "Catalina">
  2. <Connector Port = "8080" protocol = "HTTP/1.1"
  3. Connectiontimeout = "20000"
  4. Redirectport = "8443" type = "regxph" text = "yourobjectname"/>
  5. <Connector Port = "8009" protocol = "AJP/1.3" redirectport = "8443"/>
  6. <Engine...>
  7. ....
  8. </Engine>
  9. </Service>

 

Today we will talk about it. We will continue to sort out what we have already seen in the future.

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.