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请求。

  • 相关阅读:
    蛙蛙推荐:简单介绍一下托管容器持久性(CMP),顺便征集一下.NET CMP2.0的改进方案
    15分钟内快速构建数据访问层(翻译)
    【蛙蛙推荐】.NET 2.0里使用强类型数据创建多层应用
    蛙蛙推荐:迎接web2.0:写一个RSS.HTC组件
    蛙蛙推荐:web下的授权简单解决方案
    J2me访问c# Web Services
    2006年3月份技术随笔
    声讨vs.net,讨论用户控件,编码等问题
    Hadoop中mapred包和mapreduce包的区别
    hbase MapReduce程序样例入门
  • 原文地址:https://www.cnblogs.com/macula7/p/1960475.html
Copyright © 2011-2022 走看看