Java实现Linux下服务器程序的双守护进程
作者:网络转载 发布时间:[ 2014/9/10 10:57:00 ] 推荐标签:Linux Java 服务器
四、实现
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 }
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
Java性能测试有哪些不为众人所知的原则?Java设计模式??装饰者模式谈谈Java中遍历Map的几种方法Java Web入门必知你需要理解的Java反射机制知识总结编写更好的Java单元测试的7个技巧编程常用的几种时间戳转换(java .net 数据库)适合Java开发者学习的Python入门教程Java webdriver如何获取浏览器新窗口中的元素?Java重写与重载(区别与用途)Java变量的分类与初始化JavaScript有这几种测试分类Java有哪四个核心技术?给 Java开发者的10个大数据工具和框架Java中几个常用设计模式汇总java生态圈常用技术框架、开源中间件,系统架构及经典案例等

sales@spasvo.com