知识库--Lifecycle (44)

来源:互联网 发布:明略数据工资待遇 编辑:程序博客网 时间:2024/05/29 21:32

Lifecycle

Catalina consists of many components. When Catalina is started, these components need to be started as well. When Catalina is stopped, these components must also be given a chance to do a clean-up. For example, when the container is stopped, it must invoke the destroy method of all loaded servlets and the session manager must save the session objects to secondary storage. A consistent mechanism for starting and stopping components is achieved by implementing the org.apache.catalina.Lifecycle interface.

A component implementing the Lifecycle interface can also trigger one or many of the following events: BEFORE_START_EVENT, START_EVENT, AFTER_START_EVENT,
BEFORE_STOP_EVENT, STOP_EVENT, and AFTER_STOP_EVENT. The first three events are normally fired when the component is started and the last three when the component is stopped. An event is represented by the org.apache.catalina.LifecycleEvent class. And , of course, if a Catalina component can trigger events, there must be event listeners that you can write to response to those events. A listener is represented by the org.apache.catalina.LifecycleListener interface.

Three types Lifecycle, LifecycleEvent , and LifecycleListener. In addition, it will also explain a utility class called LifecycleSupport that provides an easy way for a component to fire lifecycle events and deal with lifecycle listeners.

The design of Catalina allows a component to contain other components. For example, a container can contain components such as a loader, a manager, etc. A parent component is responsible for starting and stopping its child components. The design of Catalina is such that all components but one are put “in custody” of a parent component so that a bootstrap class needs only single component. This single start/stop mechanism is made possible through the Lifecycle interface.

public interface Lifecylcle{    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[] findLifecycleListener();    public void removeLifecycleListener(LifecycleListener listener);    public void start() throws LifecycleException;    public void stop() throws LifecycleException;}
0 0