| 执行脚本参数,执行bootstrap.jar中的Bootstrap类中main方法,并传入参数start hift  eval exec ""$_RUNJAVA"" ""$LOGGING_CONFIG"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS   -D$ENDORSED_PROP=""$JAVA_ENDORSED_DIRS""   -classpath ""$CLASSPATH""   -Djava.security.manager   -Djava.security.policy==""$CATALINA_BASE/conf/catalina.policy""   -Dcatalina.base=""$CATALINA_BASE""   -Dcatalina.home=""$CATALINA_HOME""   -Djava.io.tmpdir=""$CATALINA_TMPDIR""   org.apache.catalina.startup.Bootstrap "$@" start 
 在上面脚本中我们可以看出最后执行的都是从Bootstrap的main方法作为入口的,所以我们打开Tomcat源码进去Bootstrap类中看它到底做了什么。 启动类分析 作为Tomcat的入口类,我们先看看Bootstrap中做了什么。这里只贴出main方法中重要的代码。 //初始化类加载器并且将Catalina文件加载进内存中 bootstrap.init(); String command = "start"; if (args.length > 0) {  command = args[args.length - 1]; } if (command.equals("startd")) {  args[args.length - 1] = "start";  //调用Catalina.java的load方法  daemon.load(args);  //调用Catalina.java的start  daemon.start(); } else if (command.equals("stopd")) {  args[args.length - 1] = "stop";  //调用Catalina.java的stop  daemon.stop(); } else if (command.equals("start")) {  daemon.setAwait(true);  daemon.load(args);  daemon.start();  if (null == daemon.getServer()) {  System.exit(1);  } } else if (command.equals("stop")) {  daemon.stopServer(args); } else if (command.equals("configtest")) {  daemon.load(args);  if (null == daemon.getServer()) {  System.exit(1);  }  System.exit(0); } else {  log.warn("Bootstrap: command "" + command + "" does not exist."); } 
 这里是根据脚本中传入的不同命令,调用Catalina不同的方法。由于我们主要分析的Tomcat如何做到一键式启停的,所以我们主要分析Catalina的start方法。 在Catalina的satrt方法中我们看到了这一句 getServer().start(); 
 随后经过Debug都是经过了Lifecycle的start方法,我们把Lifecycle的方法列出来 public interface Lifecycle {  public void addLifecycleListener(LifecycleListener listener);  public LifecycleListener[] findLifecycleListeners();  public void removeLifecycleListener(LifecycleListener listener);  public void init() throws LifecycleException;  public void start() throws LifecycleException;  public void stop() throws LifecycleException;  public void destroy() throws LifecycleException;  public LifecycleState getState();  public String getStateName();  public interface SingleUse {  } } 
 (编辑:宣城站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |