Tomcat6中几种Tcp的连接方式,顺带瞄下源代码

首先,这个题目感觉很贴切。


其次,通过题目可以了解到tcp和socket相关,我暂且把他们归为一类事物,因为都属于传输层的东西。那么就开始说tomcat接受客户端连接也就是tcp连接的几种方式:JIoEndpoint,NioEndpoint,AprEndpoint


源代码中关于这几个类的说明:

JIoEndpoint:Handle incoming TCP connections.This class implement a simple server model: one listener thread accepts on a socket and creates a new worker thread for each incoming connection.(处理tcp连接,实现了一个简单的服务器模型,有一个监听程序在socket监听连接,并且为每一个连接建立一个工作线程)

详细解释一下上面的意思吧,根据源代码讲解。我们知道一般服务器建立监听都是在服务器启动后就一直监听,依此逻辑,那就找start()方法吧:


public void start() throws Exception {
        // Initialize socket if not done before
        if (!initialized) {
            init();			//初始化工作,不是本文的重点
        }
        if (!running) {
            running = true;
            paused = false;

            // Create worker collection
            if (executor == null) {		//如果线程池为null,那么建立maxThreads个工作线程堆栈.取决于是否配置
                workers = new WorkerStack(maxThreads);
            }

            // 建立监听线程(守护线程),可知并不一定是建立一个,而是acceptorThreadCount个(可配置),默认情况下也就是一个,运行态通过jconsole可以清晰看到
            for (int i = 0; i < acceptorThreadCount; i++) {		
                Thread acceptorThread = new Thread(new Acceptor(), getName() + "-Acceptor-" + i);
                acceptorThread.setPriority(threadPriority);
                acceptorThread.setDaemon(daemon);
                acceptorThread.start();
            }
        }
    }
启动Acceptor用于监听连接请求

/**
     * Server socket acceptor thread.
     */
    protected class Acceptor implements Runnable {


        /**
         * The background thread that listens for incoming TCP/IP connections and
         * hands them off to an appropriate processor.
         */
        public void run() {

            // Loop until we receive a shutdown command
            while (running) {

                // Loop if endpoint is paused
                while (paused) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // Ignore
                    }
                }

                // Accept the next incoming connection from the server socket
                try {
                    Socket socket = serverSocketFactory.acceptSocket(serverSocket);		//非ssl连接的话其实也就是调用了jdk当中的socket.accept()
                    serverSocketFactory.initSocket(socket);		//其实什么都没有做
                    // Hand this socket off to an appropriate processor
                    if (!processSocket(socket)) {<span style="white-space:pre">	</span>//用于对连接请求进行业务逻辑的处理
                        // 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 will recycle itself when it finishes

            }

        }

    }

protected boolean processSocket(Socket socket) {
        try {
            if (executor == null) {			//server.xml配置决定的(为null那么没有配置连接池)
                getWorkerThread().assign(socket);
            } else {						
                executor.execute(new SocketProcessor(socket));		//从线程池中取出一个业务工作线程,并把SocketProcessor放进去执行
            }
        } catch (Throwable t) {
            // This means we got an OOM or similar creating a thread, or that
            // the pool and its queue are full
            log.error(sm.getString("endpoint.process.fail"), t);
            return false;
        }
        return true;
    }



NioEndpoint:NIO tailored thread pool, providing the following services:Socket acceptor thread,Socket poller thread,Worker threads pool。

NIO定制的线程池,它提供了socket连接线程,socket poller线程,工作线程




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值