Tomcat Architecture in detail (II.)

Source: Internet
Author: User

A few days ago, we analyzed the architecture and startup process of Tomcat and began to study its workings today. Tomcat is essentially a Web server that can run Jsp/servlet, so the most typical application is that the user accesses the server through a browser, and the Tomcat receives the request and forwards it to the servlet, which, after processing by the servlet, returns the result to the client. The internal mechanism of such a complete request is specifically resolved today.

With debug, follow down and discover that the core process of Tomcat processing requests is the following:

    • Starting the endpoint,endpoint of the pre-support protocol will start a dedicated thread to listen to the corresponding protocol request, by default, the Jioendpoint,jioendpoint based on Java ServerSocket receive HTTP request
    • ServerSocket receives a client-requested socket, wraps it all the way from host to wrapper, and then requests to the appropriate servlet

The following will focus on the above two processes.

Through the previous analysis (Tomcat source analysis one) can know that when Tomcat boots will start connector, connector will be protocolhandler to start the endpoint. By default, Tomcat launches two connector, the HTTP protocol and the AJP protocol, corresponding to Http11protocol and Ajpprotocol in turn, both of which start jioendpoint. Here's a look at the start method of Jioendpoint:

[Java]View Plaincopy
  1. Public void start () throws Exception {
  2. //Initialize socket if not done before   
  3. if (!initialized) {
  4. Init ();
  5. }
  6. if (!running) {
  7. running = true;
  8. paused = false;
  9. //Create worker Collection   
  10. if (getexecutor () = = null) {
  11. Createexecutor ();
  12. }
  13. //Start acceptor threads   
  14. for (int i = 0; i < Acceptorthreadcount; i++) {
  15. Thread acceptorthread = new thread (new acceptor (), getName () + "-  acceptor-" + i);
  16. Acceptorthread.setpriority (threadpriority);
  17. Acceptorthread.setdaemon (Getdaemon ());
  18. Acceptorthread.start ();
  19. }
  20. }
  21. }

The above code is very clear to start acceptorthreadcount threads, each thread by the acceptor agent, specifically see the acceptor Run method:

[Java]View Plaincopy
  1. Public void run () {
  2. //Loop until we receive a shutdown command   
  3. while (running) {
  4. //Loop If endpoint is paused   
  5. while (paused) {
  6. Try  {  
  7. Thread.Sleep (+);
  8. } catch (interruptedexception e) {
  9. //Ignore   
  10. }
  11. }
  12. //Accept the next incoming connection from the server socket   
  13. Try  {  
  14. Socket socket = Serversocketfactory.acceptsocket (ServerSocket);
  15. Serversocketfactory.initsocket (socket);
  16. //Hand This socket off to an appropriate processor   
  17. if (!processsocket (socket)) {
  18. //Close socket right away   
  19. Try  {  
  20. Socket.close ();
  21. } catch (IOException e) {
  22. //Ignore   
  23. }
  24. }
  25. }catch (IOException x) {
  26. if  (running) log.error (sm.getstring ("Endpoint.accept.fail"), x);
  27. } catch (Throwable t) {
  28. Log.error (sm.getstring ("Endpoint.accept.fail"), T);
  29. }
  30. //The processor would recycle itself when it finishes   
  31. }
  32. }

This leads to the conclusion that Tomcat receives a client request by ServerSocket listening to the socket. The specific code does not need me to parse, a little understanding of Java net people can understand the above code, Tomcat is the most standard and the most basic socket invocation method to handle the network request. After finding the source of the processing request, the following is the thing to do is simple, hit the breakpoint, in the browser to request a simple Hello world, debug all the way down. Along the way, the sequence diagram for the main process is as follows:

From the above, the above process can be decomposed into the following three main core points:

    • Parsing and packaging of sockets based on Http1.1 protocol
    • Standardenginevalve, Standardhostvalve, standardcontextvalve and standardwrappervalve four kinds of valve inoke. Four different levels of valve have been processed and packaged at different levels
    • Applicationfilterchain implementation of filter interception and actual servlet requests based on the responsibility chain model

The above three core points are very rich research points, which will be analyzed one by one in the following days.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Tomcat Architecture in detail (II.)

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.