• java 同时启动多个项目


    1.工具类 JarStarter.jar import java.io.*; import java.util.Objects; /** * 启动项目依赖可执行jar的工具类 */ public class JarStarter { /** * * @param parttern 匹配的jar模式 * @param args 运行jar需要的参数 * @param sync 同步执行 */ 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); } } 2.在springboot中的使用 import com.google.common.collect.Lists; import com.togeek.task.common.JarStarter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration; import org.springframework.scheduling.annotation.EnableScheduling; import java.util.List; @EnableScheduling @SpringBootApplication(scanBasePackages = {"cn.togeek.util", "com.togeek"}, exclude = LiquibaseAutoConfiguration.class) public class Application { public static void main(String[] args) { List jars = Lists.newArrayList(args); jars.add("xxl-job-admin-2.3.1-SNAPSHOT.jar"); for (String jar : jars) { JarStarter.start(jar,null,false); } SpringApplication.run(Application.class, args); } }
  • 相关阅读:
    java8 stream().map().collect()用法
    Java中Collections的emptyList、EMPTY_LIST详解
    zxing实现java二维码生成和解析
    机器学习与数据挖掘—K邻近算法(KNN)
    第一个Web项目(IDEA)
    Ucore操作系统实验-实验课程设计
    数据挖掘与机器学习--损失函数
    机器数据挖掘--常见监督学习算法以及数据挖掘流程
    Tomcat安装-环境变量配置-启动-关闭
    操作系统实验教程(Ucore)--Lab6
  • 原文地址:https://www.cnblogs.com/g177w/p/15768924.html
Copyright © 2020-2023  润新知