分类: TOMCAT2009-05-17 22:25 4366人阅读 评论(3) 收藏 举报

Tomcat的模块结构设计的相当好,而且其Web 容器的性能相当出色。JBoss直接就使用了Tomcat的web容器,WebLogic的早期版本也是使用了Tomcat的代码。
Web容器的工作过程在下面的第二个参考文档中的文档已经说得相当清楚,我也就不再重复说了。如果不清楚调用过程,需要先看这个文档。这里分析一下Connector的处理过程。
1. 一个简单的Web Server示例
这个例子也是从网上找得,不知道原作者,也就不在参考资料中引用了。
这个启动服务的主程序。
public class HttpServer {
 public static void main(String args[]) {
  int port;
  ServerSocket server_socket; // 读取服务器端口号
  try {
   port = Integer.parseInt(args[0]);
  } catch (Exception e) {
   port = 8080;
  }
  try {
   // 监听服务器端口,等待连接请求
   server_socket = new ServerSocket(port);
   System.out.println(”httpServer running on port “
     + server_socket.getLocalPort());
   // 显示启动信息
   while (true) {
    Socket socket = server_socket.accept();
    System.out.println(”New connection accepted “
      + socket.getInetAddress() + “:” + socket.getPort());
    // 创建分线程
    try {
     HttpRequestHandler request = new HttpRequestHandler(socket);
     Thread thread = new Thread(request);
     // 启动线程
     thread.start();
    } catch (Exception e) {
     System.out.println(e);
    }
   }
  } catch (IOException e) {
   System.out.println(e);
  }
 }
}

下面是创建输出的线程
public class HttpRequestHandler implements Runnable {
 final static String CRLF = “/r/n”;
 Socket socket;
 InputStream input;
 OutputStream output;
 BufferedReader br;
 // 构造方法
 public HttpRequestHandler(Socket socket) throws Exception {
  this.socket = socket;
  this.input = socket.getInputStream();
  this.output = socket.getOutputStream();
  this.br = new BufferedReader(new InputStreamReader(socket
    .getInputStream()));
 }

// 实现Runnable 接口的run()方法
 public void run() {
  try {
   processRequest();
  } catch (Exception e) {
   System.out.println(e);
  }
 }

private void processRequest() throws Exception {
  while (true) {
   // 读取并显示Web 浏览器提交的请求信息
   String headerLine = br.readLine();
   System.out.println(”The client request is ” + headerLine);
   if (headerLine.equals(CRLF) || headerLine.equals(”"))
    break;
   StringTokenizer s = new StringTokenizer(headerLine);
   String temp = s.nextToken();
   if (temp.equals(”GET”)) {
    String fileName = s.nextToken();
    fileName = “.” + fileName;
    // 打开所请求的文件
    FileInputStream fis = null;
    boolean fileExists = true;
    try {
     fis = new FileInputStream(”D:/workspace/tomcat/bin/”+fileName);
    } catch (FileNotFoundException e) {
     fileExists = false;
    }
    // 完成回应消息
    String serverLine = “Server: a simple java httpServer”;
    String statusLine = null;
    String contentTypeLine = null;
    String entityBody = null;
    String contentLengthLine = “error”;
    if (fileExists) {
     statusLine = “HTTP/1.0 200 OK” + CRLF;
     contentTypeLine = “Content-type: ” + contentType(fileName) + CRLF;
     contentLengthLine = “Content-Length: “
       + (new Integer(fis.available())).toString() + CRLF;
    } else {
     statusLine = “HTTP/1.0 404 Not Found” + CRLF;
     contentTypeLine = “text/html”;
     entityBody = “<HTML>”
       + “<HEAD><TITLE>404 Not Found</TITLE></HEAD>” + “<BODY>404 Not Found” + “<br>usage:http://yourHostName:port/” + “fileName.html</BODY></HTML>”;
    }
    // 发送到服务器信息
    output.write(statusLine.getBytes());
    output.write(serverLine.getBytes());
    output.write(contentTypeLine.getBytes());
    output.write(contentLengthLine.getBytes());
    output.write(CRLF.getBytes());
    // 发送信息内容
    if (fileExists) {
     sendBytes(fis, output);
     fis.close();
    } else {
     output.write(entityBody.getBytes());
    }
   }
  }
  // 关闭套接字和流
  try {
   output.close();
   br.close();
   socket.close();
  } catch (Exception e) {
  }
 }

private static void sendBytes(FileInputStream fis, OutputStream os)
   throws Exception {
  // 创建一个 1K buffer
  byte[] buffer = new byte[1024];
  int bytes = 0;
  // 将文件输出到套接字输出流中
  while ((bytes = fis.read(buffer)) != -1) {
   os.write(buffer, 0, bytes);
  }
 }

private static String contentType(String fileName) {
  if (fileName.endsWith(”.htm”) || fileName.endsWith(”.html”)) {
   return “text/html”;
  }
  return “fileName”;
 }

这个简单的例子说明的web服务的基本实现。Tomcat在此之上模块化出线程池,网络连接和WebHttp协议3个包。线程池可独立使用,网络连接使用池化,WebHttp直接从网络连接池中获取即可。
2. 线程池的实现
这个功能的实现在包 org.apache.tomcat.util.thread 中。
ThreadPool是线程池,是这个功能实现的核心。它使用了所有的其他类进行工作。在类图中,所有的其他类都是被此类的使用关系。
我们来看此类是如何工作得。
启动连接池的方法:
 public synchronized void start() {
  stopThePool = false;
  currentThreadCount = 0;
  currentThreadsBusy = 0;

adjustLimits();
  pool = new ControlRunnable[maxThreads];
  openThreads(minSpareThreads);
  if (maxSpareThreads < maxThreads) {
   monitor = new MonitorRunnable(this);
  }
 } 
方法中,根据配置情况,初始化所有线程进入备用状态。
首先定义maxThreads数目的数组,但是仅仅初始化其中minSpareThreads个。MonitorRunnable用于检查,是否空闲数目超过 maxSpareThreads个。
currentThreadCount 是当前初始化可以使用的线程数目,而currentThreadsBusy 是当前正在使用的线程数目。
使用连接池的方法:
 public void runIt(ThreadPoolRunnable r) {
  if (null == r) {
   throw new NullPointerException();
  }
  ControlRunnable c = findControlRunnable();
  c.runIt(r);
 } 
该方法中,先寻找可用的线程,找到后在其中运行即可。
找可用线程的方法也很简单,就是将线程数组中第 currentThreadCount - currentThreadsBusy - 1 个元素取出返回,然后将此元素设成null。
线程运行完毕后,设置currentThreadsBusy– ,然后将 currentThreadCount - currentThreadsBusy - 1 的线程放回就可以了。
线程不够用就等待,等待失败就抛出异常。
说明一下上面未提到的类的功能:
ThreadPoolRunnable 这是一个接口,规定了一个线程运行时需要运行的一些动作。这里需要写一些业务逻辑的代码了。
ThreadWithAttributes 这个类从上面的代码中没有看到,这个类标识当前运行线程的一些特征,比如记录当前运行线程的一些状态。
ThreadPoolListener 用于监控ThreadPool中新增线程的情况。
ControlRunnable 这个类是ThreadPool的内部类,用于运行ThreadPoolRunnable 。当ThreadPoolRunnable 运行完毕后,通知ThreadPool回收线程。它时刻处于备用状态。此对象实例化后,就一直在死循环检查是否有它需要运行的东西。
3. 网络连接功能的实现
这个功能的实现在包 org.apache.tomcat.util.net 中。
网络连接功能构建于线程池之上,实现了一个连接服务模型。服务器打开端口,池化进入连接,为进入的连接创建工作线程。
Tomcat的网络连接两个主要的应用是1. 自己提供的web应用。2. 给Apache提供的web应用。这两个过程的解析过程都是一样的。仅仅在于网络连接协议有差别而已。两个应用都使用此包的功能实现。

PoolTcpEndpoint是核心,它使用了ThreadPool。TcpWorkerThread通过调用接口TcpConnectionHandler来完成一次连接需要完成的工作。TcpConnection标识了一个连接对象。
PoolTcpEndpoint的初始化方法代码很简单,在构建器中创建或引用ThreadPool,在初始化时创建ServerSocket,用于侦听客户端连接。
下面是初始化方法
 public void initEndpoint() throws IOException, InstantiationException {
  try {
   if (factory == null)
    factory = ServerSocketFactory.getDefault();
   if (serverSocket == null) {
    try {
     if (inet == null) {
      serverSocket = factory.createSocket(port, backlog);
     } else {
      serverSocket = factory
        .createSocket(port, backlog, inet);
     }
    } catch (BindException be) {
     throw new BindException(be.getMessage() + “:” + port);
    }
   }
   if (serverTimeout >= 0)
    serverSocket.setSoTimeout(serverTimeout);
  } catch (IOException ex) {
   // log(”couldn’t start endpoint”, ex, Logger.DEBUG);
   throw ex;
  } catch (InstantiationException ex1) {
   // log(”couldn’t start endpoint”, ex1, Logger.DEBUG);
   throw ex1;
  }
  initialized = true;
 }
 
启动的方法同样简单,仅仅将TcpWorkerThread作为线程池的工作线程,启动连接池,就大功告成了。
 public void startEndpoint() throws IOException, InstantiationException {
  if (!initialized) {
   initEndpoint();
  }
  if (isPool) {
   tp.start();
  }
  running = true;
  paused = false;
  if (isPool) {
   listener = new TcpWorkerThread(this);
   tp.runIt(listener);
  } else {
   log.error(”XXX Error - need pool !”);
  }
 }
 
侦听的细节包装在TcpWorkerThread类中。运行时,它在ServerSocket端口侦听。当发现有连接进入后,立刻开启一个新线程继续侦听,本线程开始处理连接。下面是代码:
 public void runIt(Object perThrData[]) {
  // Create per-thread cache
  if (endpoint.isRunning()) {
   // Loop if endpoint is paused
   while (endpoint.isPaused()) {
    try {
     Thread.sleep(1000);
    } catch (InterruptedException e) {
     // Ignore
    }
   }
   // Accept a new connection
   Socket s = null;
   try {
    s = endpoint.acceptSocket();
   } finally {
    // Continue accepting on another thread…
    if (endpoint.isRunning()) {
     endpoint.tp.runIt(this);
    }
   }
   // Process the connection
   if (null != s) {
    TcpConnection con = null;
    int step = 1;
    try {
     // 1: Set socket options: timeout, linger, etc
     endpoint.setSocketOptions(s);
     // 2: SSL handshake
     step = 2;
     if (endpoint.getServerSocketFactory() != null) {
 endpoint.getServerSocketFactory().handshake(s);
     }
     // 3: Process the connection
     step = 3;
     con = (TcpConnection) perThrData[0];
     con.setEndpoint(endpoint);
     con.setSocket(s);
 endpoint.getConnectionHandler().processConnection(con,
       (Object[]) perThrData[1]);
    } catch (SocketException se) {
…… 
4. 协议 web http的实现
这个功能的实现在包 org.apache.coyote.http11 中。
对于Http协议的实现核心类是Http11Protocol。具体功能的实现类有MXPoolListener(实现ThreadPoolListener),Http11ConnectionHander(实现TcpConnectionHandler)。
Http11Protocol的初始化方法比较简单,就是设置一下让网络连接开始运行。
Http11ConnectionHander则初始化类Http11Processor,由它解析请求的字符串,交给生成此Connection的Connector的Container,也就是Engine完成。Engine通过递归,解析应返回用户的数据。这个过程在参考文档中有介绍了。

tomcat 工作原理的更多相关文章

  1. 【Tomcat】Tomcat工作原理及简单模拟实现

    Tomcat应该都不陌生,我们经常会把写好的代码打包放在Tomcat里并启动,然后在浏览器里就能愉快的调用我们写的代码来实现相应的功能了,那么Tomcat是如何工作的? 一.Tomcat工作原理 我们 ...

  2. 理解Tomcat工作原理

    WEB服务器 只要Web上的Server都叫Web Server,但是大家分工不同,解决的问题也不同,所以根据Web Server提供的功能,每个Web Server的名字也会不一样. 按功能分类,W ...

  3. Tomcat 工作原理 1 (转)

    Tomcat 系统架构与设计模式,第 1 部分: 工作原理 这个分为两个部分的系列文章将研究 Apache Tomcat 的系统架构以及其运用的很多经典设计模式.本文是第 1 部分,将主要从 Tomc ...

  4. Tomcat工作原理(转)

    Tomcat简介 作者:杨晓(http://blog.sina.com.cn/u/1237288325) 一.Tomcat背景 自从JSP发布之后,推出了各式各样的JSP引擎.Apache Group ...

  5. 【Tomcat】Tomcat工作原理

    Tomcat 总体结构 Tomcat 的结构很复杂,但是 Tomcat 也非常的模块化,找到了 Tomcat 最核心的模块,您就抓住了 Tomcat 的“七寸”.下面是 Tomcat 的总体结构图: ...

  6. Tomcat工作原理解析!

    Tomcat简介   作者:杨晓(http://blog.sina.com.cn/u/1237288325) 一.Tomcat背景 自从JSP发布之后,推出了各式各样的JSP引擎.Apache Gro ...

  7. tomcat 工作原理简析

    https://github.com/HappyTomas/another-tutorial-about-java-web/blob/master/00-08.md 在00-02.理解HTTP中给出了 ...

  8. [转]Tomcat工作原理详解

    一.Tomcat背景 自从JSP发布之后,推出了各式各样的JSP引擎.Apache Group在完成GNUJSP1.0的开发以后,开始考虑在SUN的JSWDK基础上开发一个可以直接提供Web服务的JS ...

  9. Tomcat工作原理

    http://www.cnblogs.com/shootercheng/p/5838645.html

随机推荐

  1. 【Unity Shaders】使用Unity Render Textures实现画面特效——建立画面特效脚本系统

    本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...

  2. gradle编译出错:Execution failed for task ':app:compileTestDebugJava'.

    今天更新了android studio,从0.5.3升级到0.6.1版本,结果在IDE中编译时没有问题,但是在命令行时编译就会出现以下错误: :app:compileTestDebugJava FAI ...

  3. [GitHub]第一讲:浏览器中使用GitHub

    文章转载自http://blog.csdn.net/loadsong/article/details/51591407 看到一篇关于GitHub的文章,感觉不错,因此转载来以备推敲学习. 不会用 Gi ...

  4. UNIX环境高级编程——信号之kill、raise、killpg、alarm、pause、abort、sleep、usleep、nanosleep和setitimer函数

    一.kill, raise, killpg 函数 int kill(pid_t pid, int sig); int raise(int sig); int killpg(int pgrp, int ...

  5. 【Android 应用开发】 自定义组件 宽高适配方法, 手势监听器操作组件, 回调接口维护策略, 绘制方法分析 -- 基于 WheelView 组件分析自定义组件

    博客地址 : http://blog.csdn.net/shulianghan/article/details/41520569 代码下载 : -- GitHub : https://github.c ...

  6. 【leetcode74】Sum of Two Integers(不用+,-求两数之和)

    题目描述: 不用+,-求两个数的和 原文描述: Calculate the sum of two integers a and b, but you are not allowed to use th ...

  7. 2013暑假总结-SB学习

    经过暑假的学习,使英语原本基础不好的我找到了英语学习的感觉.方向.信心,暑假的这种团队学习英语的感觉才刚刚开始,即将开学了,我们并将保持着这学习的劲头坚持努力的做下去. 暑假35天英语的全职学习,对于 ...

  8. Linux Shell脚本攻略学习总结:二

    比较与测试 程序中的流程控制是由比较和测试语句来处理的. 我们可以用if,if else 以及逻辑运算符来执行测试,而用一些比较运算符来比较数据项.另外,有一个test 命令也可以用来进行测试.让我们 ...

  9. 小强的HTML5移动开发之路(1)——HTML介绍

    来自:http://blog.csdn.net/dawanganban/article/details/17591373 HTML是HyperText Markup Language(超文本标记语言) ...

  10. AngularJS进阶(三十四)Angular数据更新不及时问题探讨

    Angular数据更新不及时问题探讨 前言 在修复控制角标正确变化过程中,发觉前端代码组织层次出现了严重问题.传递和共享数据时自己使用的是rootScope,为此造成了全局变量空间的污染.根据< ...