springboot启动时同时启动多个JAVA
2022/11/8 1:24:02
本文主要是介绍springboot启动时同时启动多个JAVA,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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); }
这篇关于springboot启动时同时启动多个JAVA的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-04敏捷管理与看板工具:提升研发、设计、电商团队工作效率的利器
- 2025-01-04智慧养老管理工具如何重塑养老生态?
- 2025-01-04如何打造高绩效销售团队:工具与管理方法的结合
- 2025-01-04解决电商团队协作难题,在线文档工具助力高效沟通
- 2025-01-04春节超市管理工具:解锁高效运营与顾客满意度的双重密码
- 2025-01-046种主流销售预测模型:如何根据场景选用最佳方案
- 2025-01-04外贸服务透明化:增强客户信任与合作的最佳实践
- 2025-01-04重新定义电商团队协作:在线文档工具的战略作用
- 2025-01-04Easysearch Java SDK 2.0.x 使用指南(三)
- 2025-01-04百万架构师第八课:设计模式:设计模式容易混淆的几个对比|JavaGuide