ICode9

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

java 同时启动多个项目

2022-01-05 21:36:10  阅读:156  来源: 互联网

标签:java String 多个 启动 args jar sync new import


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); } }

标签:java,String,多个,启动,args,jar,sync,new,import
来源: https://www.cnblogs.com/g177w/p/15768924.html

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

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

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

ICode9版权所有