ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

springboot启动时同时启动多个JAVA

2022-11-07 16:39:16  阅读:285  来源: 互联网

标签:java 函数 学习 系统 语言 平台 方法 安装 QML c++ 数据


1.思路:主程序调用工具类,工具类调用java -jar 启动程序

2.代码:

package com.togeek.task.common;

import java.io.*;
import java.util.Objects;

public class StartUpUtil {

    public static void start (String parttern, String[] args,boolean sync) {
        String root = System.getProperty("user.dir");
        findAndStartJar(new File(root),parttern,args,sync);
    }


    private static void findAndStartJar (File file, String parttern, String[] args,boolean sync) {
        File[] listFiles = file.listFiles();
        if (listFiles != null) {
            for (File x : listFiles) {
                if (x.getName().contains(parttern) && x.getName().endsWith(".jar")) {
                    doStart(x.getAbsolutePath(),args,sync);
                } else {
                    if (x.isDirectory()) {
                        findAndStartJar (x,parttern,args,sync);
                    }
                }
            }
        }
    }

    private static boolean doStart (String jarPath,String[] args,boolean sync) {
        if (sync) {
            start0(jarPath, args, sync);
        } else {
            new Thread(() -> {
                start0(jarPath, args, sync);
            }).start();
        }
        return true;
    }

    private static void start0 (String jarPath,String[] args,boolean sync) {
        Runtime runtime = Runtime.getRuntime();
        String cmd = "java -jar " + jarPath;
        if (Objects.nonNull(args) && args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                cmd = cmd.concat(args[i]);
            }
        }
        System.err.println("use command " + cmd + "start a new jar");
        try {
            Process process = runtime.exec(cmd);
            doPrint(process.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void registerHook() {
        new Thread(()-> {
            while (true) {
                Runtime runtime = Runtime.getRuntime();
                runtime.addShutdownHook(new Thread(() -> {
                    try {
                        System.err.println("exit xxl-job-admin");
                        Process exec = runtime.exec("jps");
                        doPrint(exec.getInputStream());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }));
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private static void doPrint(InputStream inputStream) throws IOException {
        byte bytes[] = new byte[1024];
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        inputStream.close();
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        start("xxl-job-admin-2.3.1",null,true);
//        registerHook();
//        Thread.sleep(10*1000);

    }
}

3.在springboot启动类中使用

package com.togeek.task;

import com.togeek.task.common.StartUpUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication(scanBasePackages = {"cn.togeek.util", "com.togeek"}, exclude = LiquibaseAutoConfiguration.class)
public class Application {
   public static void main(String[] args) {
      StartUpUtil.start("xxl-job-admin-2.3.1-SNAPSHOT.jar",null,false);
      SpringApplication.run(Application.class, args);
   }

标签:java,函数,学习,系统,语言,平台,方法,安装,QML,c++,数据
来源:

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有