tomcat source threading model first experience
Certainly more than the commonly used web server tomcat, there weblogic, jetty, etc., but so far only used tomcat server. Briefly summarize harvest.
tomcat model supports four threads
- BIO
synchronous blocking IO, it is the traditional java.io, request creates a thread, the performance of each large overhead in this mode is not suitable for high concurrency scenarios, but stable point, for a small number of connections and fixed infrastructure. - NIO
After synchronization nonblocking IO (after JDK1.4), then the mode processing based on the notification thread selector multiplexer (selector) monitoring the connection state, so as to achieve non-blocking, to better support concurrent, tomcat8.0 defaults. - APR
stands for apache protable runtime, call the Apache HTTP server in the form of jni core dynamic link library, you need to install APR library. (Specifically I have not used) - AIO
asynchronous non-blocking the IO, the rear support jdk1.7, do not need the multiplexer selector, but after the request processing by a thread of execution, a callback notification continues for subsequent operations. tomcat8.0 support after.
Configuring threading model in the tomcat
found in server.xml tomcat-> conf, open the find the following configuration.
Here is the default BIO, BIO there is a wording is: Protocol = "org.apache.coyote.http11.Http11Protocol"
NIO
Protocol = "org.apache.coyote.http11.Http11NioProtocol"
AIO
Protocol = "org.apache.coyote.http11 .Http11Nio2Protocol "
the APR
Protocol =" org.apache.coyote.http11.Http11AprProtocol "
Source Analysis in BIO tomcat
: following the general process
would first here is the inlet JioEndPoint class (early because only BIO, so here is named javaIo), which has internal class Acceptor class, there is a client request,
where countUporAwaitConnection () method is the number of connection statistics tomcat, the default setting is 10000, if more than this number, it will put the request into the blocking queue.
AcceptSocket here is to call the ServerSocket's accept method. With this, the client and server to establish a connection.
The following is a specific process processSocket connected,
getExecutor perform this is to take the thread from the thread pool socket
processing is SocketProcessor In this class, the class implements the interface runnable, the method is based on RUN state handling socket
Source Analysis in tomcat NIO
main processes:
from which it can be seen, after switching to tomcat NiO, there are three threads, acceptor, poller, socketProcessor.
Here is NioEndpoint, you can not see the start and bio differences, mainly serverSock.accpet, but this place is a SocketChannel, serverSocket is ServerSocketChannel, take a look at the following initialization
after obtaining socket, it is packaged into a pipeline, and then register in the poller,
socket.getSelector here is to give the multiplexer, the selector of the pipeline register to
the register, to begin to cycle selector, a second poll a first
acquisition channel to be processed is processed, processKey is particularly processing
This is the end of the analysis, the analysis is not detailed enough, just above personal understanding.