Tomcat中BIO与NIO

一、 前言

Tomcat中接受请求的组件是connector,本文就来研究下tomcat中BIO方式和NIO方式接受请求的原理。

二、 Connector

研究过tomcat的童鞋应该都知道tomcat的容器构造:

0?wx_fmt=png

Connector是一个桥梁它把Server和Engine链接了起来,Connector的作用是接受客户端端的请求,然后把请求委托为engine容器去处理。

0?wx_fmt=png

三、 NioEndpoint

3.1 主流程启动时序

0?wx_fmt=png

下面首先分析下套接字绑定代码:

 public void bind() throws Exception {        //创建服务套接字
        serverSock = ServerSocketChannel.open();
        socketProperties.setProperties(serverSock.socket());
        InetSocketAddress addr = (getAddress()!=null?new InetSocketAddress(getAddress(),getPort()):new InetSocketAddress(getPort()));
        serverSock.socket().bind(addr,getBacklog());
        serverSock.configureBlocking(true); //打开阻塞模式
        serverSock.socket().setSoTimeout(getSocketProperties().getSoTimeout());        // 初始化接受线程个数和轮询链接套接字状态线程个数
        if (acceptorThreadCount == 0) {            // FIXME: Doesn't seem to work that well with multiple accept threads
            acceptorThreadCount = 1;
        }        if (pollerThreadCount <= 0) {            //minimum one poller thread
            pollerThreadCount = 1;
        }
        stopLatch = new CountDownLatch(pollerThreadCount);        // Initialize SSL if needed
        if (isSSLEnabled()) {
            SSLUtil sslUtil = handler.getSslImplementation().getSSLUtil(this);

            sslContext = sslUtil.createSSLContext();
            sslContext.init(wrap(sslUtil.getKeyManagers()),
                    sslUtil.getTrustManagers(), null);

            SSLSessionContext sessionContext =
                sslContext.getServerSessionContext();            if (sessionContext != null) {
                sslUtil.configureSessionContext(sessionContext);
            }            // Determine which cipher suites and protocols to enable
            enabledCiphers = sslUtil.getEnableableCiphers(sslContext);
            enabledProtocols = s
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值