??????????
????1??GuardA?????
1 public class GuardA {
2     // GuardA??????????????
3     private File fileGuardA;
4     private FileOutputStream fileOutputStreamGuardA;
5     private FileChannel fileChannelGuardA;
6     private FileLock fileLockGuardA;
7     // GuardB??????B????
8     private File fileGuardB;
9     private FileOutputStream fileOutputStreamGuardB;
10     private FileChannel fileChannelGuardB;
11     private FileLock fileLockGuardB;
12
13     public GuardA() throws Exception {
14         fileGuardA = new File(Configure.GUARD_A_LOCK);
15         if (!fileGuardA.exists()) {
16             fileGuardA.createNewFile();
17         }
18         //???????????ò??????GuardA??????????
19         fileOutputStreamGuardA = new FileOutputStream(fileGuardA);
20         fileChannelGuardA = fileOutputStreamGuardA.getChannel();
21         fileLockGuardA = fileChannelGuardA.tryLock();
22         if (fileLockGuardA == null) {
23             System.exit(0);
24         }
25
26         fileGuardB = new File(Configure.GUARD_B_LOCK);
27         if (!fileGuardB.exists()) {
28             fileGuardB.createNewFile();
29         }
30         fileOutputStreamGuardB = new FileOutputStream(fileGuardB);
31         fileChannelGuardB = fileOutputStreamGuardB.getChannel();
32     }
33
34     /**
35      * ???B??????
36      *
37      * @return true B???????
38      */
39     public boolean checkGuardB() {
40         try {
41             fileLockGuardB = fileChannelGuardB.tryLock();
42             if (fileLockGuardB == null) {
43                 return true;
44             } else {
45                 fileLockGuardB.release();
46                 return false;
47             }
48         } catch (IOException e) {
49             System.exit(0);
50             // never touch
51             return true;
52         }
53     }
54 }
????2??GuardServer?????
1 public class GuardServer {
2     private String servername;
3
4     public GuardServer(String servername) {
5         this.servername = servername;
6     }
7
8     public void startServer(String cmd) throws Exception {
9         System.out.println("Start Server : " + cmd);
10         //????????
11 //        String[] cmds = cmd.split(" ");
12 //        ProcessBuilder builder = new ProcessBuilder(cmds);
13
14         //
15         ProcessBuilder builder=new ProcessBuilder(new String[]{"/bin/sh"??"-c"??cmd});
16         //??????????????????λ??/dev/tty
17         builder.redirectOutput(new File("/dev/tty"));
18         builder.redirectError(new File("/dev/tty"));
19         builder.start(); // throws IOException
20         Thread.sleep(10000);
21     }
22
23     /**
24      * ????????????
25      *
26      * @return ?????????java?????pid
27      * @return pid >0 ??????? pid <=0 ???????java????δ????
28      * **/
29     public int checkServer() throws Exception {
30         int pid = -1;
31         Process process = null;
32         BufferedReader reader = null;
33         process = Runtime.getRuntime().exec("jps -l");
34         reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
35         String line;
36         while ((line = reader.readLine()) != null) {
37             String[] strings = line.split("\s{1??}");
38             if (strings.length < 2)
39                 continue;
40             if (strings[1].contains(servername)) {
41                 pid = Integer.parseInt(strings[0]);
42                 break;
43             }
44         }
45         reader.close();
46         process.destroy();
47         return pid;
48     }
49 }
????3??GuardAMain???
1 public class GuardAMain {
2     public static void main(String[] args) throws Exception {
3         GuardA guardA = new GuardA();
4         Configure configure = new Configure();
5         GuardServer server = new GuardServer(configure.getServername());
6         while (true) {
7             // ???GuardBδ???? ????GuardB
8             if (!guardA.checkGuardB()) {
9                 System.out.println("Start GuardB.....");
10                 Runtime.getRuntime().exec(configure.getStartguardb());
11             }
12             // ???????????
13             if (server.checkServer() <= 0) {
14                 boolean isServerDown = true;
15                 // trip check
16                 for (int i = 0; i < 3; i++) {
17                     // ?????????????
18                     if (server.checkServer() > 0) {
19                         isServerDown = false;
20                         break;
21                     }
22                 }
23                 if (isServerDown)
24                     server.startServer(configure.getStartserver());
25             }
26             Thread.sleep(configure.getInterval());
27         }
28     }
29 }