Lifecycle

Tomcat中进行生命周期管理的接口是Lifecycle,Tomcat中的组件基本都继承了该接口。
下面来看看该接口中生命周期方法的定义:
Tomcat生命周期 - 图1
Lifecycle定义了四种生命周期状态:init、start、stop、destroy,并且提供了四个方法,并且在接口中定义了关于这四个状态多包含的一些事件,整个状态流转如下图:

  1. 1. * The valid state transitions for components that support {@link Lifecycle}
  2. 2. * are:
  3. 3. * <pre>
  4. 4. * start()
  5. 5. * -----------------------------
  6. 6. * | |
  7. 7. * | init() |
  8. 8. * NEW ->-- INITIALIZING |
  9. 9. * | | | | ------------------<-----------------------
  10. 10. * | | |auto | | |
  11. 11. * | | \|/ start() \|/ \|/ auto auto stop() |
  12. 12. * | | INITIALIZED -->-- STARTING_PREP -->- STARTING -->- STARTED -->--- |
  13. 13. * | | | | | |
  14. 14. * | | | | | |
  15. 15. * | | | | | |
  16. 16. * | |destroy()| | | |
  17. 17. * | -->-----<-- auto auto | | |
  18. 18. * | | ---------<----- MUST_STOP ---------------------<-- | |
  19. 19. * | | | | |
  20. 20. * | \|/ ---------------------------<-------------------------------- ^
  21. 21. * | | | |
  22. 22. * | | \|/ auto auto start() |
  23. 23. * | | STOPPING_PREP ------>----- STOPPING ------>----- STOPPED ---->------
  24. 24. * | | ^ | | ^
  25. 25. * | | stop() | | | |
  26. 26. * | | -------------------------- | | |
  27. 27. * | | | auto | | |
  28. 28. * | | | MUST_DESTROY------<------- | |
  29. 29. * | | | | | |
  30. 30. * | | | |auto | |
  31. 31. * | | | destroy() \|/ destroy() | |
  32. 32. * | | FAILED ---->------ DESTROYING ---<----------------- |
  33. 33. * | | ^ | |
  34. 34. * | | destroy() | |auto |
  35. 35. * | -------->----------------- \|/ |
  36. 36. * | DESTROYED |
  37. 37. * | |
  38. 38. * | stop() |
  39. 39. * --->------------------------------>------------------------------
  40. 40. *

LifecycleBase

LifecycleBase是对Lifecycle中的init、start、stop和destroy方法进行了实现,并且提供了四个钩子方法,用于实现自身的初始化
Tomcat生命周期 - 图2
下面主要分析下init和start方法,另外两个方法类似

  1. 1. public final synchronized void init() throws LifecycleException {
  2. 2. //1:用于状态检查等
  3. 3. if (!state.equals(LifecycleState.NEW)) {
  4. 4. invalidTransition(Lifecycle.BEFORE_INIT_EVENT);
  5. 5. }
  6. 6. //2:设置状态
  7. 7. setStateInternal(LifecycleState.INITIALIZING, null, false);
  8. 8. //3:执行钩子方法,内部初始化
  9. 9. try {
  10. 10. initInternal();
  11. 11. } catch (Throwable t) {
  12. 12. ExceptionUtils.handleThrowable(t);
  13. 13. setStateInternal(LifecycleState.FAILED, null, false);
  14. 14. throw new LifecycleException(
  15. 15. sm.getString("lifecycleBase.initFail",toString()), t);
  16. 16. }
  17. 17. //4:设置状态
  18. 18. setStateInternal(LifecycleState.INITIALIZED, null, false);
  19. 19. }
  1. 1. public final synchronized void start() throws LifecycleException {
  2. 2. //1:状态判断或状态流转
  3. 3. if (LifecycleState.STARTING_PREP.equals(state) ||
  4. 4. LifecycleState.STARTING.equals(state) ||
  5. 5. LifecycleState.STARTED.equals(state)) {
  6. 6.
  7. 7. if (log.isDebugEnabled()) {
  8. 8. Exception e = new LifecycleException();
  9. 9. log.debug(sm.getString("lifecycleBase.alreadyStarted",
  10. 10. toString()), e);
  11. 11. } else if (log.isInfoEnabled()) {
  12. 12. log.info(sm.getString("lifecycleBase.alreadyStarted",
  13. 13. toString()));
  14. 14. }
  15. 15.
  16. 16. return;
  17. 17. }
  18. 18.
  19. 19. if (state.equals(LifecycleState.NEW)) {
  20. 20. init();
  21. 21. } else if (state.equals(LifecycleState.FAILED)){
  22. 22. stop();
  23. 23. } else if (!state.equals(LifecycleState.INITIALIZED) &&
  24. 24. !state.equals(LifecycleState.STOPPED)) {
  25. 25. invalidTransition(Lifecycle.BEFORE_START_EVENT);
  26. 26. }
  27. 27. //2:设置状态
  28. 28. setStateInternal(LifecycleState.STARTING_PREP, null, false);
  29. 29. //3:执行钩子方法
  30. 30. try {
  31. 31. startInternal();
  32. 32. } catch (Throwable t) {
  33. 33. ExceptionUtils.handleThrowable(t);
  34. 34. setStateInternal(LifecycleState.FAILED, null, false);
  35. 35. throw new LifecycleException(
  36. 36. sm.getString("lifecycleBase.startFail",toString()), t);
  37. 37. }
  38. 38. //4:设置状态
  39. 39. if (state.equals(LifecycleState.FAILED) ||
  40. 40. state.equals(LifecycleState.MUST_STOP)) {
  41. 41. stop();
  42. 42. } else {
  43. 43. // Shouldn't be necessary but acts as a check that sub-classes are
  44. 44. // doing what they are supposed to.
  45. 45. if (!state.equals(LifecycleState.STARTING)) {
  46. 46. invalidTransition(Lifecycle.AFTER_START_EVENT);
  47. 47. }
  48. 48.
  49. 49. setStateInternal(LifecycleState.STARTED, null, false);
  50. 50. }
  51. 51. }

生命周期这块可以参考下第二篇中 Tomcat的启动,里面就是父容器分别调用子容器进行初始化和start的