Tomcat组件的生命周期Lifecycle

时间:2021-01-01 08:59:44

Tomcat中有很多组件,Tomcat启动的时候这些组件就会启动,当Tomcat关闭的时候这些组件就会关闭.

实现Lifecycle接口的最贱可以触发下面这些LifecycleEvent:START_EVENT, BEFORE_START_EVENT, AFTER_START_EVENT, STOP_EVENT, BEFORE_STOP_EVENT, AFTER_STOP_EVENT.


当实现Lifecycle接口的组件触发上述的事件的时候,会调用注册在实现Lifecycle接口组件中的监听器LifecycleListener.


上述的过程其实和我们Tomcat中的Servlet的监听器很类似.可以说就是一个事件监听模型.


下面我们挨个介绍使用到的几个接口:

1.Lifecycle 事件源,发布事件给监听器,表示具有生命周期的组件

2.LifecyceEvent 事件, 表示Lifecycle触发的事件,触发的事件会通知监听该Lifecycle的LifecycleListener.

3.LifecycleListener 事件监听器,监听Lifecycle组件, 当Liferay组件触发某事件的时候, LifecycleListener 就会收到Lifecycle发送过来的 LifecyceEvent

4.LifecycleSupport 负责Lifecycle组件的事件管理工作

简单的来说就是Lifecycle相当于一个新闻订阅中心,LifecycleEvent相当于消息,LifecycleListener相当于订阅者.当我们订阅新闻(监听器注册在Lifecycle组件中),新闻订阅中心有新动态发布的时候,会把消息发送给订阅者(.LifecycleListener)最新的动态(LifecyceEvent) 

1.Lifecycle


package weber.test.tomcat.lifecycle;

/**
* 实现该接口,表示该接口具有Lifecycle生命周期,可以触发以下的事件
* 1.可以注册监听器,删除监听器
* 2.启动调用start方法 停止调用stop方法
*
*/
public interface Lifecycle {

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 void addLifecycleListener(LifecycleListener listener);

public LifecycleListener[] findLifecycleListeners();

public void removeLifecycleListener(LifecycleListener listener);

public void start();

public void stop();

}
2.LifecyceEvent

package weber.test.tomcat.lifecycle;

import java.util.EventObject;

/**
*
* Lifecycle组件促发事件发送LifecycleEvent给LifecycleListener
* Event包含事件信息,事件源等
*
*/
public final class LifecycleEvent extends EventObject {

private static final long serialVersionUID = 1L;

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;

}

private Object data = null;

private Lifecycle lifecycle = null;

private String type = null;

public Object getData() {

return (this.data);

}

public Lifecycle getLifecycle() {

return (this.lifecycle);

}

public String getType() {

return (this.type);

}

}


3.LifecycleListener

package weber.test.tomcat.lifecycle;

/**
*
* 注册到Lifecycle组件的监听器, 当Lifecycle组件触发事件的时候,会调用lifecycleEvent方法
*/
public interface LifecycleListener {

public void lifecycleEvent(LifecycleEvent event);

}


4.LifecycleSupport

package weber.test.tomcat.lifecycle;

/**
*
* 该类实际用来管理Lifecycle的事件,包括事件监听器的注册和删除,触发事件
* 一般来说,我们直接在Lifecycle的实例中使用一个LifecycleSupport实例来实现这个效果.
* 详细请看LifecycleComponent的代码
*
*/
public final class LifecycleSupport {

public LifecycleSupport(Lifecycle lifecycle) {

super();
this.lifecycle = lifecycle;

}

private Lifecycle lifecycle = null;

private LifecycleListener listeners[] = new LifecycleListener[0];

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;
}

}

}

测试代码

1.LifecycleComponent 实现Lifecycle的组件

package weber.test.tomcat.lifecycle;

/**
*
* 测试使用的实现Lifecycle的组件
*
*/
public class LifecycleComponent implements Lifecycle {

//相当于组件的事件监听管理器
private LifecycleSupport lifecycle = new LifecycleSupport(this);

@Override
public void addLifecycleListener(LifecycleListener listener) {
lifecycle.addLifecycleListener(listener);
}

@Override
public LifecycleListener[] findLifecycleListeners() {
return lifecycle.findLifecycleListeners();
}

@Override
public void removeLifecycleListener(LifecycleListener listener) {
lifecycle.removeLifecycleListener(listener);
}

@Override
public void start() {
//启动前触发启动前的事件
lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
System.out.println("LifecycleComponent start");
lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
}

@Override
public void stop() {
lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, null);
lifecycle.fireLifecycleEvent(STOP_EVENT, null);
System.out.println("LifecycleComponent stop");
lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null);
}

}

2,SimpleLifecycleListener 实现LifecycleListener接口的事件监听器

package weber.test.tomcat.lifecycle;

/**
*
* 测试使用的简单事件监听器
* 我们将使用它注册到我们的测试Lifecycle组件中
*
*/
public class SimpleLifecycleListener implements LifecycleListener {

public void lifecycleEvent(LifecycleEvent event) {

Lifecycle lifecycle = event.getLifecycle();
System.out.println("SimpleLifecycleListener's event " + event.getType().toString());
if (Lifecycle.START_EVENT.equals(event.getType())) {
System.out.println("Starting .");
} else if (Lifecycle.STOP_EVENT.equals(event.getType())) {
System.out.println("Stopping.");
}
}
}

3.测试main类LifecycleTest

package weber.test.tomcat.lifecycle;

/**
*
* 测试mian
*
*/
public class LifecycleTest {

public static void main(String[] args) throws InterruptedException {

//new 一个组件实例
LifecycleComponent compoment = new LifecycleComponent();
//给组件注册事件监听器
compoment.addLifecycleListener(new SimpleLifecycleListener());
//组件调用Lifecycle的启动函数
compoment.start();
Thread.sleep(1000);
//组件调用Lifecycle的停止函数
compoment.stop();
}

}

运行结果:

SimpleLifecycleListener's event before_start
LifecycleComponent start
SimpleLifecycleListener's event after_start
SimpleLifecycleListener's event before_stop
SimpleLifecycleListener's event stop
Stopping.
LifecycleComponent stop
SimpleLifecycleListener's event after_stop