设为首页 收藏本站
查看: 787|回复: 0

[经验分享] Tomcat6.0连接器源码分析3

[复制链接]

尚未签到

发表于 2018-12-2 06:57:36 | 显示全部楼层 |阅读模式
  接上篇http://www.iteye.com/topic/994833,
我们看到JioEndPoint的start方法有下面一段代码:
  // Create worker collection
  if (executor == null) {
      workers = new WorkerStack(maxThreads);
  }
在上一篇中,executor一直都为null。什么时候不为空呢,这里因为Server.xml文件里的Connector元素还有一个executor属性,它指向一个Executor属性能名字。(参考:
http://tomcat.apache.org/tomcat-6.0-doc/config/http.html)。
在连接器上方有一个默认的Executor元素:

不过当前他是注释掉的,我们把他打开。
这个Executor默认的类是org.apache.catalina.core. StandardThreadExecutor,
当然你可以通过它的className属性使用自己的类,但必须像StandardThreadExecutor
一样实现Executor接口。
  StandardThreadExecutor是在Doug lea大爷的ThreadPoolExecutor类构造出来的:
public void start() throws LifecycleException {
        lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
        TaskQueue taskqueue = new TaskQueue();
        TaskThreadFactory tf = new TaskThreadFactory(namePrefix);
        lifecycle.fireLifecycleEvent(START_EVENT, null);
        executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), maxIdleTime, TimeUnit.MILLISECONDS,taskqueue, tf) {
   @Override
   protected void afterExecute(Runnable r, Throwable t) {
    AtomicInteger atomic = submittedTasksCount;
    if(atomic!=null) {
     atomic.decrementAndGet();
    }
   }
        };
        taskqueue.setParent( (ThreadPoolExecutor) executor);
        submittedTasksCount = new AtomicInteger();
        lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
}
  剩下的工作就是在Connector里添加属性executor,来引用tomcatThreadPool。

这样executor就横空出世。
  那么开始那段代码就不需要WorkerStack了。Tomcat自个搞了个WorkerStack出来,
还是给Doug lea大爷一个很大的面子哦。
这样processSocket不需大费周折,直接交给executor去执行就OK了:
protected boolean processSocket(Socket socket) {
        try {
            if (executor == null) {
                getWorkerThread().assign(socket);
            } 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;
    }
  
上一篇说讨论BIO模式的连接器,无外乎创建ServerSocket -> 绑定(Bind)端口
->接受(accept)连接->取出一个线程处理Socket 的过程。
  我们继续讨论NIO模型的连接器,首先更改连接器协议为org.apache.coyote.http11. Http11NioProtocol:
  
很简单,这样就启动了NIO模型。
与BIO模型一致,Http11NioProtocol在init方法里初始化NioEndpoint,我们讨论BIO模型时提到JioEndPoint,以后还会提到AprEndpoint。同样后续工作主要由NioEndpoint来完成。
  首先看NioEndpoint的init方法:
/**
     * Initialize the endpoint.
     */
    public void init()
        throws Exception {
  if (initialized)
            return;
  serverSock = ServerSocketChannel.open();
        serverSock.socket().setPerformancePreferences(socketProperties.getPerformanceConnectionTime(),
                                                      socketProperties.getPerformanceLatency(),
                                                      socketProperties.getPerformanceBandwidth());
        InetSocketAddress addr = (address!=null?new InetSocketAddress(address,port):new InetSocketAddress(port));
        serverSock.socket().bind(addr,backlog);
        serverSock.configureBlocking(true); //mimic APR behavior
        serverSock.socket().setSoTimeout(getSocketProperties().getSoTimeout());
        // Initialize thread count defaults for acceptor, poller
        if (acceptorThreadCount == 0) {
            // FIXME: Doesn't seem to work that well with multiple accept threads
            acceptorThreadCount = 1;
        }
        if (pollerThreadCount 0) reclaimParachute(true);
        selectorPool.open();
        initialized = true;
}
首先创建ServerSocketChannel,绑定监听端口。这里有一点不同之外,在NIO模式下,我们传统的方式像这样建立服务监听:
serverSock = ServerSocketChannel.open();
serverSock.socket().bind(new InetSocketAddress(8888));
Selector selector = Selector.open();
serverSock.configureBlocking(false);
serverSock.register(selector, SelectionKey.OP_ACCEPT);
while (selector.select() > 0) {
  //处理连接请求
}
但Tomcat采用了Blocking模式接收连接请求,在读写的时候采用No-Blocking模式。这种做法和weblogic的做法一致(我反编译看到的),像MINA等开源框架还是采用传统的方式。
我想这里有两个解释,一是接收线程的工作就是等待新的连接,没其他事可以,用No-Blocking已没有意义,另外selector.select()在一些OS上还会出现CPU 100%的空转现象,如果有其他见解,请告知我,在此感谢。
代码行设定了两个变量:acceptorThreadCount,pollerThreadCount。 前者是acceptor线程的个数,后者是读写线程的个数。这个模型是Dong Lea在《Scalable IO in Java》中的“Using Multiple Reactors”。acceptorThreadCount一般设定为1,而pollerThreadCount Tomcat给的默认值为CPU个数(Runtime.getRuntime().availableProcessors());
  初始化完毕,我们接着看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 (getUseExecutor()) {
                if ( executor == null ) {
                    TaskQueue taskqueue = new TaskQueue();
                    TaskThreadFactory tf = new TaskThreadFactory(getName() + "-exec-");
                    executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), 60, TimeUnit.SECONDS,taskqueue, tf);
                    taskqueue.setParent( (ThreadPoolExecutor) executor, this);
                }
            } else if ( executor == null ) {//avoid two thread pools being created
                workers = new WorkerStack(maxThreads);
            }
  // Start poller threads
            pollers = new Poller[getPollerThreadCount()];
            for (int i=0; i

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-642119-1-1.html 上篇帖子: spring配置下通过tomcat的jndi服务连接数据库 下篇帖子: tomcat运行一段时间后提示Error occurred during initialization of VM
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表