文档章节

Tommcat源码学习(三)--Tomcat_7.0.70停止服务过程分析

火龙战士
 火龙战士
发布于 2016/07/05 11:30
字数 1832
阅读 61
收藏 3

Tomcat关闭命令(Linux下,大部分生产环境都是部署在Linux系统下):

sh shutdown.sh 

执行这个命令之后,tomcat会为我们做了哪些操作呢?下面就来简单分析下。

shutdown.sh代码清单如下:

# Better OS/400 detection: see Bugzilla 31132
os400=false
case "`uname`" in
OS400*) os400=true;;
esac

# 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

exec "$PRGDIR"/"$EXECUTABLE" stop "$@"

从上面的代码可以看出来,这里和启动文件是一样的,也有主要的两个变量:

 PRGDIR:当前shell脚本所在的路径;
 EXECUTABLE:脚本catalina.sh

从最后一行代码可以知道,执行catalina.sh,并传递参数:stop

catalina.sh 与之相关的代码清单如下:

elif [ "$1" = "stop" ] ; then

  shift

  SLEEP=5
  if [ ! -z "$1" ]; then
    echo $1 | grep "[^0-9]" >/dev/null 2>&1
    if [ $? -gt 0 ]; then
      SLEEP=$1
      shift
    fi
  fi

  FORCE=0
  if [ "$1" = "-force" ]; then
    shift
    FORCE=1
  fi

  if [ ! -z "$CATALINA_PID" ]; then
    if [ -f "$CATALINA_PID" ]; then
      if [ -s "$CATALINA_PID" ]; then
        kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1
        if [ $? -gt 0 ]; then
          echo "PID file found but no matching process was found. Stop aborted."
          exit 1
        fi
      else
        echo "PID file is empty and has been ignored."
      fi
    else
      echo "\$CATALINA_PID was set but the specified file does not exist. Is Tomcat running? Stop aborted."
      exit 1
    fi
  fi

  eval "\"$_RUNJAVA\"" $LOGGING_MANAGER $JAVA_OPTS \
    -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
    -Dcatalina.base="\"$CATALINA_BASE\"" \
    -Dcatalina.home="\"$CATALINA_HOME\"" \
    -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
    org.apache.catalina.startup.Bootstrap "$@" stop

最终使用java命令执行了org.apache.catalina.startup.Bootstrap类中的main方法,参数是stop。

public static void main(String args[]) {

    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();
        } 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);
    }

}

当传递参数stop的时候,command等于stop,此时main方法的执行步骤如下:

  • 初始化Bootstrap(启动的时候已经介绍过就不在介绍)

  • 停止服务

    通过调用Bootstrap的stopServer方法停止Tomcat,其实质是用反射调用catalinaDaemon(类型是Catalina)的stopServer方法。

      /**
       * Stop the standalone server.
       */
      public void stopServer(String[] arguments)
          throws Exception {
    
          Object param[];
          Class<?> paramTypes[];
          if (arguments==null || arguments.length==0) {
              paramTypes = null;
              param = null;
          } else {
              paramTypes = new Class[1];
              paramTypes[0] = arguments.getClass();
              param = new Object[1];
              param[0] = arguments;
          }
          Method method =
              catalinaDaemon.getClass().getMethod("stopServer", paramTypes);//反射调用catalinaDaemon(类型是Catalina)的stopServer方法
          method.invoke(catalinaDaemon, param);
    
      }
    

    Catalina的stopServer方法的执行步骤如下:

    代码清单:

      public void stopServer() {
          stopServer(null);
      }
    
      public void stopServer(String[] arguments) {
    
          if (arguments != null) {
              arguments(arguments);
          }
    
          Server s = getServer();
          if( s == null ) {//服务不存在
              // Create and execute our Digester
              Digester digester = createStopDigester();//Digester解析server.xml文件,以构造出Server容器
              File file = configFile();
              FileInputStream fis = null;
              try {
                  InputSource is =
                      new InputSource(file.toURI().toURL().toString());
                  fis = new FileInputStream(file);
                  is.setByteStream(fis);
                  digester.push(this);
                  digester.parse(is);
              } catch (Exception e) {
                  log.error("Catalina.stop: ", e);
                  System.exit(1);
              } finally {
                  if (fis != null) {
                      try {
                          fis.close();
                      } catch (IOException e) {
                          // Ignore
                      }
                  }
              }
          } else {
              // Server object already present. Must be running as a service
              try {
                  s.stop();
              } catch (LifecycleException e) {
                  log.error("Catalina.stop: ", e);
              }
              return;
          }
    
          // Stop the existing server
          s = getServer();
          if (s.getPort()>0) {
              Socket socket = null;
              OutputStream stream = null;
              try {
                  socket = new Socket(s.getAddress(), s.getPort());//创建Socket对象连接启动Tomcat时创建的ServerSocket
                  stream = socket.getOutputStream();
                  String shutdown = s.getShutdown();
                  for (int i = 0; i < shutdown.length(); i++) {
                      stream.write(shutdown.charAt(i));//发送shutdown命令
                  }
                  stream.flush();
              } catch (ConnectException ce) {
                  log.error(sm.getString("catalina.stopServer.connectException",
                                         s.getAddress(),
                                         String.valueOf(s.getPort())));
                  log.error("Catalina.stop: ", ce);
                  System.exit(1);
              } catch (IOException e) {
                  log.error("Catalina.stop: ", e);
                  System.exit(1);
              } finally {
                  if (stream != null) {
                      try {
                          stream.close();
                      } catch (IOException e) {
                          // Ignore
                      }
                  }
                  if (socket != null) {
                      try {
                          socket.close();
                      } catch (IOException e) {
                          // Ignore
                      }
                  }
              }
          } else {
              log.error(sm.getString("catalina.stopServer"));
              System.exit(1);
          }
      }
    

创建Digester解析server.xml文件(此处只解析标签),以构造出Server容器(此时Server容器的子容器没有被实例化); 从实例化的Server容器获取Server的socket监听端口和地址,然后创建Socket对象连接启动Tomcat时创建的ServerSocket,最后向ServerSocket发送SHUTDOWN命令。根据

		@Override
	    public void await() {
	        // Negative values - don't wait on port - tomcat is embedded or we just don't like ports
	        if( port == -2 ) {
	            // undocumented yet - for embedding apps that are around, alive.
	            return;
	        }
	        if( port==-1 ) {
	            try {
	                awaitThread = Thread.currentThread();
	                while(!stopAwait) {
	                    try {
	                        Thread.sleep( 10000 );
	                    } catch( InterruptedException ex ) {
	                        // continue and check the flag
	                    }
	                }
	            } finally {
	                awaitThread = null;
	            }
	            return;
	        }
	
	        // Set up a server socket to wait on
	        try {
	            awaitSocket = new ServerSocket(port, 1,
	                    InetAddress.getByName(address));//创建socket连接的服务端对象ServerSocket
	        } catch (IOException e) {
	            log.error("StandardServer.await: create[" + address
	                               + ":" + port
	                               + "]: ", e);
	            return;
	        }
	
	        try {
	            awaitThread = Thread.currentThread();
	
	            // Loop waiting for a connection and a valid command
	            while (!stopAwait) {
	                ServerSocket serverSocket = awaitSocket;
	                if (serverSocket == null) {
	                    break;
	                }
	    
	                // Wait for the next connection
	                Socket socket = null;
	                StringBuilder command = new StringBuilder();//创建一个对象循环接收socket中的字符
	                try {
	                    InputStream stream;
	                    long acceptStartTime = System.currentTimeMillis();
	                    try {
	                        socket = serverSocket.accept();
	                        socket.setSoTimeout(10 * 1000);  // Ten seconds
	                        stream = socket.getInputStream();
	                    } catch (SocketTimeoutException ste) {
	                        // This should never happen but bug 56684 suggests that
	                        // it does.
	                        log.warn(sm.getString("standardServer.accept.timeout",
	                                Long.valueOf(System.currentTimeMillis() - acceptStartTime)), ste);
	                        continue;
	                    } catch (AccessControlException ace) {
	                        log.warn("StandardServer.accept security exception: "
	                                + ace.getMessage(), ace);
	                        continue;
	                    } catch (IOException e) {
	                        if (stopAwait) {
	                            // Wait was aborted with socket.close()
	                            break;
	                        }
	                        log.error("StandardServer.await: accept: ", e);
	                        break;
	                    }
	
	                    // Read a set of characters from the socket
	                    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("StandardServer.await: read: ", e);
	                            ch = -1;
	                        }
	                        if (ch < 32)  // Control character or EOF terminates loop
	                            break;
	                        command.append((char) ch);
	                        expected--;
	                    }
	                } finally {
	                    // Close the socket now that we are done with it
	                    try {
	                        if (socket != null) {
	                            socket.close();
	                        }
	                    } catch (IOException e) {
	                        // Ignore
	                    }
	                }
	
	                // Match against our command string
					
	                boolean match = command.toString().equals(shutdown);
	                if (match) { //如果接收到的命令与SHUTDOWN匹配(由于使用了equals,所以shutdown命令必须是大写的),那么退出循环等待
	                    log.info(sm.getString("standardServer.shutdownViaPort"));
	                    break;
	                } else
	                    log.warn("StandardServer.await: Invalid command '"
	                            + command.toString() + "' received");
	            }
	        } finally {
	            ServerSocket serverSocket = awaitSocket;
	            awaitThread = null;
	            awaitSocket = null;
	
	            // Close the server socket and return
	            if (serverSocket != null) {
	                try {
	                    serverSocket.close();
	                } catch (IOException e) {
	                    // Ignore
	                }
	            }
	        }
	    }

内容,ServerSocket循环等待接收到SHUTDOWN命令后,最终调用stop方法停止Tomcat。

最后,我们看看Catalina的stop方法的实现,其执行步骤如下:

  1. 将启动过程中添加的关闭钩子移除。Tomcat启动过程辛辛苦苦添加的关闭钩子为什么又要去掉呢?因为关闭钩子是为了在JVM异常退出后,进行资源的回收工作。主动停止Tomcat时调用的stop方法里已经包含了资源回收的内容,所以不再需要这个钩子了。

  2. 停止Server容器。有关容器的停止内容,请阅读后续文章。

  3. 代码清单:

     		/**
     	     * Stop an existing server instance.****
     	     */
     	    public void stop() {
    
     	        try {
     	            // Remove the ShutdownHook first so that server.stop()
     	            // doesn't get invoked twice
     	            if (useShutdownHook) {
     	                Runtime.getRuntime().removeShutdownHook(shutdownHook);//将启动过程中添加的关闭钩子移除
    
     	                // If JULI is being used, re-enable JULI's shutdown to ensure
     	                // log messages are not lost
     	                LogManager logManager = LogManager.getLogManager();
     	                if (logManager instanceof ClassLoaderLogManager) {
     	                    ((ClassLoaderLogManager) logManager).setUseShutdownHook(
     	                            true);
     	                }
     	            }
     	        } catch (Throwable t) {
     	            ExceptionUtils.handleThrowable(t);
     	            // This will fail on JDK 1.2. Ignoring, as Tomcat can run
     	            // fine without the shutdown hook.
     	        }
    
     	        // Shut down the server
     	        try {
     	            Server s = getServer();
     	            LifecycleState state = s.getState();
     	            if (LifecycleState.STOPPING_PREP.compareTo(state) <= 0
     	                    && LifecycleState.DESTROYED.compareTo(state) >= 0) {
     	                // Nothing to do. stop() was already called
     	            } else {
     	                s.stop();//停止Server容器。
     	                s.destroy();
     	            }
     	        } catch (LifecycleException e) {
     	            log.error("Catalina.stop", e);
     	        }
    
     	    }
    

总结:

通过对Tomcat源码的分析我们了解到Tomcat的启动和停止都离不开org.apache.catalina.startup.Bootstrap。 当停止Tomcat时,已经启动的Tomcat作为socket服务端,停止脚本启动的Bootstrap进程作为socket客户端向服务端发送shutdown命令 ,两个进程通过共享server.xml里Server标签的端口以及地址信息打通了socket的通信。

© 著作权归作者所有

火龙战士

火龙战士

粉丝 119
博文 136
码字总数 100857
作品 0
北京
后端工程师
私信 提问
centos7设置tomcat7为系统服务的方法

A.在/usr/lib/systemd/system/目录下新建文件tomcat7.service,内容如下: [Unit] Description=Tomcat7 After=syslog.target network.target remote-fs.target nss-lookup.target [Service]......

有功夫
2018/05/30
0
0
【tomcat】tomcat部署项目实验

一、实验环境 EXSI6.0虚拟机 系统:Centos7 yum源:mirror.163.com.repo 二、快速安装包含网页控制的tomcat 1、yum安装tomcat 2、yum安装tomcat网页访问必要服务 3、开放tomcat:8080端口,开...

bestlope
2018/06/29
0
0
Tomcat源码学习(二)--Tomcat_7.0.70 启动分析

项目build成功后,刷新整个项目,会发现多出一个output目录: 为了让应用跑起来,可以检查一下outputbuildconf下是否已经有配置文件,这些文件实际是从项目根路径conf目录下拷贝过来的。 找到...

火龙战士
2016/07/01
162
0
Java Web开发入门 - 第3章 Tomcat

Tomcat安装与运行 Web服务器完成底层的网络处理,包括HTTP协议报文格式的编解码、管理具体web请求处理线程等操作。 Tomcat目前最流行最常见的基于Java的web应用服务器软件。 Tomcat Apache ...

抢小孩糖吃
2016/08/19
175
0
Tomcat源码学习(六)--Tomcat_7.0.70 生命周期管理

想必大家都知道,从server.xml文件解析出来的各个对象都是容器,比如:Server、Service、Connector等。这些容器都具有新建、初始化完成、启动、停止、失败、销毁等状态。Tomcat的实现机制是通...

火龙战士
2016/07/18
65
0

没有更多内容

加载失败,请刷新页面

加载更多

OSChina 周六乱弹 —— 早上儿子问我他是怎么来的

Osc乱弹歌单(2019)请戳(这里) 【今日歌曲】 @凉小生 :#今日歌曲推荐# 少点戾气,愿你和这个世界温柔以待。中岛美嘉的单曲《僕が死のうと思ったのは (曾经我也想过一了百了)》 《僕が死の...

小小编辑
今天
815
11
Excption与Error包结构,OOM 你遇到过哪些情况,SOF 你遇到过哪些情况

Throwable 是 Java 中所有错误与异常的超类,Throwable 包含两个子类,Error 与 Exception 。用于指示发生了异常情况。 Java 抛出的 Throwable 可以分成三种类型。 被检查异常(checked Exc...

Garphy
今天
15
0
计算机实现原理专题--二进制减法器(二)

在计算机实现原理专题--二进制减法器(一)中说明了基本原理,现准备说明如何来实现。 首先第一步255-b运算相当于对b进行按位取反,因此可将8个非门组成如下图的形式: 由于每次做减法时,我...

FAT_mt
昨天
6
0
好程序员大数据学习路线分享函数+map映射+元祖

好程序员大数据学习路线分享函数+map映射+元祖,大数据各个平台上的语言实现 hadoop 由java实现,2003年至今,三大块:数据处理,数据存储,数据计算 存储: hbase --> 数据成表 处理: hive --> 数...

好程序员官方
昨天
7
0
tabel 中含有复选框的列 数据理解

1、el-ui中实现某一列为复选框 实现多选非常简单: 手动添加一个el-table-column,设type属性为selction即可; 2、@selection-change事件:选项发生勾选状态变化时触发该事件 <el-table @sel...

everthing
昨天
6
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部