Tomcat学习(三):请求处理

目录请求预处理获取请求处理器请求映射请求处理Mapper.map()Tomcat在启动时,就已经将ServerSocket(或ServerSocketChannel等)初始化完毕,并启动了Acceptor线程等待请求到达。请求预处理在Acceptor线程的run方法中,有如下代码片段:U socket = null;try { // Accept t...
摘要由CSDN通过智能技术生成

目录

请求预处理

获取请求处理器

请求映射

请求处理

Mapper.map()


Tomcat在启动时,就已经将ServerSocket(或ServerSocketChannel等)初始化完毕,并启动了Acceptor线程等待请求到达。

请求预处理

在Acceptor线程的run方法中,有如下代码片段:

U socket = null;
try {
    // Accept the next incoming connection from the server
    // socket
    socket = endpoint.serverSocketAccept();
} catch (Exception ioe) {
   // 略
}
// Successful accept, reset the error delay
errorDelay = 0;

// Configure the socket
if (endpoint.isRunning() && !endpoint.isPaused()) {
    // setSocketOptions() will hand the socket off to
    // an appropriate processor if successful
    if (!endpoint.setSocketOptions(socket)) {
        endpoint.closeSocket(socket);
    }
} else {
    endpoint.destroySocket(socket);
}

关键语句就是 socket = endpoint.serverSocketAccept() 和 endpoint.setSocketOptions(socket)。

前者在NioEndpoint中,其实就是返回 serverSock.accept(),即获取一个请求。

后者则是对请求对象进行封装,在NioEndpoint中的实现如下:

    protected boolean setSocketOptions(SocketChannel socket) {
        NioSocketWrapper socketWrapper = null;
        try {
            // Allocate channel and wrapper
            NioChannel channel = null;
            if (nioChannels != null) {
                channel = nioChannels.pop();
            }
            if (channel == null) {
                SocketBufferHandler bufhandler = new SocketBufferHandler(
                        socketProperties.getAppReadBufSize(),
                        socketProperties.getAppWriteBufSize(),
                        socketProperties.getDirectBuffer());
                if (isSSLEnabled()) {
                    channel = new SecureNioChannel(bufhandler, selectorPool, this);
                } else {
                    channel = new NioChannel(bufhandler);
                }
            }
            NioSocketWrapper newWrapper = new NioSocketWrapper(channel, this);
            channel.reset(socket, newWrapper);
            connections.put(socket, newWrapper);
            socketWrapper = newWrapper;

            // Set socket properties
            // Disable blocking, polling will be used
            socket.configureBlocking(false);
            socketProperties.setProperties(socket.socket());

            socketWrapper.setReadTimeout(getConnectionTimeout());
            socketWrapper.setWriteTimeout(getConnectionTimeout());
            socketWrapper.setKeepAliveLeft(NioEndpoint.this.getMaxKeepAliveRequests());
            socketWrapper.setSecure(isSSLEnabled());
            poller.register(channel, socketWrapper);
            return true;
        } catch (Throwable t) {
            // 略
        }
        // Tell to close the socket if needed
        return false;
    }

可见是将请求封装成一个SocketWrapper。值得注意的是,在Tomcat的启动阶段,ServerSocketChannel被设置为同步(configureBlocking(true)),在这里又设置为false了,注释也做了解释,是为了应用Poller来多线程处理请求。

封装后,将SocketWrapper发送给Poller处理。register方法将N

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值