`
mabusyao
  • 浏览: 252581 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Tomcat 源码学习 之 Http11ConnectionHandler

Web 
阅读更多

 

Class Name org.apache.coyote.http11.Http11ConnectionHandler
Inheritance

Handler

Related Classes

Http11Protocol

RequestGroupInfo

Http11Processor

Functionality Maitainance processor list,assign cooresponding processor to input requests.

 

 

Analysis

Http11ConnectionHandler is a static class been defined in Http11Protocal.java file, it's very strange design that it actually inherited from Handler interface which was defined in  JIoEndpoint. Not sure why Tomcat design the connector this way, but there must be a reason.

 

Recalling to blog about JIoEndpoint, Http11ConnectionHandler will be used by cocurrent threads in SocketProcessor.run() method, which means, Http11ConnectionHandler need to be working file in multiple thread. That's why thread safe was considered highest priority when desin this class.

 

Fields

There are five instance fields defined in this class:

 

  • Http11Protocal proto, reference to the protocal instace, this instance was readonly in this class.
  • AtomicLong registerCount, this field count the number of processors been registered in JMX by handler. As you can see, AtomicLong type was used to ensure the atomic access to this value in the multiple threads environment.
  • RequestGroupInfo global, it is currently used only as a JMX artifact, to aggregate the data collected from each RequestProcessor thread.
  • ConcurrentHashMap<SocketWrapper<Socket>, Http11Processor> connections, each socket will be assigned one Http11Processor, we need to make sure this map is thread safe.
  • ConcurrentLinkedQueue<Http11Processor> recycledProcessors, idle processors could be used to assign to new socket. because of the default implimentation of ConcurrentLinkedQueue is unbounded, Http11ConnectionHandler inherited it and implements as bounded.
Methods

The key method of Http11ConnectionHandler is the "process" method:

 

        public SocketState process(SocketWrapper<Socket> socket, SocketStatus status) {
            Http11Processor processor = connections.remove(socket);
            try {
                if (processor == null) {
                    processor = recycledProcessors.poll();
                }
                if (processor == null) {
                    processor = createProcessor();
                }

                if (proto.isSSLEnabled() && (proto.sslImplementation != null)) {
                    processor.setSSLSupport(
                            proto.sslImplementation.getSSLSupport(
                                    socket.getSocket()));
                } else {
                    processor.setSSLSupport(null);
                }
                
                SocketState state = socket.isAsync()?processor.asyncDispatch(status):processor.process(socket);
                if (state == SocketState.LONG) {
                    connections.put(socket, processor);
                    socket.setAsync(true);
                    // longPoll may change socket state (e.g. to trigger a
                    // complete or dispatch)
                    return processor.asyncPostProcess();
                } else {
                    socket.setAsync(false);
                    recycledProcessors.offer(processor);
                }
                return state;
            } catch(java.net.SocketException e) {
                // SocketExceptions are normal
                log.debug(sm.getString(
                        "http11protocol.proto.socketexception.debug"), e);
            } catch (java.io.IOException e) {
                // IOExceptions are normal
                log.debug(sm.getString(
                        "http11protocol.proto.ioexception.debug"), e);
            }
            // Future developers: if you discover any other
            // rare-but-nonfatal exceptions, catch them here, and log as
            // above.
            catch (Throwable e) {
                ExceptionUtils.handleThrowable(e);
                // any other exception or error is odd. Here we log it
                // with "ERROR" level, so it will show up even on
                // less-than-verbose logs.
                log.error(sm.getString("http11protocol.proto.error"), e);
            }
            recycledProcessors.offer(processor);
            return SocketState.CLOSED;
        }

 

The method will first check if an existing processor been assign to current socket, use the existing processor if has, otherwise a new one will be created.

 

Processor will set it's SSL support based on the setting in ProtocalHandler, we will leave this part in future discussion.

 

After that, the socket will be processed by the processor differently according to if the socket is an asynchronized. 

 

  • If it's an asynchronized socket, it will call processor's asyncDispatch method. A LONG status will return if process succesufully, and processor's asyncPostProcess method will be called.
  • If it's not an asynchronized socket, it will call processor's process method.

The processor will be recyled once finished process, and the socket status will be set to CLOSED.

 

分享到:
评论

相关推荐

    tomcat源码分析

    Apache Tomcat是Java开发人员最常用的Web服务器之一,它不仅能够作为独立的应用程序运行Servlet和JavaServer Pages(JSP),还可以作为一个内嵌的容器来运行Web应用。Tomcat的核心设计遵循了模块化原则,这使得其...

    基于全局储存的新思路 _ Tomcat的一种通用回显方法研究1

    在分析Tomcat源码的过程中,作者发现Http11Processor类(继承自AbstractProcessor)持有Request和Response的引用,这两个引用是final类型的,一旦赋值就不会改变。因此,只要能获取到Http11Processor实例,就能得到...

    yj软件项目开发设计说明文档

    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:624) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)

    COS——R.log

    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java...

    commons-beanutils-1.7.0

    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread....

    jeecg常见问题

    2014-6-27 15:29:21 org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler process 严重: Error reading request, ignored java.lang.OutOfMemoryError: PermGen space ``` **解决方案**: 1. **增加...

    jPortMap源码

    - 源码可能还包含处理网络事件的线程类,如`ConnectionHandler`,处理连接的建立、保持和关闭。 了解jPortMap的源码有助于开发者深入理解端口映射的实现细节,同时也为自定义NAT穿透解决方案提供了基础。通过学习...

    解决struts2下载异常的jar包 struts2-sunspoter-stream-1.0.jar

    298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache....

    Openfire源码分析

    ### Openfire源码分析 #### 一、Openfire概述 **Openfire**是一款基于XMPP协议的开源实时通信服务器,XMPP(可扩展消息处理协议)又称为Jabber协议,是一种开放标准的即时通讯协议。Openfire的核心功能主要包括...

    java head space.txt

    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote....

    java TCP网络通信聊天室(源码+演示视频+报告论文6000字).zip

    2. ConnectionHandler类:处理每个客户端的连接,可能在一个新的线程中运行,以便服务器可以同时处理多个客户端连接。 3. MessageQueue或类似的数据结构:存储待广播的消息,确保消息的顺序发送。 客户端部分可能...

    cpp-oat一个纯C实现零依赖面向性能的Web服务开发框架

    4. **易用性**:`oatpp`的API设计简洁,使得学习和使用变得简单。它提供了强大的类型安全性和错误检查机制,有助于减少开发中的错误。 5. **模块化**:框架分为多个模块,如`oatpp-web-server`、`oatpp-db`等,允许...

    Netty4.0学习笔记系列之四:混合使用coder和handler

    在本篇Netty4.0学习笔记中,我们将聚焦于如何在实际应用中混合使用`coder`和`handler`,这是Netty框架中非常关键的一部分,对于构建高性能、低延迟的网络应用程序至关重要。Netty是一个用Java编写的异步事件驱动的...

    openfire源代码研究

    - **XXHandler**:实际的处理逻辑类,例如`ConnectionHandler`,用于处理客户端连接请求。 #### 三、系统配置详解 Openfire的配置采用了多层面的方案,包括文件配置、数据库配置以及硬编码配置等。 ##### 文件...

    gloox学习笔记(转载)

    这些处理器通常以虚类接口的形式存在,如LogHandler、GenericTagHandler、ConnectionHandler等。例如,`ConnectionListener`接口中的`onConnect()`和`onTLSConnect()`方法,分别用于处理连接建立成功和TLS/SSL连接...

    java动态代理实现数据库连接池

    3. **实现InvocationHandler**:创建一个实现了`InvocationHandler`接口的类`ConnectionHandler`,用于处理所有针对代理对象的调用。 - 当调用`Connection.close()`方法时,`ConnectionHandler`中的`invoke`方法将...

    android-connection-handler:Android库,用于处理与RESTful服务的通信

    ConnectionHandler是一个开放源代码Android库项目,用于处理与RESTful服务的通信。 它基于Apache HttpClient。 ConnectionHandler的优点: 支持同步和异步调用。 支持许多配置选项,例如连接超时,使用cookie等。...

    CommandHandler-开源

    这种图形化的设计方式降低了学习曲线,使得即使是不熟悉命令行的用户也能快速上手。 CommandDesigner 提供了一个直观的界面,用户可以在这里绘制命令的流程图,包括不同的参数、选项和子命令。这就像搭建积木一样...

    void服务器,基于android平台的

    在ConnectionHandler类中,我们可以编写处理客户端请求的具体逻辑,如解析HTTP请求、执行操作并关闭连接。 另外,由于Android的安全机制,服务器应用需要在Manifest.xml中声明相应的权限,如INTERNET权限,以允许...

    使用Libmicrohttpd搭建内嵌(本地)服务器的方法

    在本教程中,我们学习了如何使用Libmicrohttpd搭建内嵌服务器。Libmicrohttpd是一个功能强大且轻便的C语言库,它提供了一个简单的API,且所有的函数都是可重入的。该库支持HTTP 1.1协议,并提供了四种多线程模型。...

Global site tag (gtag.js) - Google Analytics