Java多次啟動相同jar程序


背景

現在很多軟件都支持集群部署,但是測試環境通常資源有限,所以一般通過單台機器模擬集群部署(使用不同端口,運行相同jar包),本文的目的就是通過多種方式實現此需求。

兩個程序

1、jar程序

  ① springboot程序

  ② 只包含一個main方法,用於啟動程序,輸出進程ID

  ③ 路徑:C:/demo.jarwindows/demo.jarLinux

2、啟動程序

  ① 包含main方法的程序

多種方式

1、通過URLClassLoader加載jar程序(windows平台)

2、通過java -jar命令啟動jar程序(windows平台)

3、通過復制原始jar文件,啟動不同的jar程序(windows平台)

4、通過Linux Shell腳本啟動(Linux平台)

方式一

1、通過URLClassLoader加載jar程序(windows平台)

  ① 說明

    1) 啟動程序多次加載jar程序

    2) jar程序和啟動程序使用相同進程,非獨立進程,無實際意義,僅介紹

  ② 啟動jar程序:運行啟動程序main方法

  ③ 終止jar程序:停止啟動程序(因為共用同一個進程,終止主程序,jar程序會同時終止)

2、代碼

  ① jar程序

@SpringBootApplication

public class DemoStarter {

    public static void main(String[] args) {

        // 獲取進程Id

        String name = ManagementFactory.getRuntimeMXBean().getName();

        String processId = name.split("@")[0];

        System.out.println(processId);


        SpringApplication.run(DemoStarter.class, args);
    }
}

 

  ② 啟動程序

import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

public class Starter1 {

    public static void main(String[] args) throws Exception {

        start("7001");
        start("7002");
        start("7003");
    }
    
    private static void start(String port) throws Exception {

        String path = "file:" + "C:/demo.jar";

        URLClassLoader classLoader = new URLClassLoader(new URL[]{new URL(path)});

        // jar程序的啟動類完整路徑
        Class demo = classLoader.loadClass("DemoStarter");

        Method method = demo.getMethod("main", String[].class);

        method.invoke(null, (Object) new String[]{port});
    }
}

 

方式二

1、通過java -jar命令啟動jar包(windows平台)

  ① 說明

    1) 啟動程序使用命令多次啟動jar

    2) 動態構建cmd命令(不同參數),啟動相同jar程序,各個jar程序使用不同進程

  ② 啟動jar程序

    1) 運行啟動程序main方法

    2) 保存各個進程ID到文件

  ③ 終止jar程序

    1) 根據保存的進程ID停止各個jar程序

2、代碼

  ① jar程序(同方式一)

  ② 啟動程序

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Starter2 {

    public static void main(String[] args) throws Exception {

        cmd("7001");
        cmd("7002");
        cmd("7003");

        // 根據文件中的進程Id終止程序
        killByProcessId("PID1");
        killByProcessId("PID2");
        killByProcessId("PID3");
    }

    private static void cmd(String port) throws Exception {

        String cmd = "java -jar -Dserver.port=" + port + " " + "C:/demo.jar";

        Process p = Runtime.getRuntime().exec(cmd);
        InputStream is = p.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        // 獲取進程Id(DemoStarter-->main方法
        // reader.readLine()第一行為System.out.println(processId)輸出內容
        String processId;
        while ((processId = reader.readLine()) != null) {
            break;
        }
        is.close();
        reader.close();

        // 這里可以將進程ID保存到文件中
        System.out.println("processId:" + processId);
    }

    private static void killByProcessId(String processId) throws Exception {

        String cmd = "taskkill /F /PID \"" + processId + "\"";
        Runtime.getRuntime().exec(cmd);
    }
}

 

 

方式三

1、通過復制原始jar文件,啟動不同的jar程序(windows平台)

  ① 說明

    1) 復制原始jar包,生成新的jar

    2) 動態構建cmd命令(不同參數、不同jar包名稱),啟動不同jar包,各個jar包使用不同進程

  ② 啟動jar程序

    1) 運行啟動程序的main方法

    2) 保存各個進程ID到文件

  ③ 終止程序

    1) 根據保存的進程ID停止各個jar程序

  ④ 復制jar文件需要時間,但可以解決啟動相同jar包可能存在的問題

2、代碼

  ③ jar程序(同方式一)

  ④ 啟動程序

import org.apache.commons.io.FileUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Starter3 {

    public static void main(String[] args) throws Exception {

        copyCmd();

        // 根據文件中的進程Id終止程序
        killByProcessId("PID1");
        killByProcessId("PID2");
        killByProcessId("PID3");
    }

    private static void copyCmd() throws Exception {

        File srcFile = new File("C:/demo.jar");
        File destiFile2 = new File("C:/demo2.jar");
        File destiFile3 = new File("C:/demo3.jar");

        // 刪除之前復制的jar包
        FileUtils.forceDelete(destiFile2);
        FileUtils.forceDelete(destiFile3);

        FileUtils.copyFile(srcFile, destiFile2);
        FileUtils.copyFile(srcFile, destiFile3);

        copy("java -jar -Dserver.port=7001 C:/demo.jar");
        copy("java -jar -Dserver.port=7002 C:/demo2.jar");
        copy("java -jar -Dserver.port=7003 C:/demo3.jar");
    }

    private static void copy(String cmd) throws Exception {

        Process p;

        p = Runtime.getRuntime().exec(cmd);
        InputStream is = p.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        // 獲取進程Id(DemoStarter-->main方法
        // reader.readLine()第一行為System.out.println(processId)輸出內容
        String processId;
        while ((processId = reader.readLine()) != null) {
            break;
        }
        is.close();
        reader.close();

        // 這里可以將進程ID保存到文件中
        System.out.println("ProcessId:" + processId);
    }

    private static void killByProcessId(String processId) throws Exception {

        String cmd = "taskkill /F /PID \"" + processId + "\"";
        Runtime.getRuntime().exec(cmd);
    }
}

 

方式四

1、通過Linux Shell腳本啟動(Linux平台)

  ① 執行java -jar命令

  ② 根據端口獲取進程ID

  ③ 根據進程ID終止程序

2、代碼

  ① jar程序(同方式一)

  ② Shell命令

    1) 啟動程序

#!/bin/bash

java -jar -Dserver.port=7001 /demo.jar
java -jar -Dserver.port=7002 /demo.jar
java -jar -Dserver.port=7003 /demo.jar

 

  2) 終止程序

#!/bin/bash

pid1=`netstat -anp | grep 7001 | awk '{printf $7}' | cut -d/ -f1`
pid2=`netstat -anp | grep 7002 | awk '{printf $7}' | cut -d/ -f1`
pid3=`netstat -anp | grep 7003 | awk '{printf $7}' | cut -d/ -f1`

kill ${pid1}
kill ${pid2}
kill ${pid3}

 

問題&總結

  1、方式一可以通過調用method.invoke傳遞參數

  2、其它方式可通過jar命令傳遞參數

  3、啟動程序通過Process啟動jar程序並獲取jar程序進程ID

  4、多次啟動jar程序時報錯:”unable to register MBean” 
      設置參數spring.jmx.enabled=false

  5、根據端口號獲取進程IDwindows
    netstat -ano|findstr "7001 7002 7003"

  6、根據進程ID停止進程(windows

    taskkill /F /PID "1"

 

參考資料

  1、https://my.oschina.net/u/2971292/blog/2960777

  2、https://www.jianshu.com/p/3eea5e7e1e6f

  3、https://www.cnblogs.com/sxdcgaq8080/p/10579073.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM