其实就是主要调用 protocolHandler.start()方法,继续跟踪,为了方便表述,我会把接下来的代码统一放在一起说明,代码如下:
- //1.类:AbstractProtocol implements ProtocolHandler,
- MBeanRegistration
- public void start() throws Exception {
- // 省略部分代码
- endpoint.start();
- }
- //2. 类:AbstractEndPoint
- public final void start() throws Exception {
- // 省略部分代码
- startInternal();
- }
- /**3.类:NioEndPoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
- * Start the NIO endpoint, creating acceptor, poller threads.
- */
- @Override
- public void startInternal() throws Exception {
- //省略部分代码
-
- // Start poller thread
- poller = new Poller();
- Thread pollerThread = new Thread(poller, getName() + "-ClientPoller");
- pollerThread.setPriority(threadPriority);
- pollerThread.setDaemon(true);
- pollerThread.start();
- startAcceptorThread();
- }
- }
到这里,其实整个启动代码就完成了,我们看到最后是在NioEndPoint创建了一个Poller,并且启动它,这里需要补充说明下,这里只是以NioEndPoint为示列,其实Tomcat 主要提供了三种实现,分别是AprEndPoint,NioEndPoint,Nio2EndPoint,这里表示了tomcat支持的I/O模型:
- APR:采用 Apache 可移植运行库实现,它根据不同操作系统,分别用c重写了大部分IO和系统线程操作模块,据说性能要比其他模式要好(未实测)。
- NIO:非阻塞 I/O
- NIO.2:异步 I/O
上述代码主要是开启两个线程,一个是Poller,一个是开启Acceptor,既然是线程,核心的代码肯定是run方法,我们来查看源码,代码如下:
- //4.类:Acceptor<U> implements Runnable
- public void run() {
- //省略了部分代码
- U socket = null;
- socket = endpoint.serverSocketAccept();
- // Configure the socket
- if (endpoint.isRunning() && !endpoint.isPaused()) {
- // setSocketOptions() will hand the socket off to
- // an appropriate processor if successful
- //核心逻辑
- if (!endpoint.setSocketOptions(socket)) {
- endpoint.closeSocket(socket);
- }
- } else {
- endpoint.destroySocket(socket);
- }
-
- state = AcceptorState.ENDED;
- }
- //5.类:NioEndpoint
- protected boolean setSocketOptions(SocketChannel socket) {
- // Process the connection
- //省略部分代码
- try {
- // Disable blocking, polling will be used
- socket.configureBlocking(false);
- Socket sock = socket.socket();
- socketProperties.setProperties(sock);
- NioSocketWrapper socketWrapper = new NioSocketWrapper(channel, this);
- channel.setSocketWrapper(socketWrapper);
- socketWrapper.setReadTimeout(getConnectionTimeout());
- socketWrapper.setWriteTimeout(getConnectionTimeout());
- socketWrapper.setKeepAliveLeft(NioEndpoint.this.getMaxKeepAliveRequests());
- socketWrapper.setSecure(isSSLEnabled());
- //核心逻辑
- poller.register(channel, socketWrapper);
- return true;
-
- }
(编辑:晋中站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|