大概思路:
寫兩個程序,一個負責重啟的程序,一個是待重啟的程序,在這里為了區分我們假設負責重啟的那個程序叫A,待重啟的程序叫B,他們都是線程,還要搭配數據庫,他是兩個程序的橋梁,通過設置信號量進行判斷程序狀態(不妨設置信號量為Flag),我是這么設置的,0:表示程序正在運行中,1:表示程序需要重啟,正準備做關閉自己的操作(只針對待重啟的程序B),2:表示B程序已經把自己給關閉了,需要A程序把B程序啟動。
實現步驟:
A程序:寫一個線程進行讀信號量Flag,當Flag為2的時候就把B程序啟動
B程序:寫一個線程進行讀信號量Flag,當Flag為1的時候就把自己給關閉(java System.exit(0);)
數據庫:需要一個表存Flag的值,創建表restart,并新建一個字段Flag,int(4)noNull
實現細節:
A 程序:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
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'" ; // 準備啟動程序,設置Status為0,表示已啟動 try { int result = con.executeUpdate(restar); System.out.println( "初始化,并將status狀態設置為0,表示程序正常被啟動了!" ); } catch (SQLException e) { e.printStackTrace(); } while ( true ) { while ( true ) { if (status == 2 ) { // 2:表示關閉的程序等待重啟 System.out.println( "status狀態為2,表示需要重新啟動數采程序!" ); try { int result = con.executeUpdate(restar); System.out.println( "程序馬上就被啟動,并將status狀態設置為0,表示程序正常運行!" ); } 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( "檢測當前狀態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程序:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
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'" ; // 關閉了程序,等待重啟 // 寫日志相關內容 while ( true ) { try { rs = dbtool.executeQuery(select); while (rs.next()) { status = rs.getInt( "status" ); } } catch (SQLException e) { e.printStackTrace(); } if (status == 1 ) { // 1:表示等待關閉程序 System.out.println( "status狀態為1,表示需要關閉當前程序!" ); try { int result = dbtool.executeUpdate(restar); System.out.println( "程序馬上就被關閉,并將status狀態設置為2,表示程序關閉了,需要重啟!" ); } 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(); } } |
數據庫讀取工具類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
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" ); 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操作另一個Java程序使其重啟的簡單實現就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。