Lifecycle
Tomcat中进行生命周期管理的接口是Lifecycle,Tomcat中的组件基本都继承了该接口。
下面来看看该接口中生命周期方法的定义:
Lifecycle定义了四种生命周期状态:init、start、stop、destroy,并且提供了四个方法,并且在接口中定义了关于这四个状态多包含的一些事件,整个状态流转如下图:
1. * The valid state transitions for components that support {@link Lifecycle}
2. * are:
3. * <pre>
4. * start()
5. * -----------------------------
6. * | |
7. * | init() |
8. * NEW ->-- INITIALIZING |
9. * | | | | ------------------<-----------------------
10. * | | |auto | | |
11. * | | \|/ start() \|/ \|/ auto auto stop() |
12. * | | INITIALIZED -->-- STARTING_PREP -->- STARTING -->- STARTED -->--- |
13. * | | | | | |
14. * | | | | | |
15. * | | | | | |
16. * | |destroy()| | | |
17. * | -->-----<-- auto auto | | |
18. * | | ---------<----- MUST_STOP ---------------------<-- | |
19. * | | | | |
20. * | \|/ ---------------------------<-------------------------------- ^
21. * | | | |
22. * | | \|/ auto auto start() |
23. * | | STOPPING_PREP ------>----- STOPPING ------>----- STOPPED ---->------
24. * | | ^ | | ^
25. * | | stop() | | | |
26. * | | -------------------------- | | |
27. * | | | auto | | |
28. * | | | MUST_DESTROY------<------- | |
29. * | | | | | |
30. * | | | |auto | |
31. * | | | destroy() \|/ destroy() | |
32. * | | FAILED ---->------ DESTROYING ---<----------------- |
33. * | | ^ | |
34. * | | destroy() | |auto |
35. * | -------->----------------- \|/ |
36. * | DESTROYED |
37. * | |
38. * | stop() |
39. * --->------------------------------>------------------------------
40. *
LifecycleBase
LifecycleBase是对Lifecycle中的init、start、stop和destroy方法进行了实现,并且提供了四个钩子方法,用于实现自身的初始化
下面主要分析下init和start方法,另外两个方法类似
1. public final synchronized void init() throws LifecycleException {
2. //1:用于状态检查等
3. if (!state.equals(LifecycleState.NEW)) {
4. invalidTransition(Lifecycle.BEFORE_INIT_EVENT);
5. }
6. //2:设置状态
7. setStateInternal(LifecycleState.INITIALIZING, null, false);
8. //3:执行钩子方法,内部初始化
9. try {
10. initInternal();
11. } catch (Throwable t) {
12. ExceptionUtils.handleThrowable(t);
13. setStateInternal(LifecycleState.FAILED, null, false);
14. throw new LifecycleException(
15. sm.getString("lifecycleBase.initFail",toString()), t);
16. }
17. //4:设置状态
18. setStateInternal(LifecycleState.INITIALIZED, null, false);
19. }
1. public final synchronized void start() throws LifecycleException {
2. //1:状态判断或状态流转
3. if (LifecycleState.STARTING_PREP.equals(state) ||
4. LifecycleState.STARTING.equals(state) ||
5. LifecycleState.STARTED.equals(state)) {
6.
7. if (log.isDebugEnabled()) {
8. Exception e = new LifecycleException();
9. log.debug(sm.getString("lifecycleBase.alreadyStarted",
10. toString()), e);
11. } else if (log.isInfoEnabled()) {
12. log.info(sm.getString("lifecycleBase.alreadyStarted",
13. toString()));
14. }
15.
16. return;
17. }
18.
19. if (state.equals(LifecycleState.NEW)) {
20. init();
21. } else if (state.equals(LifecycleState.FAILED)){
22. stop();
23. } else if (!state.equals(LifecycleState.INITIALIZED) &&
24. !state.equals(LifecycleState.STOPPED)) {
25. invalidTransition(Lifecycle.BEFORE_START_EVENT);
26. }
27. //2:设置状态
28. setStateInternal(LifecycleState.STARTING_PREP, null, false);
29. //3:执行钩子方法
30. try {
31. startInternal();
32. } catch (Throwable t) {
33. ExceptionUtils.handleThrowable(t);
34. setStateInternal(LifecycleState.FAILED, null, false);
35. throw new LifecycleException(
36. sm.getString("lifecycleBase.startFail",toString()), t);
37. }
38. //4:设置状态
39. if (state.equals(LifecycleState.FAILED) ||
40. state.equals(LifecycleState.MUST_STOP)) {
41. stop();
42. } else {
43. // Shouldn't be necessary but acts as a check that sub-classes are
44. // doing what they are supposed to.
45. if (!state.equals(LifecycleState.STARTING)) {
46. invalidTransition(Lifecycle.AFTER_START_EVENT);
47. }
48.
49. setStateInternal(LifecycleState.STARTED, null, false);
50. }
51. }
生命周期这块可以参考下第二篇中 Tomcat的启动,里面就是父容器分别调用子容器进行初始化和start的