Tomcat runs the entry method of the entry class BootStrap, that is, the startup method; the startup of Tomcat is divided into two parts, init and start
- init: the initialization phase, that is, the initialization method of each component will be called in turn
- InitInternal: Standard~, Connector; because they inherited LifeCycleBase, the init method is implemented in template mode
- init: ProtocolHandler is an enum, Endpoint is an abstract class; they all define init by themselves, not LifeCycle's init
- start: The operating phase, that is, the start method of each component will be called in turn
- startInternal: Standard~, Connector; because they inherited LifeCycleBase, the start method is implemented in template mode
- start: ProtocolHandler is an enum, Endpoint is an abstract class; they all define start by themselves, not the start of LifeCycle
public static void main(String args[]) {
//1.初始化阶段 init()
if (daemon == null) {
// Don't set daemon until init() has completed
Bootstrap bootstrap = new Bootstrap();
try {
// 调用init()初始化BootStrap
bootstrap.init();
} catch (Throwable t) {
handleThrowable(t);
t.printStackTrace();
return;
}
// 将守护线程bootstop赋给守护线程daemon
daemon = bootstrap;
} else {
Thread.currentThread().setContextClassLoader(daemon.catalinaLoader);
}
//2.运行阶段 start()
try {
String command = "start";
if (args.length > 0) {
command = args[args.length - 1];
}
if (command.equals("startd")) {
args[args.length - 1] = "start";
// deamon.load(args),实际上会去调用Catalina#load(args)方法,
// 会去初始化一些资源,优先加载conf/server.xml,找不到再去加载server-embed.xml;
// 此外,load方法还会初始化Server
daemon.load(args);
// daemon.start(),实例上是调用Catalina.start()
daemon.start();
// 输入stop命令就停止
} else if (command.equals("stopd")) {
args[args.length - 1] = "stop";
daemon.stop();
} else if (command.equals("start")) {
daemon.setAwait(true);
daemon.load(args);
daemon.start();
} 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.");
}
} catch (Throwable t) {
// Unwrap the Exception for clearer error reporting
if (t instanceof InvocationTargetException &&
t.getCause() != null) {
t = t.getCause();
}
handleThrowable(t);
t.printStackTrace();
System.exit(1);
}
}