- 浏览: 1012306 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (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 984一 windows下安装 直接 ... -
tomcat 7 源码分析-14 tomcat的container设计
2011-03-11 19:18 1546tomcat 7 源码分析-14 tomca ... -
tomcat 7 源码分析-13 处理request的Valve和Valve的链表Pipeline
2011-03-11 19:17 1128tomcat 7 源码分析-13 处理request的Val ... -
tomcat 7 源码分析-12 Enumeration枚举
2011-03-11 19:14 1250tomcat 7 源码分析-12 Enumeration枚举 ... -
tomcat 7 源码分析-11 tomcat对http协议的实现
2011-03-11 19:13 1401tomcat 7 源码分析-11 tomcat对http协议 ... -
tomcat 7 源码分析-10 线程池ThreadPoolExecutor
2011-03-11 19:12 2211tomcat 7 源码分析-10 线程池ThreadPool ... -
tomcat 7 源码分析-9 tomcat对ServerSocket的封装和使用
2011-03-11 19:11 1635tomcat 7 源码分析-9 tomcat对ServerS ... -
tomcat 7 源码分析-7 server初始化中的JMX(DynamicMBean)再续
2011-03-11 19:09 1041tomcat 7 源码分析-7 server初始化中的JMX ... -
tomcat 7 源码分析-6 server初始化中的JMX(DynamicMBean)续
2011-03-11 19:08 1189tomcat 7 源码分析-6 server ... -
tomcat 7 源码分析-5 server初始化中的JMX(DynamicMBean)
2011-03-11 19:08 1364tomcat 7 源码分析-5 server初始化中的JMX ... -
tomcat 7 源码分析-4 server初始化背后getServer().init()
2011-03-11 19:06 1352tomcat 7 源码分析-4 server初始化背后get ... -
tomcat 7 源码分析-3 使用Digester读取xml文件实例化server
2011-03-11 19:05 1388tomcat 7 源码分析-3 使用Digester读取xm ... -
tomcat 7 源码分析-2 类加载ClassLoader
2011-03-11 19:04 1932tomcat 7 源码分析-2 类加载ClassLoader ... -
tomcat 7 源码分析-1 关于读取properties及注册系统properties
2011-03-11 19:02 1591tomcat 7 源码分析-1 关于读取properties ... -
Tomcat的四种基于HTTP协议的Connector性能比较
2011-03-11 17:58 1114Tomcat的四种基于HTTP协议的Connector性能比较 ...
相关推荐
在Tomcat 7的源码中,`getServer().init()`方法可能位于`StandardServer`类中,这个类实现了`Lifecycle`接口,管理服务器的生命周期状态。当调用`init()`方法时,Tomcat会执行以下主要步骤: 1. 加载全局JNDI资源:...
jar包,亲测可用
为了帮助开发者更有效地跟踪和控制这些组件的状态,出现了一类被称为"生命周期监听工具库"的辅助工具,如"Android-Android生命周期监听工具库"。这类库通常提供便利的方法来监控和管理应用程序的生命周期,以避免...
Tomcat7的生命周期管理是另一个重要的概念。每个组件都有自己的启动和停止方法,这些方法在`Lifecycle`接口中定义。通过观察源码,我们可以了解到如何正确初始化和关闭Tomcat的各种组件。 此外,源码中还有许多值得...
7. **集成与使用**:将tc-init-health-check-listener集成到你的Tomcat服务器中,可能需要修改Tomcat的`server.xml`配置文件,添加自定义的监听器配置,然后按照项目的文档指示进行编译和部署。 8. **自定义健康...
4. Lifecycle:生命周期接口,用于管理和控制组件的启动、停止、暂停和恢复等状态变化。 二、启动流程 1. 初始化:Tomcat启动时,会读取配置文件server.xml,解析配置信息,构建出服务器的结构。 2. 加载Web应用:...
Android官方activity-lifecycle生命周期例子,参考网页: http://developer.android.com/training/basics/activity-lifecycle/index.html 方便了解Activity生命周期,提供了非常形象的说明。生动讲解。可以结合官网...
jar包,亲测可用
jar包,亲测可用
jar包,亲测可用
jar包,亲测可用
jar包,亲测可用
jar包,亲测可用
jar包,亲测可用
了解并掌握Tomcat的生命周期和事件管理,对于开发者来说至关重要,它能帮助我们更好地控制应用的行为,优化性能,以及在出现问题时快速定位和解决问题。在实际开发中,我们可能需要编写自定义的`LifecycleListener`...
### Tomcat架构的源码分析 #### 一、Tomcat的架构概述 Tomcat作为一款广泛使用的开源Java Servlet容器,其内部架构设计简洁而高效。本文档将对Tomcat的架构进行详细介绍,并从源码层面深入分析其核心组成部分。...
Tomcat提供自动部署和管理Web应用的功能,这涉及到`StandardContext`和`LifeCycle`接口的实现。 6. **安全管理**: Tomcat的安全特性包括用户认证、角色授权等,这在`org.apache.catalina.security`和`org.apache...
4. JMX Remote Lifecycle Listener:允许远程管理Tomcat实例。 安装Apache Tomcat 7.0.59时,你需要解压rar文件,然后设置CATALINA_HOME环境变量指向解压后的目录。接着,你可以通过bin目录下的startup.sh(Unix/...
kie workbench:6.5 相关依赖包下载: copy following libs into TOMCAT_HOME/lib ... btm-tomcat55-lifecycle-2.1.4.jar * h2-1.3.161.jar * jta-1.1.jar * slf4j-api-1.7.2.jar * slf4j-jdk14-1.7.2.jar