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
- Public void start () throws Exception {
- //Initialize socket if not done before
- if (!initialized) {
- Init ();
- }
- if (!running) {
- running = true;
- paused = false;
- //Create worker Collection
- if (getexecutor () = = null) {
- Createexecutor ();
- }
- //Start acceptor threads
- for (int i = 0; i < Acceptorthreadcount; i++) {
- Thread acceptorthread = new thread (new acceptor (), getName () + "- acceptor-" + i);
- Acceptorthread.setpriority (threadpriority);
- Acceptorthread.setdaemon (Getdaemon ());
- Acceptorthread.start ();
- }
- }
- }
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
- Public void run () {
- //Loop until we receive a shutdown command
- while (running) {
- //Loop If endpoint is paused
- while (paused) {
- Try {
- Thread.Sleep (+);
- } catch (interruptedexception e) {
- //Ignore
- }
- }
- //Accept the next incoming connection from the server socket
- Try {
- Socket socket = Serversocketfactory.acceptsocket (ServerSocket);
- Serversocketfactory.initsocket (socket);
- //Hand This socket off to an appropriate processor
- if (!processsocket (socket)) {
- //Close socket right away
- Try {
- Socket.close ();
- } catch (IOException e) {
- //Ignore
- }
- }
- }catch (IOException x) {
- if (running) log.error (sm.getstring ("Endpoint.accept.fail"), x);
- } catch (Throwable t) {
- Log.error (sm.getstring ("Endpoint.accept.fail"), T);
- }
- //The processor would recycle itself when it finishes
- }
- }
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.)