Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自更新Spring启动应用程序_Java_Spring Boot_Auto Update - Fatal编程技术网

Java 自更新Spring启动应用程序

Java 自更新Spring启动应用程序,java,spring-boot,auto-update,Java,Spring Boot,Auto Update,我想做的是编写一个自更新的SpringBoot2.2.5应用程序 我编写了一个带有RESTController的springbootstarterweb应用程序来控制更新过程(我知道REST路径没有正确设置,例如/update/update…) 该过程应如下所示: 从应用程序库中使用自传的OpenJDK启动Spring引导应用程序 检查服务器()上是否有可用的更新 将更新下载到本地目录,并将所有文件解压缩到临时的“更新目录”。() 关闭Spring启动应用程序,包括JVM() 修改应用程序文件夹

我想做的是编写一个自更新的SpringBoot2.2.5应用程序

我编写了一个带有RESTController的springbootstarterweb应用程序来控制更新过程(我知道REST路径没有正确设置,例如/update/update…)

该过程应如下所示:

  • 从应用程序库中使用自传的OpenJDK启动Spring引导应用程序
  • 检查服务器()上是否有可用的更新
  • 将更新下载到本地目录,并将所有文件解压缩到临时的“更新目录”。()
  • 关闭Spring启动应用程序,包括JVM()
  • 修改应用程序文件夹(替换库和其他文件)
  • 以与在第1点中启动应用程序相同的方式启动应用程序(使用自行交付的OpenJDK和params)
  • 目前,我尝试使用以下教程完成此任务:

    在这个例子中,我可以很好地重新启动我的应用程序。但是我必须注意操作系统特定的Shell/命令才能正常工作。例如,我不能让它在Windows上与新的CMD窗口一起运行。只有在后台启动应用程序时,我才不会收到任何错误,或者至少应用程序正在启动和响应

    我也看过Spring引导执行器的东西,但这主要是重新加载上下文,但我需要交换当前运行的JVM使用的资源

    所以我想问的是: 有没有办法重新启动我的Spring Boot应用程序,包括参数、更新应用程序中的所有文件并重新启动JVM

    我当前的代码如下:

    SelfupdateApplication(Spring启动类)

    谢谢你的阅读,我感谢你的建议

    @SpringBootApplication
    public class SelfupdateApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SelfupdateApplication.class, args);
        }
    
        public static void update() {
            try {
                SelfupdateApplication.restartApplication(null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * Sun property pointing the main class and its arguments. Might not be defined
         * on non Hotspot VM implementations.
         */
        public static final String SUN_JAVA_COMMAND = "sun.java.command";
    
        /**
         * Restart the current Java application
         * 
         * @param runBeforeRestart some custom code to be run before restarting
         * @throws IOException
         */
        public static void restartApplication(Runnable runBeforeRestart) throws IOException {
            try {
                // java binary
                String java = System.getProperty("java.home") + "/bin/java";
                System.out.println("Java-Home: " + java);
                // vm arguments
                List<String> vmArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
                StringBuffer vmArgsOneLine = new StringBuffer();
                for (String arg : vmArguments) {
                    // if it's the agent argument : we ignore it otherwise the
                    // address of the old application and the new one will be in conflict
                    if (!arg.contains("-agentlib")) {
                        vmArgsOneLine.append(arg);
                        vmArgsOneLine.append(" ");
                    }
                }
                // init the command to execute, add the vm args
                final StringBuffer cmd = new StringBuffer("\"" + java + "\" " + vmArgsOneLine);
    
                // program main and program arguments
                String[] mainCommand = System.getProperty(SUN_JAVA_COMMAND).split(" ");
                // program main is a jar
                if (mainCommand[0].endsWith(".jar")) {
                    // if it's a jar, add -jar mainJar
                    cmd.append("-jar " + new File(mainCommand[0]).getPath());
                } else {
                    // else it's a .class, add the classpath and mainClass
                    cmd.append("-cp \"" + System.getProperty("java.class.path") + "\" " + mainCommand[0]);
                }
                // finally add program arguments
                for (int i = 1; i < mainCommand.length; i++) {
                    cmd.append(" ");
                    cmd.append(mainCommand[i]);
                }
                // execute the command in a shutdown hook, to be sure that all the
                // resources have been disposed before restarting the application
                Runtime.getRuntime().addShutdownHook(new Thread() {
                    @Override
                    public void run() {
                        try {
                            Runtime.getRuntime().exec(cmd.toString());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
                // execute some custom code before restarting
                if (runBeforeRestart != null) {
                    runBeforeRestart.run();
                }
                // exit
                System.exit(0);
            } catch (Exception e) {
                // something went wrong
                throw new IOException("Error while trying to restart the application", e);
            }
        }
    }
    
    @RestController
    @RequestMapping("/update")
    public class UpdateController {
    
        @GetMapping("/download")
        public boolean download() {
            File currentDir = new File(System.getProperty("user.dir"));
            File destDir = new File(currentDir.getAbsolutePath() + File.separator + "update_lib");
            File downloadDir = new File("C:/temp/selfupdate/someServer/update");
    
            try {
                FileUtils.copyDirectory(downloadDir, destDir);
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
    
        @GetMapping("/check")
        public void check() {
            // not relevant for now
            SomeHelper.checkForUpdate();
        }
    
        @GetMapping("/update")
        public void update() {
            SelfupdateApplication.update();
        }
    
        @GetMapping("/restart")
        public void restart() {
            try {
                SelfupdateApplication.restartApplication(null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        @GetMapping("/alive")
        public String alive() {
            return "Yes i am still here ;-)";
        }
    
        @GetMapping("/shutdown")
        public String shutdown() {
            Thread thread = new Thread() {
                public void run() {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.exit(0);
                }
            };
            thread.start();
            return "System shutdown initiated";
        }
    
        @GetMapping("/version")
        public String version() {
            return "0.0.1";
        }
    
    }