Windows如何設(shè)置定時(shí)重啟Tomcat
項(xiàng)目場景:
系統(tǒng):Windows 7
Tomcat:apache-tomcat-8.0.5
JDK:1.8
問題描述
最近項(xiàng)目的Tomcat隔一段時(shí)間就假死,最后想到的解決方式就是:每天凌晨1點(diǎn)重啟tomact。
解決方案:
使用Windows系統(tǒng)的計(jì)劃任務(wù)程序,可以在這里設(shè)置定時(shí)執(zhí)行的.bat批處理文件(將你要定時(shí)執(zhí)行的cmd命令放在這里),這樣就可以實(shí)現(xiàn)讓電腦在某個(gè)時(shí)刻做你想讓它干的事。
實(shí)現(xiàn)步驟:
一、創(chuàng)建tomcat重啟的腳本
創(chuàng)建txt文件restart.txt,編輯內(nèi)容,把下面的內(nèi)容復(fù)制進(jìn)去,然后把綴改為.bat,最后文件名為:restart.bat
echo 正在關(guān)閉Tomcat服務(wù),請(qǐng)稍等...... @echo off rem Licensed to the Apache Software Foundation (ASF) under one or more rem contributor license agreements. See the NOTICE file distributed with rem this work for additional information regarding copyright ownership. rem The ASF licenses this file to You under the Apache License, Version 2.0 rem (the "License"); you may not use this file except in compliance with rem the License. You may obtain a copy of the License at rem rem http://www.apache.org/licenses/LICENSE-2.0 rem rem Unless required by applicable law or agreed to in writing, software rem distributed under the License is distributed on an "AS IS" BASIS, rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. rem See the License for the specific language governing permissions and rem limitations under the License. rem --------------------------------------------------------------------------- rem Stop script for the CATALINA Server rem --------------------------------------------------------------------------- setlocal rem Guess CATALINA_HOME if not defined 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 echo The CATALINA_HOME environment variable is not defined correctly echo This environment variable is needed to run this program goto end :okHome set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat" rem Check that target executable exists if exist "%EXECUTABLE%" goto okExec echo Cannot find "%EXECUTABLE%" echo This file is needed to run this program goto end :okExec 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 call "%EXECUTABLE%" stop %CMD_LINE_ARGS% :end echo 關(guān)閉Tomcat服務(wù)完成 ping 127.0.0.1 -n 20 echo ****************************************** echo 正在啟動(dòng)Tomcat服務(wù),請(qǐng)稍等...... @echo off rem Licensed to the Apache Software Foundation (ASF) under one or more rem contributor license agreements. See the NOTICE file distributed with rem this work for additional information regarding copyright ownership. rem The ASF licenses this file to You under the Apache License, Version 2.0 rem (the "License"); you may not use this file except in compliance with rem the License. You may obtain a copy of the License at rem rem http://www.apache.org/licenses/LICENSE-2.0 rem rem Unless required by applicable law or agreed to in writing, software rem distributed under the License is distributed on an "AS IS" BASIS, rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. rem See the License for the specific language governing permissions and rem limitations under the License. rem --------------------------------------------------------------------------- rem Start script for the CATALINA Server rem --------------------------------------------------------------------------- setlocal rem Guess CATALINA_HOME if not defined 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 echo The CATALINA_HOME environment variable is not defined correctly echo This environment variable is needed to run this program goto end :okHome set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat" rem Check that target executable exists if exist "%EXECUTABLE%" goto okExec echo Cannot find "%EXECUTABLE%" echo This file is needed to run this program goto end :okExec 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 call "%EXECUTABLE%" start %CMD_LINE_ARGS% :end echo 啟動(dòng)Tomcat服務(wù)完成
把restart.bat 文件復(fù)制到apache-tomcat-8.0.50\bin目錄下 ,然后可以直接雙擊運(yùn)行,啟動(dòng)會(huì)報(bào)個(gè)錯(cuò),是因?yàn)閠omcat沒啟動(dòng),是正常的。
二、創(chuàng)建任務(wù)計(jì)劃程序
1、開始菜單搜索“任務(wù)計(jì)劃程序”
或者到控制面板里面找到“計(jì)劃任務(wù)”
2、彈出任務(wù)計(jì)劃程序界面,在右側(cè)點(diǎn)擊創(chuàng)建任務(wù)
3、輸入 名稱和描述
4、點(diǎn)擊觸發(fā)器選項(xiàng)卡,點(diǎn)擊左下角新建,彈出新建操作,新建一個(gè)觸發(fā)器,每天凌晨一點(diǎn)觸發(fā)
5、點(diǎn)擊操作選項(xiàng)卡,點(diǎn)擊左下角新建,彈出新建操作,點(diǎn)擊瀏覽。選擇要執(zhí)行的腳本restart.bat ,點(diǎn)擊打開確定。
附加:
這里說下nginx配置集群和tomcat實(shí)現(xiàn)session共享
首先要啟動(dòng)兩個(gè)tomcat,然后端口不一樣,比如8889和8899
1、nginx.conf關(guān)鍵配置
upstream serverlist { #最少連接數(shù),根據(jù)連接數(shù)多少自動(dòng)選擇后端服務(wù)器,連接數(shù)相等時(shí)根據(jù)權(quán)重選擇 least_conn; #每個(gè)請(qǐng)求按訪問ip的hash結(jié)果分配,這樣每個(gè)訪客固定訪問一個(gè)后端服務(wù)器,可以解決session的問題 #ip_hash; #若120秒內(nèi)出現(xiàn)2次錯(cuò)誤,則下個(gè)120秒內(nèi)不再訪問此服務(wù)器,weight權(quán)重越大,請(qǐng)求機(jī)會(huì)越多 server 127.0.0.1:8889 max_fails=2 fail_timeout=120s weight=1;#主服務(wù) server 127.0.0.1:8899 max_fails=2 fail_timeout=120s weight=1 backup;#備用tomcat服務(wù),主服務(wù)掛了會(huì)調(diào)用備用服務(wù) }
2、tomcat實(shí)現(xiàn)session共享
server.xml在Engine標(biāo)簽內(nèi)加上配置
<Engine name="Catalina" defaultHost="localhost"> <!-- tomcat集群session共享 --> <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> </Engine>
修改web項(xiàng)目,工程WEB-INF下的web.xml,添加如下一句配置
<distributable />
到此這篇關(guān)于Windows如何設(shè)置定時(shí)重啟Tomcat 的文章就介紹到這了,更多相關(guān)Tomcat定時(shí)重啟內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java開啟/關(guān)閉tomcat服務(wù)器的方法
這篇文章主要介紹了Java開啟/關(guān)閉tomcat服務(wù)器的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-06-06IntelliJ?IDEA中配置Tomcat超詳細(xì)教程
這篇文章主要介紹了IntelliJ?IDEA中配置Tomcat超詳細(xì)教程,需要的朋友可以參考下2022-08-08tomcat 6.0.20在一個(gè)機(jī)器上安裝多個(gè)服務(wù)的方法
當(dāng)前前提是你已經(jīng)可以同時(shí)運(yùn)行他們了,他們的端口不能相同,這里只是解釋如何把它們做成服務(wù)2009-08-08搭建Tomcat 8源碼開發(fā)環(huán)境的步驟詳解
相信大家都知道開源軟件tomcat目前幾乎已經(jīng)是Java web開發(fā)的必備軟件了,目前有很多關(guān)于tomcat的書籍,已經(jīng)通過配置對(duì)tomcat進(jìn)行一些跟應(yīng)用業(yè)務(wù)功能的調(diào)優(yōu),但感覺如果僅僅只是了解一些配置,可能稍微少了點(diǎn)什么,下面通過本文深入到源代碼中進(jìn)行學(xué)些和了解。2016-10-10Tomcat啟動(dòng)war包卡死及啟動(dòng)慢的問題解決
本文主要介紹了Tomcat啟動(dòng)war包卡死及啟動(dòng)慢的問題解決,文中通過圖文示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08