`
fj_ding
  • 浏览: 37117 次
社区版块
存档分类
最新评论

Tomcat中Lifecycle详解(源码阅读)

阅读更多

在tomcat中,每一个组件生命周期都是需要统一管理的,一般是由调用该组件的组件来启动或停止当前组建,如connector组件控制processor组件启动和停止,因此每个组件主要类都会继承Lifecycle接口。tomcat中周期控制采用观察者模式来设计。其中主要类和接口如下:

 

  1. Lifecycle接口(要使用生命周期控制的类都会继承该类)
  2. LifecycleListener接口(监听器都会继承该类)
  3. LifecycleSupport类(用来对监听器进行管理)
  4. LifecycleEvent类(该类是一个辅助类,用来作为参数类型)
  5. LifecycleException类(异常类)

 

 Talk is cheap. Show me the code.


Lifecycle接口

public interface Lifecycle {

    // ----------------------------------------------------- Manifest Constants

    public static final String START_EVENT = "start";

    public static final String BEFORE_START_EVENT = "before_start";

    public static final String AFTER_START_EVENT = "after_start";

    public static final String STOP_EVENT = "stop";

    public static final String BEFORE_STOP_EVENT = "before_stop";

    public static final String AFTER_STOP_EVENT = "after_stop";

    // --------------------------------------------------------- Public Methods

    public void addLifecycleListener(LifecycleListener listener);

    public LifecycleListener[] findLifecycleListeners();

    public void removeLifecycleListener(LifecycleListener listener);

    public void start() throws LifecycleException;

    public void stop() throws LifecycleException;

}
 

LifecycleListener接口

public interface LifecycleListener {
    public void lifecycleEvent(LifecycleEvent event);
}
 

LifecycleSupport类
该类是一种中间类,主要目的就是对LifecycleListener[]数组进行管理。而且

public final class LifecycleSupport {
    // ----------------------------------------------------------- Constructors    
    public LifecycleSupport(Lifecycle lifecycle) {

        super();
        this.lifecycle = lifecycle;

    }
    // ----------------------------------------------------- Instance Variables
    private Lifecycle lifecycle = null;
    private LifecycleListener listeners[] = new LifecycleListener[0];
    // --------------------------------------------------------- Public Methods
    //添加监听器,该方法中动态数组的方法值得借鉴
    public void addLifecycleListener(LifecycleListener listener) {

      synchronized (listeners) {
          LifecycleListener results[] =
            new LifecycleListener[listeners.length + 1];
          for (int i = 0; i < listeners.length; i++)
              results[i] = listeners[i];
          results[listeners.length] = listener;
          listeners = results;
      }

    }
    public LifecycleListener[] findLifecycleListeners() {

        return listeners;

    }
    public void fireLifecycleEvent(String type, Object data) {

        LifecycleEvent event = new LifecycleEvent(lifecycle, type, data);
        LifecycleListener interested[] = null;
        synchronized (listeners) {
            interested = (LifecycleListener[]) listeners.clone();
        }
        for (int i = 0; i < interested.length; i++)
            interested[i].lifecycleEvent(event);

    }
    public void removeLifecycleListener(LifecycleListener listener) {

        synchronized (listeners) {
            int n = -1;
            for (int i = 0; i < listeners.length; i++) {
                if (listeners[i] == listener) {
                    n = i;
                    break;
                }
            }
            if (n < 0)
                return;
            LifecycleListener results[] =
              new LifecycleListener[listeners.length - 1];
            int j = 0;
            for (int i = 0; i < listeners.length; i++) {
                if (i != n)
                    results[j++] = listeners[i];
            }
            listeners = results;
        }

    }


}
  

LifecycleEvent类
该类是一个辅助类,用来 lifecycleEvent(LifecycleEvent event)方法的参数类型。

public final class LifecycleEvent extends EventObject {

    // ----------------------------------------------------------- Constructors

    public LifecycleEvent(Lifecycle lifecycle, String type) {

        this(lifecycle, type, null);

    }

    public LifecycleEvent(Lifecycle lifecycle, String type, Object data) {

        super(lifecycle);
        this.lifecycle = lifecycle;
        this.type = type;
        this.data = data;

    }

    // ----------------------------------------------------- Instance Variables

    private Object data = null;

    private Lifecycle lifecycle = null;

    private String type = null;

    // ------------------------------------------------------------- Properties

    public Object getData() {

        return (this.data);

    }

    public Lifecycle getLifecycle() {

        return (this.lifecycle);

    }

    public String getType() {

        return (this.type);

    }

}
  

LifecycleException类
该类只是一个异常类,该处代码省略。

connector中的实际调用

如tomcat4中connector组件的默认实现类HttpConnector就实现了Lifecycle接口,如下所示


public final class HttpConnector implements Connector, Lifecycle, Runnable{

//实例化一个LifecycleSupport 类,用来管理监听器的注册和取消。
protected LifecycleSupport lifecycle = new LifecycleSupport(this);

public void addLifecycleListener(LifecycleListener listener) {

        lifecycle.addLifecycleListener(listener);

    }
    public LifecycleListener[] findLifecycleListeners() {
        return lifecycle.findLifecycleListeners();
    }
    public void removeLifecycleListener(LifecycleListener listener) {

        lifecycle.removeLifecycleListener(listener);
    }
public void start() throws LifecycleException {

        // Validate and update our current state
        if (started)
            throw new LifecycleException
                (sm.getString("httpConnector.alreadyStarted"));
        threadName = "HttpConnector[" + port + "]";
        //激活监听器,该方法会调用各个监听器的lifecycleEvent()方法。
        lifecycle.fireLifecycleEvent(START_EVENT, null);
        started = true;

/*        // Start our background thread
        threadStart();

        // Create the specified minimum number of processors
        while (curProcessors < minProcessors) {
            if ((maxProcessors > 0) && (curProcessors >= maxProcessors))
                break;
            HttpProcessor processor = newProcessor();
            recycle(processor);
        }*/

    }

    public void stop() throws LifecycleException {

        // Validate and update our current state
        if (!started)
            throw new LifecycleException
                (sm.getString("httpConnector.notStarted"));
        lifecycle.fireLifecycleEvent(STOP_EVENT, null);
        started = false;

        // Gracefully shut down all processors we have created
        for (int i = created.size() - 1; i >= 0; i--) {
            HttpProcessor processor = (HttpProcessor) created.elementAt(i);
            if (processor instanceof Lifecycle) {
                try {
                    ((Lifecycle) processor).stop();
                } catch (LifecycleException e) {
                    log("HttpConnector.stop", e);
                }
            }
        }

      /*  synchronized (threadSync) {
            // Close the server socket we were using
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    ;
                }
            }
            // Stop our background thread
            threadStop();
        }
        serverSocket = null;

    }*/
}
 
分享到:
评论

相关推荐

    tomcat架构的源码分析

    #### 二、Tomcat各个组件详解 ##### 1. Server组件 - **定义**:`Server`组件是Tomcat实例的顶层元素,代表整个容器,由`org.apache.catalina.Server`接口定义。它控制着Tomcat实例的启动与停止过程。 - **功能**...

    Tomcat源码分析1

    《Tomcat源码分析1——服务启动与架构详解》 Tomcat,作为一款广泛应用的开源Java Servlet容器,其内部架构和启动流程对于深入理解和优化Web应用程序至关重要。本文将重点解析Tomcat启动时的关键步骤和核心组件,...

    tomcat8 源码

    源码中,你可以看到每个模块的职责划分和它们之间的交互。 2. **生命周期管理**:在Catalina模块中,`Container`接口和`LifeCycle`接口代表了Tomcat中的组件和它们的生命周期方法,如`start()`、`stop()`等,这对于...

    tomcat源码

    **Tomcat源码详解** Tomcat,作为Apache软件基金会的项目之一,是广泛使用的Java Servlet容器,它实现了Java EE的Web部分,特别是Servlet和JSP规范。Tomcat以其轻量级、开源和易于理解的特点,成为了许多开发者学习...

    tomcat源码分析

    ### Tomcat源码分析 #### 一、概述 Apache Tomcat是Java开发人员最常用的Web服务器之一,它不仅能够作为独立的应用程序运行Servlet和JavaServer Pages(JSP),还可以作为一个内嵌的容器来运行Web应用。Tomcat的...

    详解Maven命令大全

    它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理系统(Dependency Management System),和用来运行定义在生命周期阶段(phase)中插件(plugin)...

    maven构建web工程

    ** Maven构建Web工程详解 ** Maven是一款强大的项目管理和依赖管理工具,广泛应用于Java开发领域。它通过统一的构建过程和约定优于配置的原则,极大地简化了项目的构建、编译、测试和部署流程。在本篇内容中,我们...

    简单的maven webapp项目

    这个WAR文件可以直接部署到Tomcat或其他Servlet容器中运行。 总的来说,Maven通过提供一套标准化的流程,使得开发、测试和部署Web应用程序变得简单而高效。对于初学者,理解并熟练掌握Maven的工作原理和使用方法,...

    Maven入门教程

    ### Maven入门教程详解 #### Maven概述 Maven是一款强大的项目管理和构建工具,广泛应用于Java项目的构建、依赖管理和项目信息管理。Maven通过一系列预定义的生命周期、构建规则以及项目对象模型(POM)来实现自动...

    Java服务端+Vue前端+Android移动端+iOS移动端全栈系列源代码

    以下是针对这些技术栈的知识点详解: **Java服务端** Java作为后端开发的主要语言,其核心知识点包括: 1. **Spring Boot框架**:快速构建Web应用,内置Tomcat服务器,简化配置。 2. **RESTful API设计**:通过HTTP...

    maven-web-demo:Maven网络演示

    【 Maven 网络演示项目详解】 Maven 是一个强大的项目管理和依赖管理工具,尤其在 Java 开发领域中被广泛使用。"Maven Web Demo" 项目是一个典型的 Maven 应用示例,专为初学者设计,用于演示如何使用 Maven 来构建...

Global site tag (gtag.js) - Google Analytics