手机版
你好,游客 登录 注册
背景:
阅读新闻

Tomcat 源代码分析之Socket通讯

[日期:2012-02-11] 来源:Linux社区  作者:redhat [字体: ]

此系列文章皆为Tomcat 7.0代码代码分析。

1.    Socket通讯:

Tomcat对于 Socket的处理方式主要分为以下几种:

1. BIO方式:采用Java阻塞Socket通讯的方式处理连接。

2. NIO方式:之前采用BIO(阻塞方式),现在由于在Java1.4之后引入NIO,提供了NIO的实现。

3.APR方式:为了和本地机器更好的集成,有更高的性能,例如一些高级的系统IO功能(sendfile, epoll and OpenSSL),本地操作的处理(shared memory, NT pipes and Unix sockets以及OS Level的功能(random number generation, system status, etc),Tomcat使用JNI调用处理Socket链接。

4.AJP和ARP结合的方式。

5.AJP方式:通过AJP协议进行通讯 : AJP主要用于Apache的HTTP服务器和Servlet Web容器之间通讯,它是Packet_Oriented的,换句话说,它发送给浏览器(其他Web Server)的数据是Packet(s),得到Servlet 容器的响应也是Packet(s),这里有特殊情况,如果Servlet 容器发送的数据是二进制的,则直接发送给浏览器。此外,AJP还可以重用和Servlet容器之间的Socket连接(Socket Connection),降低创建开销。 具体请看:http://httpd.apache.org/docs/2.2/mod/mod_proxy_ajp.html

2.    模型介绍

Connector由ProtocolHandler和一个连接端口组成,ProtocolHandler使用以上介绍的各种方式处理Socket。

根据配置选取不同的ProtocolHandler实现类的代码如下:

  1. /** 
  2.      * Set the Coyote protocol which will be used by the connector. 
  3.      * 
  4.      * @param protocol The Coyote protocol name 
  5.      */  
  6.     public void setProtocol(String protocol) {  
  7.   
  8.         if (AprLifecycleListener.isAprAvailable()) {  
  9.             if ("HTTP/1.1".equals(protocol)) {  
  10.                 setProtocolHandlerClassName  
  11.                     ("org.apache.coyote.http11.Http11AprProtocol");  
  12.             } else if ("AJP/1.3".equals(protocol)) {  
  13.                 setProtocolHandlerClassName  
  14.                     ("org.apache.coyote.ajp.AjpAprProtocol");  
  15.             } else if (protocol != null) {  
  16.                 setProtocolHandlerClassName(protocol);  
  17.             } else {  
  18.                 setProtocolHandlerClassName  
  19.                     ("org.apache.coyote.http11.Http11AprProtocol");  
  20.             }  
  21.         } else {  
  22.             if ("HTTP/1.1".equals(protocol)) {  
  23.                 setProtocolHandlerClassName  
  24.                     ("org.apache.coyote.http11.Http11Protocol");  
  25.             } else if ("AJP/1.3".equals(protocol)) {  
  26.                 setProtocolHandlerClassName  
  27.                     ("org.apache.coyote.ajp.AjpProtocol");  
  28.             } else if (protocol != null) {  
  29.                 setProtocolHandlerClassName(protocol);  
  30.             }  
  31.         }  
  32.   
  33.     }  

其相应的配置例子如下:

  1. <Connector port="8080" protocol="HTTP/1.1"  
  2.                connectionTimeout="20000"  
  3.                redirectPort="8443" />  

Connector调用ProtocolHandler对象处理Socket,主要代码在该Connector类的startInternal()里,如下

  1. /** 
  2.      * Begin processing requests via this Connector. 
  3.      * 
  4.      * @exception LifecycleException if a fatal startup error occurs 
  5.      */  
  6.     @Override  
  7.     protected void startInternal() throws LifecycleException {  
  8.   
  9.         setState(LifecycleState.STARTING);  
  10.   
  11.         try {  
  12.             protocolHandler.start();  
  13.         } catch (Exception e) {  
  14.             String errPrefix = "";  
  15.             if(this.service != null) {  
  16.                 errPrefix += "service.getName(): \"" + this.service.getName() + "\"; ";  
  17.             }  
  18.   
  19.             throw new LifecycleException  
  20.                 (errPrefix + " " + sm.getString  
  21.                  ("coyoteConnector.protocolHandlerStartFailed"), e);  
  22.         }  
  23.   
  24.         mapperListener.start();  
  25. }  
linux
相关资讯       Tomcat 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款