Java操作另一個(gè)Java程序使其重啟的簡(jiǎn)單實(shí)現(xiàn)
大概思路:
寫(xiě)兩個(gè)程序,一個(gè)負(fù)責(zé)重啟的程序,一個(gè)是待重啟的程序,在這里為了區(qū)分我們假設(shè)負(fù)責(zé)重啟的那個(gè)程序叫A,待重啟的程序叫B,他們都是線程,還要搭配數(shù)據(jù)庫(kù),他是兩個(gè)程序的橋梁,通過(guò)設(shè)置信號(hào)量進(jìn)行判斷程序狀態(tài)(不妨設(shè)置信號(hào)量為Flag),我是這么設(shè)置的,0:表示程序正在運(yùn)行中,1:表示程序需要重啟,正準(zhǔn)備做關(guān)閉自己的操作(只針對(duì)待重啟的程序B),2:表示B程序已經(jīng)把自己給關(guān)閉了,需要A程序把B程序啟動(dòng)。
實(shí)現(xiàn)步驟:
A程序:寫(xiě)一個(gè)線程進(jìn)行讀信號(hào)量Flag,當(dāng)Flag為2的時(shí)候就把B程序啟動(dòng)
B程序:寫(xiě)一個(gè)線程進(jìn)行讀信號(hào)量Flag,當(dāng)Flag為1的時(shí)候就把自己給關(guān)閉(java System.exit(0);)
數(shù)據(jù)庫(kù):需要一個(gè)表存Flag的值,創(chuàng)建表restart,并新建一個(gè)字段Flag,int(4)noNull
實(shí)現(xiàn)細(xì)節(jié):
A 程序:
package com.app;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import databasetool.DBtool;
public class ReStart implements Runnable {
int status = 0;
public void run() {
DBtool con = new DBtool();
ResultSet rs = null;
String select = "select * from restart";
String restar = "update restart set status = '0'";// 準(zhǔn)備啟動(dòng)程序,設(shè)置Status為0,表示已啟動(dòng)
try {
int result = con.executeUpdate(restar);
System.out.println("初始化,并將status狀態(tài)設(shè)置為0,表示程序正常被啟動(dòng)了!");
} catch (SQLException e) {
e.printStackTrace();
}
while (true) {
while (true) {
if (status == 2) {// 2:表示關(guān)閉的程序等待重啟
System.out.println("status狀態(tài)為2,表示需要重新啟動(dòng)數(shù)采程序!");
try {
int result = con.executeUpdate(restar);
System.out.println("程序馬上就被啟動(dòng),并將status狀態(tài)設(shè)置為0,表示程序正常運(yùn)行!");
} catch (SQLException e) {
e.printStackTrace();
}
String cmd = "cmd /c start E:\\Bats\\MainThread.bat";// pass
try {
Process ps = Runtime.getRuntime().exec(cmd);
ps.waitFor();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
rs = con.executeQuery(select);
while (rs.next()) {
status = rs.getInt("status");
System.out.println("檢測(cè)當(dāng)前狀態(tài)status:"+status);
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
ReStart res = new ReStart();
res.run();
}
}
B程序:
package datacollect;
import java.sql.ResultSet;
import java.sql.SQLException;
import databasetool.DBtool;
public class ExitMain implements Runnable {
@Override
public void run() {
DBtool dbtool = new DBtool();
int status = 0;// 0:表示不需要重啟
ResultSet rs = null;
String select = "select * from restart";
String restar = "update restart set status = '2'";// 關(guān)閉了程序,等待重啟
// 寫(xiě)日志相關(guān)內(nèi)容
while (true) {
try {
rs = dbtool.executeQuery(select);
while (rs.next()) {
status = rs.getInt("status");
}
} catch (SQLException e) {
e.printStackTrace();
}
if (status == 1) {// 1:表示等待關(guān)閉程序
System.out.println("status狀態(tài)為1,表示需要關(guān)閉當(dāng)前程序!");
try {
int result = dbtool.executeUpdate(restar);
System.out.println("程序馬上就被關(guān)閉,并將status狀態(tài)設(shè)置為2,表示程序關(guān)閉了,需要重啟!");
} catch (SQLException e) {
e.printStackTrace();
}
System.exit(0);
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ExitMain extm = new ExitMain();
extm.run();
}
}
數(shù)據(jù)庫(kù)讀取工具類(lèi):
package databasetool;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBtool {
private Connection connection = null;
public Statement statement = null;
private ResultSet result = null;
public DBtool() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName=tianjincollect;user=sa;password=123456";
connection = DriverManager.getConnection(url);
statement = connection.createStatement();
} catch (SQLException ex) {
System.out.println(ex.getMessage());
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
}
public ResultSet executeQuery(String sql) throws SQLException {
try {
result = statement.executeQuery(sql);
} catch (SQLException se) {
System.out.println("ERROR:" + se.getMessage());
}
return result;
}
public int executeUpdate(String sql) throws SQLException {
int updatenum = 0;
try {
updatenum = statement.executeUpdate(sql);
return updatenum;
} catch (SQLException se) {
System.out.println("ERROR:" + se.getMessage());
}
return updatenum;
}
public void free() throws SQLException {
try {
if (result != null)
result.close();
if (statement != null)
statement.close();
if (connection != null)
connection.close();
} catch (SQLException se) {
System.out.println("ERROR:" + se.getMessage());
}
}
public static void main(String[] args) {
DBtool con = new DBtool();
ResultSet rs = null;
String sql = "select * from restart";
try {
rs = con.executeQuery(sql);
while(rs.next()){
int status = rs.getInt("status");
System.out.println(status);
}
} catch (SQLException e) {
e.printStackTrace();
}
sql = "update restart set status = '1'";
try {
int result = con.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
以上這篇Java操作另一個(gè)Java程序使其重啟的簡(jiǎn)單實(shí)現(xiàn)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA的部署設(shè)置改為war exploded運(yùn)行項(xiàng)目出錯(cuò)問(wèn)題
在使用IDEA配置warexploded部署時(shí),可能會(huì)遇到路徑問(wèn)題或404錯(cuò)誤,解決方法是進(jìn)入Deployment設(shè)置,刪除Application content中的/marry_war_exploded,使其為空,然后重新運(yùn)行項(xiàng)目即可,這是一種有效的解決策略,希望能幫助到遇到同樣問(wèn)題的開(kāi)發(fā)者2024-10-10
Spring boot Rabbitmq消息防丟失實(shí)踐
這篇文章主要介紹了Spring boot Rabbitmq消息防丟失實(shí)踐,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
一篇文章帶你了解mybatis的動(dòng)態(tài)SQL
這篇文章主要為大家介紹了mybatis的動(dòng)態(tài)SQL?,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-01-01
RabbitMQ?延遲隊(duì)列實(shí)現(xiàn)訂單支付結(jié)果異步階梯性通知(實(shí)例代碼)
這篇文章主要介紹了RabbitMQ?延遲隊(duì)列實(shí)現(xiàn)訂單支付結(jié)果異步階梯性通知,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
springboot集成nacos無(wú)法動(dòng)態(tài)獲取nacos配置的問(wèn)題
這篇文章主要介紹了springboot集成nacos無(wú)法動(dòng)態(tài)獲取nacos配置的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Java實(shí)現(xiàn)電影院訂票系統(tǒng)代碼
這篇文章主要介紹了Java實(shí)現(xiàn)電影院訂票系統(tǒng)代碼,代碼實(shí)現(xiàn)了界面類(lèi)登錄注冊(cè)類(lèi),用戶類(lèi)等,具有一定參考價(jià)值,需要的朋友可以參考下。2017-11-11
Spring Cloud Feign實(shí)現(xiàn)文件上傳下載的示例代碼
Feign框架對(duì)于文件上傳消息體格式并沒(méi)有做原生支持,需要集成模塊feign-form來(lái)實(shí)現(xiàn),本文就詳細(xì)的介紹一下如何使用,感興趣的可以了解一下2022-02-02

