设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 数据 手机
当前位置: 首页 > 服务器 > 系统 > 正文

Tomcat 中的 NIO 源码分析(6)

发布时间:2020-01-07 12:09 所属栏目:52 来源:站长网
导读:在 NioEndpoint init 的时候,我们开启了一个 ServerSocketChannel,后来 start 的时候,我们开启多个 acceptor(实际上,默认是 1 个),每个 acceptor 启动以后就开始循环调用 ServerSocketChannel 的 accept 方法

在 NioEndpoint init 的时候,我们开启了一个 ServerSocketChannel,后来 start 的时候,我们开启多个 acceptor(实际上,默认是 1 个),每个 acceptor 启动以后就开始循环调用 ServerSocketChannel 的 accept 方法获取新的连接,然后调用 endpoint.setSocketOptions(socket) 处理新的连接,之后再进入循环 accept 下一个连接。

到这里,大家应该也就知道了,为什么这个叫 acceptor 了吧?接下来,我们来看看 setSocketOptions 方法到底做了什么。

NioEndpoint # setSocketOptions

@Override 

protected booleansetSocketOptions(SocketChannel socket) { 

try { 

// 设置该 SocketChannel 为非阻塞模式 

socket.configureBlocking(false); 

Socket sock = socket.socket; 

// 设置 socket 的一些属性 

socketProperties.setProperties(sock); 

 

// 还记得 startInternal 的时候,说过了 nioChannels 是缓存用的。 

// 限于篇幅,这里的 NioChannel 就不展开了,它包括了 socket 和 buffer 

NioChannel channel = nioChannels.pop; 

if (channel == ) { 

// 主要是创建读和写的两个 buffer,默认地,读和写 buffer 都是 8192 字节,8k 

SocketBufferHandler bufhandler = new SocketBufferHandler( 

socketProperties.getAppReadBufSize, 

socketProperties.getAppWriteBufSize, 

socketProperties.getDirectBuffer); 

if (isSSLEnabled) { 

channel = new SecureNioChannel(socket, bufhandler, selectorPool, this); 

} else { 

channel = new NioChannel(socket, bufhandler); 

} else { 

channel.setIOChannel(socket); 

channel.reset; 

 

// getPoller0 会选取所有 poller 中的一个 poller 

getPoller0.register(channel); 

} catch (Throwable t) { 

ExceptionUtils.handleThrowable(t); 

try { 

log.error("",t); 

} catch (Throwable tt) { 

ExceptionUtils.handleThrowable(tt); 

// Tell to close the socket 

return false; 

return true; 

(编辑:ASP站长网)

网友评论
推荐文章
    热点阅读