Tomcat学习(一):启动过程(1)

本文详细介绍了Tomcat的启动过程,从启动脚本startup.sh开始,探讨Bootstrap类加载器结构,接着解析Catalina的启动,包括server.xml的加载和解析,StandardServer的初始化,以及启动后处理。讲解了Tomcat类加载器的层次,如Common, Server, Shared和Web ClassLoader,以及双亲委派机制。" 106882025,5768734,C++ async与future实现线程返回,"['C++', '并发编程', '多线程']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1. 启动入口

2.Bootstrap启动过程

2.1 Tomcat类加载器结构

2.2 Bootstrap的启动

3.Catalina的启动之Server的加载

3.1 server.xml的加载和解析

3.2 StandardServer的初始化

3.3 启动的后处理


以Tomcat 9.0.30为例,参考资料:《Tomcat架构解析》

1. 启动入口

一般使用Tomcat,都是运行 $CATALINA_BASE/bin/startup.sh(CATALINA_BASE指Tomcat根目录),所以先来看该脚本的主要内容:

# resolve links - $0 may be a softlink
PRG="$0"

while [ -h "$PRG" ] ; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done

PRGDIR=`dirname "$PRG"`
EXECUTABLE=catalina.sh

# Check that target executable exists
if $os400; then
  # -x will Only work on the os400 if the files are:
  # 1. owned by the user
  # 2. owned by the PRIMARY group of the user
  # this will not work if the user belongs in secondary groups
  eval
else
  if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]; then
    echo "Cannot find $PRGDIR/$EXECUTABLE"
    echo "The file is absent or does not have execute permission"
    echo "This file is needed to run this program"
    exit 1
  fi
fi

其实就是根据命令,解析出startup.sh所在位置,然后拼出catalina.sh的路径,并且执行该脚本。catalina.sh很长,所以不贴内容了,该脚本的主要功能就是配置环境变量,然后拼接Java命令,大概长这样:

eval exec "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER "$JAVA_OPTS" "$CATALINA_OPTS" \
      -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \
      -classpath "\"$CLASSPATH\"" \
      -Dcatalina.base="\"$CATALINA_BASE\"" \
      -Dcatalina.home="\"$CATALINA_HOME\"" \
      -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
      org.apache.catalina.startup.Bootstrap "$@" start

从这里可以看到,Tomcat的启动类是org.apache.catalina.startup.Bootstrap,同时传入了start指令。

2.Bootstrap启动过程

进入Bootstrap#main方法,首先会尝试同步初始化Bootstrap(去掉了源码中日志输出内容):

public void init() throws Exception {

        initClassLoaders();

        Thread.currentThread().setContextClassLoader(catalinaLoader);

        SecurityClassLoad.securityClassLoad(catalinaLoader);

        // Load our startup class and call its process() method
        Class<?> startupClass = catalinaLoader.loadClass("org.apache.catalina.startup.Catalina");
        Object startupInstance = startupClass.getConstructor().newInstance();

        // Set the shared extensions class loader
        String methodName = "setParentClassLoader";
        Class<?> paramTypes[] = new Class[1];
        paramTypes[0] = Class.forName("java.lang.ClassLoader");
        Object paramValues[] = new Object[1];
        paramValues[0] = sharedLoader;
        Method 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值