大概思路:
写两个程序,一个负责重启的程序,一个是待重启的程序,在这里为了区分我们假设负责重启的那个程序叫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程序使其重启的简单实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。