一、获取源码并启动程序
获取教程地址
总体架构
二、Tomcat的启动入口
Catalina类主要负责 具体的管理类,而Bootstrap类是启动的入口(main方法)。
/**
* Main method and entry point when starting Tomcat via the provided
* scripts.
*
* @param args Command line arguments to be processed
*/
public static void main(String args[]) {
synchronized (daemonLock) {
if (daemon == null) {
// Don't set daemon until init() has completed
Bootstrap bootstrap = new Bootstrap();
try {
bootstrap.init();
} catch (Throwable t) {
handleThrowable(t);
t.printStackTrace();
return;
}
daemon = bootstrap;
} else {
// When running as a service the call to stop will be on a new
// thread so make sure the correct class loader is used to
// prevent a range of class not found exceptions.
Thread.currentThread().setContextClassLoader(daemon.catalinaLoader);
}
}
try {
String command = "start";
if (args.length > 0) {
command = args[args.length - 1];
}
if (command.equals("startd")) {
args[args.length - 1] = "start";
daemon.load(args);
daemon.start();
} 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();
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.");
}
} 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);
}
}
2.1 Bootstrap#init()
/**
* Initialize daemon.
* @throws Exception Fatal initialization error
*/
public void init() throws Exception {
//1、初始化类加载器
initClassLoaders();
//2、设置当前线程的上下文加载器为catalinaLoader
Thread.currentThread().setContextClassLoader(catalinaLoader);
//3、预先加载tomcat、javax包的自定义类
SecurityClassLoad.securityClassLoad(catalinaLoader);
// 加载启动类以及调用setParentClassLoad()方法 if (log.isDebugEnabled())
if (log.isDebugEnabled()) {
log.debug("Loading startup class");
}
//4.通过反射创建Catalina对象
Class<?> startupClass = catalinaLoader.loadClass("org.apache.catalina.startup.Catalina");
Object startupInstance = startupClass.getConstructor().newInstance();
// Set the shared extensions class loader
if (log.isDebugEnabled()) {
log.debug("Setting startup class properties");
}
String methodName = "setParentClassLoader";
Class<?> paramTypes[] = new Class[1];
paramTypes[0] = Class.forName("java.lang.ClassLoader");
Object paramValues[] = new Object[1];
paramValues[0] = sharedLoader;
Method method =
startupInstance.getClass().getMethod(methodName, paramTypes);
method.invoke(startupInstance, paramValues);
//5.将startupInstance对象赋值给catalinaDaemon
catalinaDaemon = startupInstance;
}
关键步骤
- 初始化类加载器
- 设置当前线程的上下文加载器为catalinaLoader
- 预先加载tomcat、javax包的自定义类
- 通过反射创建Catalina对象
- 将startupInstance对象赋值给catalinaDaemon
2.2 Bootstrap#init()#initClassLoaders()
init()初始化的方法也相对的简单,首先调用initClassLoaders()初始化类加载器,使得tomcat可以加载应用程序类,接着设置当前线程的上下文加载器为CatalinaLoader。
private void initClassLoaders() {
try {
commonLoader = createClassLoader("common", null);
if (commonLoader == null) {
// no config file, default to this loader - we might be in a 'single' env.
commonLoader = this.getClass().getClassLoader();
}
//在initClassLoaders()初始化方法中可发现会创建三种类加载器并赋予成员变量,其中catalinaLoader与sharedLoader加载器的父加载器都是commonLoader。
catalinaLoader = createClassLoader("server", commonLoader);
sharedLoader = createClassLoader("shared", commonLoader);
} catch (Throwable t) {
handleThrowable(t);
log.error("Class loader creation threw exception", t);
System.exit(1);
}
}
在initClassLoaders()初始化方法中可发现会创建三种类加载器并赋予成员变量,其中catalinaLoader与sharedLoader加载器的父加载器都是commonLoader。
2.3 Catalina#load()
load()方法主要完成StardardServer及子组件的初始化,下图是该方法的主要序列流程:
public void load() {
if (loaded) {
return;
}
loaded = true;
long t1 = System.nanoTime();
initDirs();
// Before digester - it may be needed
initNaming();
// Create and execute our Digester
Digester digester = createStartDigester();
InputSource inputSource = null;
InputStream inputStream = null;
File file = null;
try {
try {
file = configFile();
//读取serve.xml文件
inputStream = new FileInputStream(file);
inputSource = new InputSource(file.toURI().toURL().toString());
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("catalina.configFail", file), e);
}
}
try {
inputSource.setByteStream(inputStream);
digester.push(this);
//解析serve.xml文件
digester.parse(inputSource);
} catch (SAXParseException spe) {
log.warn("Catalina.start using " + getConfigFile() + ": " +
spe.getMessage());
return;
} catch (Exception e) {
log.warn("Catalina.start using " + getConfigFile() + ": " , e);
return;
}
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// Ignore
}
}
}
getServer().setCatalina(this);
getServer().setCatalinaHome(Bootstrap.getCatalinaHomeFile());
getServer().setCatalinaBase(Bootstrap.getCatalinaBaseFile());
// Stream redirection
initStreams();
// Start the new server
try {
getServer().init();
} catch (LifecycleException e) {
if (Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE")) {
throw new java.lang.Error(e);
} else {
log.error("Catalina.start", e);
}
}
}
2.4 standerServer#initInternal()
在执行LifecycleBase#initInternal()抽象方法时,将由standerServer#initInternal()的方法完成初始化。
@Override
protected void initInternal() throws LifecycleException {
//1、调用LifecycleMBeanBase#initInternal方法
super.initInternal();
//2、注册全局字符串缓存,如果有多个Server则也会注册多个字符串缓存对象
onameStringCache = register(new StringCache(), "type=StringCache");
//3、注册MBeanFactory
MBeanFactory factory = new MBeanFactory();
factory.setContainer(this);
onameMBeanFactory = register(factory, "type=MBeanFactory");
//4、注册全局命名资源并初始化,该全局命名资源是配置在server.xml中
globalNamingResources.init();
//5、使用catalina的父加载器校验系统JAR包是否包含MANIFEST文件等,此处忽略部分代码
//6、初始化自定义的service
for (Service service : services) {
service.init();
}
}
而service组件的init()方法主要做了以下事情:
- 初始化StandardEngine容器。
- 创建指定或默认的Realm。
- 初始化绑定的StandardThreadExecutor线程执行器。
- 初始化绑定的MapperListener监听器。
- 更新mapper监听器的生命周期状态为LifecycleState.STARTING。
- 查找并绑定默认的虚拟主机Host。
- 绑定该监听器到Engine及子容器。
- 注册Engine容器到绑定的虚拟主机Host以及上下文Context、Wrapper。
- 初始化绑定的所有connector连接器。
/**
* Invoke a pre-startup initialization. This is used to allow connectors to bind to restricted ports under Unix
* operating environments.
*/
@Override
protected void initInternal() throws LifecycleException {
super.initInternal();
if (engine != null) {
engine.init();
}
// Initialize any Executors
for (Executor executor : findExecutors()) {
if (executor instanceof JmxEnabled) {
((JmxEnabled) executor).setDomain(getDomain());
}
executor.init();
}
// Initialize mapper listener
mapperListener.init();
// Initialize our defined Connectors
synchronized (connectorsLock) {
//初始化所有关联的connector连接器
for (Connector connector : connectors) {
try {
connector.init();
} catch (Exception e) {
String message = sm.getString("standardService.connector.initFailed", connector);
log.error(message, e);
if (Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE")) {
throw new LifecycleException(message);
}
}
}
}
}
在整个service初始化过程中,connector连接器的初始化尤其重要,以下为connector连接器初始化代码:
- 创建、初始化Coyote适配器,并绑定APR或HTTP1.1协议处理器。
- 检查parseBodyMethodsSet是否有值,若没则设置为POST方法。
- 判断APR的native库是否必须且协议处理器实例是否已创建,若都不满足则抛出异常。
- 判断APR本地库是否必须且APR协议处理器是否可用,若都不满足则抛出异常。
- 判断APR协议处理器是否可用且Apr协议处理器是否使用OpenSSL且协议处理器是否AbstractHttp11JsseProtocol类型。
- 判断是否启用SSL且其类名为空,若满足则使用OpenSSLImplementation类名(OpenSSL与JSSE的配置兼容)。
- 否则执行步骤6
- 初始化协议处理器(该协议处理器实例时在server.xml解析时创建的)。
@Override
protected void initInternal() throws LifecycleException {
super.initInternal();
// 1、创建并初始化Coyote适配器,并关联至协议处理器中
adapter = new CoyoteAdapter(this);
protocolHandler.setAdapter(adapter);
//2、确认parseBodyMethodsSet有默认值,parsetBodyMethods默认值为POST方法
if (null == parseBodyMethodsSet) {
setParseBodyMethods(getParseBodyMethods());
}
//3、判断是否需要APR本地库 AND 判断Apr协议处理器实例是否被创建,若是Apr本地路且没创建Apr协议处理器则抛出异常
if (protocolHandler.isAprRequired() && !AprLifecycleListener.isInstanceCreated()) {
throw new LifecycleException(sm.getString("coyoteConnector.protocolHandlerNoAprListener",
getProtocolHandlerClassName()));
}
//4、判断是否需要APR/native库 AND Apr协议处理器是否可用,若是需要Apr本地库且没有Apr协议处理器可用则抛出异常
if (protocolHandler.isAprRequired() && !AprLifecycleListener.isAprAvailable()) {
throw new LifecycleException(sm.getString("coyoteConnector.protocolHandlerNoAprLibrary",
getProtocolHandlerClassName()));
}
//5、判断Apr协议处理器是否可用 AND Apr协议处理器是否使用OpenSSL AND 协议处理器是否AbstractHttp11JsseProtocol类型
if (AprLifecycleListener.isAprAvailable() && AprLifecycleListener.getUseOpenSSL() &&
protocolHandler instanceof AbstractHttp11JsseProtocol) {
AbstractHttp11JsseProtocol<?> jsseProtocolHandler =
(AbstractHttp11JsseProtocol<?>) protocolHandler;
//5.1、如果SSL启用了且SSL实现类名为空,则使用OpenSSLImplementation类名
if (jsseProtocolHandler.isSSLEnabled() &&
jsseProtocolHandler.getSslImplementationName() == null) {
// 如果APR可用,可以使用OpenSSL,因为OpenSSL与JSSE的配置兼容。
jsseProtocolHandler.setSslImplementationName(OpenSSLImplementation.class.getName());
}
}
try {
//协议处理器执行初始化(在server.xml解析时创建protocolHandler)
protocolHandler.init();
} catch (Exception e) {
throw new LifecycleException(
sm.getString("coyoteConnector.protocolHandlerInitializationFailed"), e);
}
}
2.5Catalina#start()
文章来源:https://www.toymoban.com/news/detail-563873.html
public void start(){
//判断server是否为空,为空则执行初始化
if (getServer() == null) {
load();
}
//若server为空,说明未能正确配置server.xml或者server初始化时异常
if (getServer() == null) {
log.fatal("Cannot start server. Server instance is not configured.");
return;
}
//此处省略部分代码
try {
getServer().start();
} catch (LifecycleException e) {
log.fatal(sm.getString("catalina.serverStartFail"), e);
try {
getServer().destroy();
} catch (LifecycleException e1) {
log.debug("destroy() failed for failed Server ", e1);
}
return;
}
//此处忽略部分代码
// 注册关闭钩子
if (useShutdownHook) {
if (shutdownHook == null) {
shutdownHook = new CatalinaShutdownHook();
}
Runtime.getRuntime().addShutdownHook(shutdownHook);
// If JULI is being used, disable JULI's shutdown hook since
// shutdown hooks run in parallel and log messages may be lost
// if JULI's hook completes before the CatalinaShutdownHook()
LogManager logManager = LogManager.getLogManager();
if (logManager instanceof ClassLoaderLogManager) {
((ClassLoaderLogManager) logManager).setUseShutdownHook(
false);
}
}
if (await) {
//创建ServerSocker并监听端口
await();
//停止standardServer实例
stop();
}
}
上面主要是启动tomcat中各个组件、容器,这时候还需要创建等待关闭tomcat的ServerSocket以及它监听的端口,而await()方法的作用正是创建ServerSocket并等待接收关闭命令的。文章来源地址https://www.toymoban.com/news/detail-563873.html
@Override
public void await(){
//该处省略部分代码...
//创建ServerSocker并监听端口
try {
awaitSocket = new ServerSocket(port, 1,InetAddress.getByName(address));
} catch (IOException e) {
log.error("StandardServer.await: create[" + address+ ":" + port+ "]: ", e);
return;
}
try {
awaitThread = Thread.currentThread();
// 循环等待有效连接和命令
while (!stopAwait) {
ServerSocket serverSocket = awaitSocket;
if (serverSocket == null) {
break;
}
// 等待下一个连接
Socket socket = null;
StringBuilder command = new StringBuilder();
try {
InputStream stream;
long acceptStartTime = System.currentTimeMillis();
try {
socket = serverSocket.accept();
socket.setSoTimeout(10 * 1000); // Ten seconds
stream = socket.getInputStream();
} catch (SocketTimeoutException ste) {
log.warn(sm.getString("standardServer.accept.timeout",
Long.valueOf(System.currentTimeMillis() - acceptStartTime)), ste);
continue;
} catch (AccessControlException ace) {
log.warn(sm.getString("standardServer.accept.security"), ace);
continue;
} catch (IOException e) {
if (stopAwait) {
break;
}
log.error(sm.getString("standardServer.accept.error"), e);
break;
}
// 从套接字读取一组字符
int expected = 1024; // Cut off to avoid DoS attack
while (expected < shutdown.length()) {
if (random == null)
random = new Random();
expected += (random.nextInt() % 1024);
}
while (expected > 0) {
int ch = -1;
try {
ch = stream.read();
} catch (IOException e) {
log.warn(sm.getString("standardServer.accept.readError"), e);
ch = -1;
}
// 若字符是控制字符或者EOF(-1)则终止循环
if (ch < 32 || ch == 127) {
break;
}
command.append((char) ch);
expected--;
}
} finally {
// 完成操作后关闭socket
try {
if (socket != null) {
socket.close();
}
} catch (IOException e) {
// Ignore
}
}
// 判断命令内容是否SHUTDOWN
boolean match = command.toString().equals(shutdown);
if (match) {
log.info(sm.getString("standardServer.shutdownViaPort"));
break;
} else
log.warn(sm.getString("standardServer.invalidShutdownCommand", command.toString()));
}
} finally {
ServerSocket serverSocket = awaitSocket;
awaitThread = null;
awaitSocket = null;
// 关闭ServerScoket
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
}
}
}
}
结束
到了这里,关于Tomcat 8.5 源码分析的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!