tomcat study 第六章.生命周期

本文介绍了Tomcat中组件的生命周期管理方式,重点讲解了如何使用Lifecycle接口来启动和停止组件,以及如何通过监听器机制触发特定事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Lifecycle接口(第六章.生命周期)
Catalina的设计允许一个组件包含其它的组件。例如一个容器可以包含一系列的组件如加载器、管理器等。一个父组件负责启动和停止其子组件。Catalina的设计成所有的组件被一个父组件来管理(in custody),所以启动bootstrap类只需启动一个组件即可。这种单一的启动停止机制通过继承Lifecycle来实现。
1.监听器Listener:(第六章.生命周期)
StandardWrapper中有InstanceSupport instanceSupport = new InstanceSupport(this);
StandardManager中有LifecycleSupport lifecycle = new LifecycleSupport(this);
而LifecycleSupport又将StandardManager设为他的属性值
StandardManager中:
    public void addLifecycleListener(LifecycleListener listener) {

        lifecycle.addLifecycleListener(listener);
    }
在LifecycleSupport中有:专门存放监听器(*Support含有LifecycleListener listeners[]属性)
private LifecycleListener listeners[] = new LifecycleListener[0];
通过这个方法触发事件
    public void fireLifecycleEvent(String type, Object data) {
	//将StandardManager和type(start_before,stop_aftert,……)和要传得data数据
//包装LifecycleEvent传递给监听器Listener
        LifecycleEvent event = new LifecycleEvent(lifecycle, type, data);
        LifecycleListener interested[] = null;
        synchronized (listeners) {
            interested = (LifecycleListener[]) listeners.clone();
        }
        for (int i = 0; i < interested.length; i++)
//从而让监听器Listener触发:
            interested[i].lifecycleEvent(event);
    }
监听器Listener必须实现LifecycleListener接口
public interface LifecycleListener {

    public void lifecycleEvent(LifecycleEvent event);
}
监听器Listener:就可以根据需要做一些操作
下面是ServerLifecycleListener类的lifecycleEvent方法:
public void lifecycleEvent(LifecycleEvent event) {
        Lifecycle lifecycle = event.getLifecycle();
        if (Lifecycle.START_EVENT.equals(event.getType())) {

            if (lifecycle instanceof Server) {

                // Loading additional MBean descriptors
                loadMBeanDescriptors();
                createMBeans();
            }
            /*
            // Ignore events from StandardContext objects to avoid
            // reregistering the context
            if (lifecycle instanceof StandardContext)
                return;
            createMBeans();
            */

        } else if (Lifecycle.STOP_EVENT.equals(event.getType())) {

            if (lifecycle instanceof Server) {
                destroyMBeans();
            }

        } else if (Context.RELOAD_EVENT.equals(event.getType())) {

            // Give context a new handle to the MBean server if the
            // context has been reloaded since reloading causes the
            // context to lose its previous handle to the server
            if (lifecycle instanceof StandardContext) {
                // If the context is privileged, give a reference to it
                // in a servlet context attribute
                StandardContext context = (StandardContext)lifecycle;
                if (context.getPrivileged()) {
                    context.getServletContext().setAttribute
                        (Globals.MBEAN_REGISTRY_ATTR,
                         MBeanUtils.createRegistry());
                    context.getServletContext().setAttribute
                        (Globals.MBEAN_SERVER_ATTR,
                         MBeanUtils.createServer());
                }
            }
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值