Tomcat的批处理

Posted wansw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tomcat的批处理相关的知识,希望对你有一定的参考价值。

Tomcat的启动和关闭

来源

本文摘抄自  <Tomcat内核设计剖析> 一书。

Tomcat的批处理

​ Tomcat的启动、关闭批处理脚本在/bin目录下。

startup.bat

第一部分
setlocal

set "CURRENT_DIR=%cd%"
if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%CURRENT_DIR%"
if exist "%CATALINA_HOME%\\bin\\catalina.bat" goto okHome
cd ..
set "CATALINA_HOME=%cd%"
cd "%CURRENT_DIR%"

:gotHome
if exist "%CATALINA_HOME%\\bin\\catalina.bat" goto okHome
goto end

:okHome
set "EXECUTABLE=%CATALINA_HOME%\\bin\\catalina.bat"
if exist "%EXECUTABLE%" goto okExec
goto end
:okExec

说明:

1、setlocal : 表示之后所有对环境变量的改变只限于本批处理文件。需要还原设置,可执行 endlocal,未显示执行,会在批处理的最后自动隐试执行endlocal命令。

第二部分

确定了 catalina_home 的值,就已经确定了 catalina.bat。在启动时,会接收参数:

set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1 // 将参数组成一行
shift // 参数前移一位
goto setArgs
:doneSetArgs

call "%EXECUTABLE%" start %CMD_LINE_ARGS%

​ //用收集到的 参数作为 调用 catalina.bat 的参数, 传递 start 参数

:end

shutdown.bat

setlocal

set "CURRENT_DIR=%cd%"
if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%CURRENT_DIR%"
if exist "%CATALINA_HOME%\\bin\\catalina.bat" goto okHome
cd ..
set "CATALINA_HOME=%cd%"
cd "%CURRENT_DIR%"
:gotHome
if exist "%CATALINA_HOME%\\bin\\catalina.bat" goto okHome
goto end
:okHome

set "EXECUTABLE=%CATALINA_HOME%\\bin\\catalina.bat"

if exist "%EXECUTABLE%" goto okExec
goto end
:okExec

set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs

call "%EXECUTABLE%" stop %CMD_LINE_ARGS% // 类似,传递参数 stop 给 catalina.bat

:end

catalina.bat

是Tomcat服务器启动和关闭的核心脚本,最终目的是 组合出一个最终的执行命令。

第一部分

主要目的是按住 Ctrl+C 组合键终止程序时自动确认。

if not ""%1"" == ""run"" goto mainEntry
if "%TEMP%" == "" goto mainEntry
if exist "%TEMP%%~nx0.run" goto mainEntry
if not exist "%TEMP%%~nx0.run" goto mainEntry
call "%~f0" %* <"%TEMP%%~nx0.Y"
set RETVAL=%ERRORLEVEL%
del /Q "%TEMP%%~nx0.Y" >NUL 2>&1
exit /B %RETVAL%
:mainEntry
del /Q "%TEMP%%~nx0.run" >NUL 2>&1

第二部分

​ 用于设置CATALINA_HOME、CATALINA_BASE 两个变量。(再一次设置catalina_home 是为了直接运行这个bat启动)。

set "CURRENT_DIR=%cd%"
if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%CURRENT_DIR%"
if exist "%CATALINA_HOME%\\bin\\catalina.bat" goto okHome
cd ..
set "CATALINA_HOME=%cd%"
cd "%CURRENT_DIR%"
:gotHome

if exist "%CATALINA_HOME%\\bin\\catalina.bat" goto okHome
goto end
:okHome

if not "%CATALINA_BASE%" == "" goto gotBase
set "CATALINA_BASE=%CATALINA_HOME%"
:gotBase

第三部分

​ 用于尝试寻找setenv.bat和setclasspath.bat并执行,然后将Tomcat的启动包bootstrap.jar和日志包tomcat-juli.jar添加到 CLASSPATH 环境变量中。

set CLASSPATH=

if not exist "%CATALINA_BASE%\\bin\\setenv.bat" goto checkSetenvHome
call "%CATALINA_BASE%\\bin\\setenv.bat"
goto setenvDone
:checkSetenvHome
if exist "%CATALINA_HOME%\\bin\\setenv.bat" call "%CATALINA_HOME%\\bin\\setenv.bat"
:setenvDone

if exist "%CATALINA_HOME%\\bin\\setclasspath.bat" goto okSetclasspath
goto end
:okSetclasspath
call "%CATALINA_HOME%\\bin\\setclasspath.bat" %1
if errorlevel 1 goto end

if "%CLASSPATH%" == "" goto emptyClasspath
set "CLASSPATH=%CLASSPATH%;"
:emptyClasspath
set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\\bin\\bootstrap.jar"

if not "%CATALINA_TMPDIR%" == "" goto gotTmpdir
set "CATALINA_TMPDIR=%CATALINA_BASE%\\temp"
:gotTmpdir

if not exist "%CATALINA_BASE%\\bin\\tomcat-juli.jar" goto juliClasspathHome
set "CLASSPATH=%CLASSPATH%;%CATALINA_BASE%\\bin\\tomcat-juli.jar"
goto juliClasspathDone
:juliClasspathHome
set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\\bin\\tomcat-juli.jar"
:juliClasspathDone

第四部分

​ 对日志配置的设置。

if not "%LOGGING_CONFIG%" == "" goto noJuliConfig
set LOGGING_CONFIG=-Dnop
if not exist "%CATALINA_BASE%\\conf\\logging.properties" goto noJuliConfig
set LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\\conf\\logging.properties"
:noJuliConfig

if not "%LOGGING_MANAGER%" == "" goto noJuliManager
set LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
:noJuliManager

第五部分

执行命令前的一些参数的初始化。

set _EXECJAVA=%_RUNJAVA%
set MAINCLASS=org.apache.catalina.startup.Bootstrap
set ACTION=start
set SECURITY_POLICY_FILE=
set DEBUG_OPTS=
set JPDA=

if not ""%1"" == ""jpda"" goto noJpda
set JPDA=jpda
if not "%JPDA_TRANSPORT%" == "" goto gotJpdaTransport
set JPDA_TRANSPORT=dt_socket
:gotJpdaTransport
if not "%JPDA_ADDRESS%" == "" goto gotJpdaAddress
set JPDA_ADDRESS=localhost:8000
:gotJpdaAddress
if not "%JPDA_SUSPEND%" == "" goto gotJpdaSuspend
set JPDA_SUSPEND=n
:gotJpdaSuspend
if not "%JPDA_OPTS%" == "" goto gotJpdaOpts
set JPDA_OPTS=-agentlib:jdwp=transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%
:gotJpdaOpts
shift
:noJpda

第六部分

根据不同的参数跳转到不同的位置执行不同的命令,为下一步的执行命令做准备。

if ""%1"" == ""debug"" goto doDebug
if ""%1"" == ""run"" goto doRun
if ""%1"" == ""start"" goto doStart
if ""%1"" == ""stop"" goto doStop
if ""%1"" == ""configtest"" goto doConfigTest
if ""%1"" == ""version"" goto doVersion

:doDebug
shift
set _EXECJAVA=%_RUNJDB%
set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%....\\java"
if not ""%1"" == ""-security"" goto execCmd
shift
set "SECURITY_POLICY_FILE=%CATALINA_BASE%\\conf\\catalina.policy"
goto execCmd

:doRun
shift
if not ""%1"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set "SECURITY_POLICY_FILE=%CATALINA_BASE%\\conf\\catalina.policy"
goto execCmd

:doStart
shift
if "%TITLE%" == "" set TITLE=Tomcat
set _EXECJAVA=start "%TITLE%" %_RUNJAVA%
if not ""%1"" == ""-security"" goto execCmd
shift
set "SECURITY_POLICY_FILE=%CATALINA_BASE%\\conf\\catalina.policy"
goto execCmd

:doStop
shift
set ACTION=stop
set CATALINA_OPTS=
goto execCmd

:doConfigTest
shift
set ACTION=configtest
set CATALINA_OPTS=
goto execCmd

:doVersion
%_EXECJAVA% -classpath "%CATALINA_HOME%\\lib\\catalina.jar" org.apache.catalina.util.ServerInfo
goto end

第七部分

将前面所有脚本运行后组成一个最终的命令并开始执行。

:execCmd
rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=

:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs

:doneSetArgs
if not "%JPDA%" == "" goto doJpda
if not "%SECURITY_POLICY_FILE%" == "" goto doSecurity
%EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -D%ENDORSED_PROP%="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end

_
:doSecurity
%_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -D%ENDORSED_PROP%="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end

:doJpda
if not "%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda
%EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %JPDA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -D%ENDORSED_PROP%="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end

_
:doSecurityJpda
%_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %JPDA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -D%ENDORSED_PROP%="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end

:end

以上是关于Tomcat的批处理的主要内容,如果未能解决你的问题,请参考以下文章

mysql jdbc源码分析片段 和 Tomcat's JDBC Pool

处理屏幕旋转上的片段重复(带有示例代码)

在 Python 多处理进程中运行较慢的 OpenCV 代码片段

你如何在 python 中处理 graphql 查询和片段?

是否有在单个活动中处理多个片段的 Android 设计模式?

译文:18个实用的JavaScript代码片段,助你快速处理日常编程任务