zoukankan      html  css  js  c++  java
  • tomcat源码阅读_代码篇10

    JIoEndpoint类处理TCP连接,实现一个简单的服务器模型:一个线程监听套接字并为每个传入的连接创建一个新的工作线程。

    该类的init方法在上一篇日志中已经介绍,接下来是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) {
                    workers = new WorkerStack(maxThreads);
                }

                // Start acceptor threads,启动acceptorThreadCount个线程用于处理请求。
                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类为该类的内部类:

    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);
                        serverSocketFactory.initSocket(socket);
                        // Hand this socket off to an appropriate processor
                        if (!processSocket(socket)) {
                            // 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

                }

            }

        }

    用于监听用户连接,处理socket使用的是processSocket(socket))方法。该方法如下:

        protected boolean processSocket(Socket socket) {
            try {
                if (executor == null) {
                    getWorkerThread().assign(socket);//获得一个Worker并将socket分配给它,由它来处理。Worker类也是本类的内部类,使用堆栈结构来进行存储。
                } else {
                    executor.execute(new SocketProcessor(socket));
                }
            } 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;
        }

    在worker中的run方法中,使用handler.process(socket)来对socket进行处理,而handler就是Http11Processor类。

    该类用来处理HTTP请求。

  • 相关阅读:
    利用相关的Aware接口
    java 值传递和引用传递。
    权限控制框架Spring Security 和Shiro 的总结
    优秀代码养成
    Servlet 基础知识
    leetcode 501. Find Mode in Binary Search Tree
    leetcode 530. Minimum Absolute Difference in BST
    leetcode 543. Diameter of Binary Tree
    leetcode 551. Student Attendance Record I
    leetcode 563. Binary Tree Tilt
  • 原文地址:https://www.cnblogs.com/macula7/p/1960475.html
Copyright © 2011-2022 走看看