Tomcat NioEndpoint的Acceptor

功能简介

Tomcat服务器在启动时,会在NioEndpoint实例启动过程中创建并启动若干daemon线程来接收来自网络的连接请求(具体启动几个这样的接收线程,可以在NioEndpoint实例启动前设定的),而这些线程所执行的实际的网络连接请求接收逻辑由NioEndpoint的内部类Acceptor来提供。

源码分析

其具体实现如下 :

    // --------------------------------------------------- Acceptor Inner Class
    /**
     * 网络TCP/IP连接监听后台线程,接收到来自网络的TCP/IP连接并交给合适的处理器处理
     * 1.这里的处理器指的是SocketProcessor/ConnectionHandler(实现接口Handler)
     * 2.Accepto并非直接将连接请求交给SocketProcessor,而是经过了Poller
     * The background thread that listens for incoming TCP/IP connections and
     * hands them off to an appropriate processor.
     * AbstractEndpoint.Acceptor是一个抽象类,实现了Runnable接口
     */
    protected class Acceptor extends AbstractEndpoint.Acceptor {

        @Override
        public void run() {
			// 该方法运行在 acceptor 线程中, 比如 : http-nio-8080-Acceptor-0,
			// 用来接收来自网络的客户端请求,然后封装后注册到 poller 的事件队列,
			// 最终 poller 线程将要处理的请求交给 worker 线程。
            int errorDelay = 0;

            // Loop until we receive a shutdown command
            // running变量是所属NioEndpoint实例是否处于运行状态的标记,
            // true 表示处于运行中,false表示处于停止服务状态;NioEndpoint实例
            // 还有另外一个状态paused用来表示服务的暂停,比如 running==true&&
            // paused==true表示NioEndpoint实例处于运行中但暂停服务状态,
            // running==true&&paused==false才表示NioEndpoint实例正处于有效服务状态
			
            while (running) {

                // Loop if endpoint is paused
                while (paused && running) {
	                // 启动中但是处于暂停接收请求状态
                    state = AcceptorState.PAUSED;
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        // Ignore
                    }
                }

                if (!running) {
	                // 已经被通知处于停止状态了,退出接收请求服务循环
                    break;
                }
                // 逻辑走到这里表示处于正常接收请求状态 running==true&&paused==false
                state = AcceptorState.RUNNING;

                try {
                    //if we have reached max connections, wait
                    // 如果还没有到达最大连接数,则当前连接数量做加一操作,
                    // 如果到达最大连接数则让当前接收线程处于等待状态,直到有连接
                    // 被释放
                    countUpOrAwaitConnection();

                    SocketChannel socket = null;
                    try {
                        // Accept the next incoming connection from the server
                        // socket
                        // 从服务器套接字serverSock接收下一个进入的连接请求
                        socket = serverSock.accept();
                    } catch (IOException ioe) {
                        // We didn't get a socket
                        // 获取连接异常,前面获取连接前做了加一操作的当前连接数量
                        // 要在这里做一个减一操作,表明不占用连接数量
                        countDownConnection();
                        if (running) {
                            // Introduce delay if necessary
                            errorDelay = handleExceptionWithDelay(errorDelay);
                            // re-throw
                            throw ioe;
                        } else {
                            break;
                        }
                    }
                    // Successful accept, reset the error delay
                    errorDelay = 0;

                    // Configure the socket
                    if (running && !paused) {
                        // setSocketOptions() will hand the socket off to
                        // an appropriate processor if successful
                        // 设置套接字的一些选项,成功的话将其交给合适的processor,
                        // 当前 acceptor线程的主要目的是接收请求并委托出去,本身并不执行处理逻辑,
                        // 这里setSocketOptions()正是设置了套接字处理的一些参数,然后把处理工作
                        // 委托给了 poller 线程,委托完成后,该acceptor线程继续执行自己的请求
                        // 监听接收和委托任务。
                        if (!setSocketOptions(socket)) {
	                        // 如果没有设置成功,这直接关闭套接字通道
                            closeSocket(socket);
                        }
                    } else {
	                    // 如果系统已经被标记为不是正常运行状态,则直接关闭套接字通道
                        closeSocket(socket);
                    }
                } catch (Throwable t) {
                    ExceptionUtils.handleThrowable(t);
                    log.error(sm.getString("endpoint.accept.fail"), t);
                }
            }
            state = AcceptorState.ENDED;
        }
		// 关闭套接字通道
        private void closeSocket(SocketChannel socket) {
	        // 连接数量-1
            countDownConnection();
            try {
	            // 关闭套接字
                socket.socket().close();
            } catch (IOException ioe)  {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("endpoint.err.close"), ioe);
                }
            }
            try {
	            // 关闭套接字通道
                socket.close();
            } catch (IOException ioe) {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("endpoint.err.close"), ioe);
                }
            }
        }
    }        

上面Acceptor接收到连接请求后将待处理的套接字通过调用当前NioEndpoint实例的setSocketOptions()方法委托了出去。下面是其实现逻辑 :

	/**
	* 处理指定的连接套接字
	**/
    protected boolean setSocketOptions(SocketChannel socket) {
        // Process the connection
        try {
            //disable blocking, APR style, we are gonna be polling it
            socket.configureBlocking(false);
            Socket sock = socket.socket();
            socketProperties.setProperties(sock);

			// 将一个Java标准SocketChannel套接字请求通道封装成一个Tomcat的NioChannel,
			// 这样对于SSL的情况和非SSL的情况就可以采用同样的处理逻辑。
            NioChannel channel = nioChannels.pop();
            if (channel == null) {
                SocketBufferHandler bufhandler = new SocketBufferHandler(
                        socketProperties.getAppReadBufSize(),
                        socketProperties.getAppWriteBufSize(),
                        socketProperties.getDirectBuffer());
                if (isSSLEnabled()) {
                    channel = new SecureNioChannel(socket, bufhandler, selectorPool, this);
                } else {
                    channel = new NioChannel(socket, bufhandler);
                }
            } else {
                channel.setIOChannel(socket);
                channel.reset();
            }
            // 向Poller注册新接收到的请求套接字,委托其完成相应处理,委托完成后当前线程
            // 继续自己被设定的监听接收委托任务;
            getPoller0().register(channel);
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            try {
                log.error("",t);
            } catch (Throwable tt) {
                ExceptionUtils.handleThrowable(tt);
            }
            // Tell to close the socket
            return false;
        }
        return true;
    }

相关资料

Tomcat的NioEndpoint的Poller和PollerEvent
Tomcat NIO 基本架构
缺省配置Springboot Web应用中tomcat的启动过程

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值