zoukankan      html  css  js  c++  java
  • tomcat源码分析 Acceptor run 过程

    org.apache.tomcat.util.net.JIoEndpoint.Acceptor

    1、调用countUpOrAwaitConnection,阀门数加1。

         每个Acceptor有最大处理上限,默认为10000,如为-1,则无上限。

         

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

    2、默认serverSocket监听8080端口,当监听到请求时,为socket设置默认的options。

    protected boolean setSocketOptions(Socket socket) {
            try {
                // 1: Set socket options: timeout, linger, etc
                socketProperties.setProperties(socket);
            } catch (SocketException s) {
                //error here is common if the client has reset the connection
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("endpoint.err.unexpected"), s);
                }
                // Close the socket
                return false;
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                log.error(sm.getString("endpoint.err.unexpected"), t);
                // Close the socket
                return false;
            }
            return true;
        }

    3、为其生成一个包装器类,SocketWrapper,除socket属性之外,还包括其它一些属性

        如:lastAccess, timeout,async

    4、为包装器类生成一个SocketProcessor类,这个类实现了Runnable接口,并将此实例添加到线程队列中,交由线程池处理。

    protected boolean processSocket(Socket socket) {
            // Process the request from this socket
            try {
                SocketWrapper<Socket> wrapper = new SocketWrapper<Socket>(socket);
                wrapper.setKeepAliveLeft(getMaxKeepAliveRequests());
                // During shutdown, executor may be null - avoid NPE
                if (!running) {
                    return false;
                }
                getExecutor().execute(new SocketProcessor(wrapper));
            } catch (RejectedExecutionException x) {
                log.warn("Socket processing request was rejected for:"+socket,x);
                return false;
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(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;
        }

    5、阀门数减1.

    protected long countDownConnection() {
            if (maxConnections==-1) return -1;
            LimitLatch latch = connectionLimitLatch;
            if (latch!=null) {
                long result = latch.countDown();
                if (result<0) {
                    getLog().warn("Incorrect connection count, multiple socket.close called on the same socket." );
                }
                return result;
            } else return -1;
        }
  • 相关阅读:
    技术总监7年经验——论程序员的职业发展路线
    2.MySQL入门基本操作初体验
    1.MySQL的安装(linux Ubuntu环境下)
    Boot Petalinux Project Using a remote system
    字符设备驱动、平台设备驱动、设备驱动模型、sysfs的比较和关联
    linux采用模块方法,添加一个新的设备
    在远程服务器上完成本地设备的程序烧写和调试(基于vivado ,SDK软件)
    Linux Master/Baremetal Remote 配置下的裸机调试
    利用Xlinix SDK 建立Linux程序以及对该程序进行调试
    Vivado Launching SDK "Importing Hardware Specification" error的解决方法
  • 原文地址:https://www.cnblogs.com/knockon/p/3333998.html
Copyright © 2011-2022 走看看