Windows如何設(shè)置定時重啟Tomcat
項目場景:
系統(tǒng):Windows 7
Tomcat:apache-tomcat-8.0.5
JDK:1.8
問題描述
最近項目的Tomcat隔一段時間就假死,最后想到的解決方式就是:每天凌晨1點重啟tomact。
解決方案:
使用Windows系統(tǒng)的計劃任務(wù)程序,可以在這里設(shè)置定時執(zhí)行的.bat批處理文件(將你要定時執(zhí)行的cmd命令放在這里),這樣就可以實現(xiàn)讓電腦在某個時刻做你想讓它干的事。
實現(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ù),請稍等...... @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 正在啟動Tomcat服務(wù),請稍等...... @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 啟動Tomcat服務(wù)完成
把restart.bat 文件復(fù)制到apache-tomcat-8.0.50\bin目錄下 ,然后可以直接雙擊運行,啟動會報個錯,是因為tomcat沒啟動,是正常的。
二、創(chuàng)建任務(wù)計劃程序
1、開始菜單搜索“任務(wù)計劃程序”
或者到控制面板里面找到“計劃任務(wù)”
2、彈出任務(wù)計劃程序界面,在右側(cè)點擊創(chuàng)建任務(wù)
3、輸入 名稱和描述
4、點擊觸發(fā)器選項卡,點擊左下角新建,彈出新建操作,新建一個觸發(fā)器,每天凌晨一點觸發(fā)
5、點擊操作選項卡,點擊左下角新建,彈出新建操作,點擊瀏覽。選擇要執(zhí)行的腳本restart.bat ,點擊打開確定。
附加:
這里說下nginx配置集群和tomcat實現(xiàn)session共享
首先要啟動兩個tomcat,然后端口不一樣,比如8889和8899
1、nginx.conf關(guān)鍵配置
upstream serverlist { #最少連接數(shù),根據(jù)連接數(shù)多少自動選擇后端服務(wù)器,連接數(shù)相等時根據(jù)權(quán)重選擇 least_conn; #每個請求按訪問ip的hash結(jié)果分配,這樣每個訪客固定訪問一個后端服務(wù)器,可以解決session的問題 #ip_hash; #若120秒內(nèi)出現(xiàn)2次錯誤,則下個120秒內(nèi)不再訪問此服務(wù)器,weight權(quán)重越大,請求機(jī)會越多 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ù)掛了會調(diào)用備用服務(wù) }
2、tomcat實現(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項目,工程WEB-INF下的web.xml,添加如下一句配置
<distributable />
到此這篇關(guān)于Windows如何設(shè)置定時重啟Tomcat 的文章就介紹到這了,更多相關(guān)Tomcat定時重啟內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java開啟/關(guān)閉tomcat服務(wù)器的方法
這篇文章主要介紹了Java開啟/關(guān)閉tomcat服務(wù)器的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-06-06IntelliJ?IDEA中配置Tomcat超詳細(xì)教程
這篇文章主要介紹了IntelliJ?IDEA中配置Tomcat超詳細(xì)教程,需要的朋友可以參考下2022-08-08tomcat 6.0.20在一個機(jī)器上安裝多個服務(wù)的方法
當(dāng)前前提是你已經(jīng)可以同時運行他們了,他們的端口不能相同,這里只是解釋如何把它們做成服務(wù)2009-08-08搭建Tomcat 8源碼開發(fā)環(huán)境的步驟詳解
相信大家都知道開源軟件tomcat目前幾乎已經(jīng)是Java web開發(fā)的必備軟件了,目前有很多關(guān)于tomcat的書籍,已經(jīng)通過配置對tomcat進(jìn)行一些跟應(yīng)用業(yè)務(wù)功能的調(diào)優(yōu),但感覺如果僅僅只是了解一些配置,可能稍微少了點什么,下面通過本文深入到源代碼中進(jìn)行學(xué)些和了解。2016-10-10