博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot的tomcat启动过程
阅读量:6332 次
发布时间:2019-06-22

本文共 5671 字,大约阅读时间需要 18 分钟。

时序图1

图片描述

时序图2

图片描述

NioEndpoint(重点类)

/**     * Start the NIO endpoint, creating acceptor, poller threads.     */    @Override    public void startInternal() throws Exception {        if (!running) {            running = true;            paused = false;            processorCache = new SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,                    socketProperties.getProcessorCache());            eventCache = new SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,                            socketProperties.getEventCache());            nioChannels = new SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,                    socketProperties.getBufferPool());            // Create worker collection            if ( getExecutor() == null ) {                createExecutor();            }            initializeConnectionLatch();            // Start poller threads            pollers = new Poller[getPollerThreadCount()];            for (int i=0; i

startAcceptorThreads(Thread[http-nio-8080-Acceptor-0,5,main])

protected final void startAcceptorThreads() {        int count = getAcceptorThreadCount();        acceptors = new Acceptor[count];        for (int i = 0; i < count; i++) {            acceptors[i] = createAcceptor();            String threadName = getName() + "-Acceptor-" + i;            acceptors[i].setThreadName(threadName);            Thread t = new Thread(acceptors[i], threadName);            t.setPriority(getAcceptorThreadPriority());            t.setDaemon(getDaemon());            t.start();        }    }

NioEndpoint$Acceptor

// --------------------------------------------------- Acceptor Inner Class    /**     * The background thread that listens for incoming TCP/IP connections and     * hands them off to an appropriate processor.     */    protected class Acceptor extends AbstractEndpoint.Acceptor {        @Override        public void run() {            int errorDelay = 0;            // Loop until we receive a shutdown command            while (running) {                // Loop if endpoint is paused                while (paused && running) {                    state = AcceptorState.PAUSED;                    try {                        Thread.sleep(50);                    } catch (InterruptedException e) {                        // Ignore                    }                }                if (!running) {                    break;                }                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                        socket = serverSock.accept();                    } catch (IOException ioe) {                        //we didn't get a socket                        countDownConnection();                        // Introduce delay if necessary                        errorDelay = handleExceptionWithDelay(errorDelay);                        // re-throw                        throw ioe;                    }                    // Successful accept, reset the error delay                    errorDelay = 0;                    // setSocketOptions() will add channel to the poller                    // if successful                    if (running && !paused) {                        if (!setSocketOptions(socket)) {                            countDownConnection();                            closeSocket(socket);                        }                    } else {                        countDownConnection();                        closeSocket(socket);                    }                } catch (SocketTimeoutException sx) {                    // Ignore: Normal condition                } catch (IOException x) {                    if (running) {                        log.error(sm.getString("endpoint.accept.fail"), x);                    }                } catch (Throwable t) {                    ExceptionUtils.handleThrowable(t);                    log.error(sm.getString("endpoint.accept.fail"), t);                }            }            state = AcceptorState.ENDED;        }    }

到了这里就看到久违的java网络编程的熟悉代码了。

countUpOrAwaitConnection

protected void countUpOrAwaitConnection() throws InterruptedException {        if (maxConnections==-1) return;        LimitLatch latch = connectionLimitLatch;        if (latch!=null) latch.countUpOrAwait();    }

LimitLatch的初始值(maxConnections)

protected LimitLatch initializeConnectionLatch() {        if (maxConnections==-1) return null;        if (connectionLimitLatch==null) {            connectionLimitLatch = new LimitLatch(getMaxConnections());        }        return connectionLimitLatch;    }

超过这个值,请求进来就一直在等待,即连接堆积在accept队列,无法得到service。

socket中TCP的三次握手建立连接详解

图片描述

当客户端调用connect时,触发了连接请求,向服务器发送了SYN J包,这时connect进入阻塞状态;服务器监听到连接请求,即收到SYN J包,调用accept函数接收请求向客户端发送SYN K ,ACK J+1,这时accept进入阻塞状态;客户端收到服务器的SYN K ,ACK J+1之后,这时connect返回,并对SYN K进行确认;服务器收到ACK K+1时,accept返回,至此三次握手完毕,连接建立。

客户端的connect在三次握手的第二个次返回,而服务器端的accept在三次握手的第三次返回。

三次握手的socket层执行逻辑

三次握手建立TCP连接的流程如下:    C(Browser)                                    S(www.baidu.com) 1. CLOSED                                             LISTEN 2. SYN-SENT    →
→ SYN-RECEIVED 3. ESTABLISHED←
← SYN-RECEIVED 4. ESTABLISHED→
→ ESTABLISHED3-Way Handshake for Connection Synchronization

S调用socket的listen函数进入监听状态;C调用connect函数连接S:[SYN],S调用accept函数接受C的连接并发起与C方向上的连接:[SYN,ACK]。C发送[ACK]完成三次握手,connect函数返回;S收到C发送的[ACK]后,accept函数返回。

图片描述

doc

转载地址:http://sdboa.baihongyu.com/

你可能感兴趣的文章
kinect sdk开发入门WPFdemo笔记[2] 获取深度数据
查看>>
html块状元素、内联元素
查看>>
IPerf——网络测试工具介绍与源码解析(1)
查看>>
Python-Day7
查看>>
PreparedStatement是如何大幅度提高性能的
查看>>
Altium_Designer17-PCB-如何重新定义板子外形
查看>>
web测试总结
查看>>
SQL Server 2008导入、导出数据库
查看>>
java 和 Android Base64加密
查看>>
黑客工具软件大全100套
查看>>
在Ubuntu中打开pycharm步骤:
查看>>
[03]项目实战-PC 端固定布局(3)
查看>>
比较牛逼的大佬博客
查看>>
Elementary Methods in Number Theory Exercise 1.2.31
查看>>
为应付选修课【老子专题研读】而写的扯谈发言稿:无之境界
查看>>
『Python Web框架之Django』第几节: AJAX
查看>>
GifView
查看>>
Firefox便携版
查看>>
爬取我主良缘,获取个人图片及其信息
查看>>
[异常笔记] spring cloud 服务消费者启动-2018040501
查看>>