- 浏览: 1035778 次
- 性别:
- 来自: 杭州
-
文章分类
- 全部博客 (826)
- 硬件 (8)
- 软件 (24)
- 软件工程 (34)
- JAVA (229)
- C/C++/C# (77)
- JavaScript (8)
- PHP (1)
- Ruby (3)
- MySQL (14)
- 数据库 (19)
- 心情记事 (12)
- 团队管理 (19)
- Hadoop (1)
- spring (22)
- mybatis(ibatis) (7)
- tomcat (16)
- velocity (0)
- 系统架构 (6)
- JMX (8)
- proxool (1)
- 开发工具 (16)
- python (10)
- JVM (27)
- servlet (5)
- JMS (26)
- ant (2)
- 设计模式 (5)
- 智力题 (2)
- 面试题收集 (1)
- 孙子兵法 (16)
- 测试 (1)
- 数据结构 (7)
- 算法 (22)
- Android (11)
- 汽车驾驶 (1)
- lucene (1)
- memcache (12)
- 技术架构 (7)
- OTP-Erlang (7)
- memcached (17)
- redis (20)
- 浏览器插件 (3)
- sqlite (3)
- Heritrix (9)
- Java线程 (1)
- scala (0)
- Mina (6)
- 汇编 (2)
- Netty (15)
- libevent (0)
- CentOS (12)
- mongod (5)
- mac os (0)
最新评论
-
kingasdfg:
你这里面存在一个错误添加多个任务 应该是这样的 /** * ...
Quartz的任务的临时启动和暂停和恢复【转】 -
kyzeng:
纠正一个错误,long型对应的符号是J,不是L。
Jni中C++和Java的参数传递 -
zhaohaolin:
抱歉,兄弟,只是留下作记录,方便学习,如果觉得资料不好,可以到 ...
netty的个人使用心得【转】 -
cccoooccooco:
谢谢!自己一直以为虚机得使用网线才可以与主机连接呢。。
主机网卡无网线连接与虚拟机通信 -
yuqilin001:
要转别人的东西,请转清楚点嘛,少了这么多类,误人子弟
netty的个人使用心得【转】
tomcat 7 源码分析-8 生命周期lifecycle和监听listener
每个应用都有生命周期lifecycle,可能包括init,start,stop,destroy等更多。针对生命周期的变化,如何做变化作出反 应,tomcat在设计的时候,把时间监听listener结合起来,所以listener取的名字是lifecyclelistener,对 lifecycle进行监听。
总的最底层的来自两个interface的设计Lifecycle和LifecycleListener。
lifecycle这个接口定义了除本身生命周期的函数,另外还定义了整个生命周期阶段的事件类型(这个肯定是为 lifecyclelistener准备的),同时还对LifecycleListener有增加,删除,通知等的函数。 LifecycleListener这个接口定义了对事件Event的反应。简单看下Lifecycle和LifecycleListener的代码片 段。
- public interface Lifecycle {
- /**
- * The LifecycleEvent type for the "component init" event.
- */
- public static final String INIT_EVENT = "init" ;
- /**
- * The LifecycleEvent type for the "component start" event.
- */
- public static final String START_EVENT = "start" ;
- /**
- * The LifecycleEvent type for the "component before start" event.
- */
- public static final String BEFORE_START_EVENT = "before_start" ;
- [..........]
- // --------------------------------------------------------- Public Methods
- /**
- * Add a LifecycleEvent listener to this component.
- *
- * @param listener The listener to add
- */
- public void addLifecycleListener(LifecycleListener listener);
- /**
- * Get the lifecycle listeners associated with this lifecycle. If this
- * Lifecycle has no listeners registered, a zero-length array is returned.
- */
- public LifecycleListener[] findLifecycleListeners();
- [.............]
- public void init() throws LifecycleException;
- public void start() throws LifecycleException;
- public void stop() throws LifecycleException;
- public void destroy() throws LifecycleException;
- public LifecycleState getState();
- }
public interface Lifecycle { /** * The LifecycleEvent type for the "component init" event. */ public static final String INIT_EVENT = "init"; /** * The LifecycleEvent type for the "component start" event. */ public static final String START_EVENT = "start"; /** * The LifecycleEvent type for the "component before start" event. */ public static final String BEFORE_START_EVENT = "before_start"; [..........] // --------------------------------------------------------- Public Methods /** * Add a LifecycleEvent listener to this component. * * @param listener The listener to add */ public void addLifecycleListener(LifecycleListener listener); /** * Get the lifecycle listeners associated with this lifecycle. If this * Lifecycle has no listeners registered, a zero-length array is returned. */ public LifecycleListener[] findLifecycleListeners(); [.............] public void init() throws LifecycleException; public void start() throws LifecycleException; public void stop() throws LifecycleException; public void destroy() throws LifecycleException; public LifecycleState getState(); }
- public interface LifecycleListener {
- /**
- * Acknowledge the occurrence of the specified event.
- *
- * @param event LifecycleEvent that has occurred
- */
- public void lifecycleEvent(LifecycleEvent event);
- }
public interface LifecycleListener { /** * Acknowledge the occurrence of the specified event. * * @param event LifecycleEvent that has occurred */ public void lifecycleEvent(LifecycleEvent event); }
后续的问题是,谁来维护LifecycleListener?谁维护LifecycleListener,必然要提供真正意思上对操作(增,删, 查和通知)函数。Tomcat将这些交给了LifecycleSupport。同时LifecycleSupport还提供对lifecycle传来的 bype,data封装成
LifecycleEvent类,给LifecycleListener用。
- public final class LifecycleSupport {
- public LifecycleSupport(Lifecycle lifecycle) {
- super ();
- this .lifecycle = lifecycle;
- }
- private Lifecycle lifecycle = null ;
- private LifecycleListener listeners[] = new LifecycleListener[ 0 ];
- private final Object listenersLock = new Object(); // Lock object for changes to listeners
- public void addLifecycleListener(LifecycleListener listener) {
- synchronized (listenersLock) {
- 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[] = listeners;
- for ( int i = 0 ; i < interested.length; i++)
- interested[i].lifecycleEvent(event);
- }
- public void removeLifecycleListener(LifecycleListener listener) {
- synchronized (listenersLock) {
- 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;
- }
- }
- }
public final class LifecycleSupport { public LifecycleSupport(Lifecycle lifecycle) { super(); this.lifecycle = lifecycle; } private Lifecycle lifecycle = null; private LifecycleListener listeners[] = new LifecycleListener[0]; private final Object listenersLock = new Object(); // Lock object for changes to listeners public void addLifecycleListener(LifecycleListener listener) { synchronized (listenersLock) { 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[] = listeners; for (int i = 0; i < interested.length; i++) interested[i].lifecycleEvent(event); } public void removeLifecycleListener(LifecycleListener listener) { synchronized (listenersLock) { 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; } } }
tomcat的应用都继承之Lifecycle,包括server,service,Connector,GlobalNamingResources等,而针对这些又实现不同的LifecycleListener。
发表评论
-
tomcat7.0.8的高级应用-apr1.4.2安装
2011-03-24 17:16 1002一 windows下安装 直接 ... -
tomcat 7 源码分析-14 tomcat的container设计
2011-03-11 19:18 1569tomcat 7 源码分析-14 tomca ... -
tomcat 7 源码分析-13 处理request的Valve和Valve的链表Pipeline
2011-03-11 19:17 1144tomcat 7 源码分析-13 处理request的Val ... -
tomcat 7 源码分析-12 Enumeration枚举
2011-03-11 19:14 1267tomcat 7 源码分析-12 Enumeration枚举 ... -
tomcat 7 源码分析-11 tomcat对http协议的实现
2011-03-11 19:13 1423tomcat 7 源码分析-11 tomcat对http协议 ... -
tomcat 7 源码分析-10 线程池ThreadPoolExecutor
2011-03-11 19:12 2233tomcat 7 源码分析-10 线程池ThreadPool ... -
tomcat 7 源码分析-9 tomcat对ServerSocket的封装和使用
2011-03-11 19:11 1662tomcat 7 源码分析-9 tomcat对ServerS ... -
tomcat 7 源码分析-7 server初始化中的JMX(DynamicMBean)再续
2011-03-11 19:09 1057tomcat 7 源码分析-7 server初始化中的JMX ... -
tomcat 7 源码分析-6 server初始化中的JMX(DynamicMBean)续
2011-03-11 19:08 1209tomcat 7 源码分析-6 server ... -
tomcat 7 源码分析-5 server初始化中的JMX(DynamicMBean)
2011-03-11 19:08 1379tomcat 7 源码分析-5 server初始化中的JMX ... -
tomcat 7 源码分析-4 server初始化背后getServer().init()
2011-03-11 19:06 1384tomcat 7 源码分析-4 server初始化背后get ... -
tomcat 7 源码分析-3 使用Digester读取xml文件实例化server
2011-03-11 19:05 1420tomcat 7 源码分析-3 使用Digester读取xm ... -
tomcat 7 源码分析-2 类加载ClassLoader
2011-03-11 19:04 1956tomcat 7 源码分析-2 类加载ClassLoader ... -
tomcat 7 源码分析-1 关于读取properties及注册系统properties
2011-03-11 19:02 1615tomcat 7 源码分析-1 关于读取properties ... -
Tomcat的四种基于HTTP协议的Connector性能比较
2011-03-11 17:58 1136Tomcat的四种基于HTTP协议的Connector性能比较 ...
相关推荐
Tomcat7的生命周期管理是另一个重要的概念。每个组件都有自己的启动和停止方法,这些方法在`Lifecycle`接口中定义。通过观察源码,我们可以了解到如何正确初始化和关闭Tomcat的各种组件。 此外,源码中还有许多值得...
【tc-init-health-check-listener-7.0.52.zip】是一个开源项目,主要关注的是对Tomcat服务器的健康检查和初始化监听器。这个项目旨在提供一种方式来监控和管理Tomcat服务器的生命周期,确保其在运行过程中能够稳定、...
Tomcat5的每个组件都有自己的生命周期,包括初始化、启动、停止和销毁等阶段。这些阶段通过接口`Lifecycle`和`LifecycleListener`来控制,使得开发者能够灵活地插入自定义逻辑。 7. **容器和组件** Tomcat5采用...
6. **Lifecycle Interfaces**: Tomcat API包含一系列生命周期接口,如Lifecycle、LifecycleListener、LifecycleState等,用于管理组件的创建、启动、停止和销毁过程。开发者可以通过实现这些接口,监控和干预Tomcat...
第6章:生命周期(Lifecycle) 容器和其组件都有各自的生命周期,包括初始化、启动、停止和销毁等阶段。本章将探讨这些阶段的详细过程,以及如何通过接口和事件来控制这些阶段。 第7章:Logger 日志系统在Tomcat中...
通过`<listener>`元素添加监听器,用于监听Web应用生命周期中的特定事件。 15. **J2EE元素** `web.xml`还可以包含J2EE规范定义的其他元素,用于实现更复杂的应用功能,如EJB、JMS、JNDI等。 以上是`web.xml`控制...
- **Phase Listener**: 监听JSF生命周期的各个阶段,可用于自定义行为。 - **Converter和Validator**: 分别用于数据格式转换和验证,确保输入数据的正确性。 综上所述,JSF开发涉及多个层面的知识,包括但不限于...
在Tomcat中,事件机制用于管理服务器的生命周期,比如容器的启动、停止、加载和卸载等。Tomcat使用`Lifecycle`接口和一系列的`LifecycleEvent`来表示这些状态变化。开发者可以通过实现`LifecycleListener`接口来监听...
7.2 Tomcat的生命周期管理52 7.2.1 Lifecycle接口52 7.2.2 LifecycleBase53 7.3 Container分析59 7.3.1 ContainerBase的结构59 7.3.2 Container的4个子容器60 7.3.3 4种容器的配置方法60 7.3.4 Container的...