首页 > 解决方案 > 如何从 Spring Boot webapp 运行 commads

问题描述

我有 Spring Boot Web 应用程序,我想在命令行中调用一些命令。当我在运行进程后使用 ProcessBuilder 和 Process 类时,ExecutorService 被关闭。

我运行该过程的方法:

public void runTestsInProject(String projectPath){
    System.out.println("Starting runTestsInProject() ------");
    try{
        ProcessBuilder builder = new ProcessBuilder(
                "cmd.exe", "/c", "cd \"" + projectPath + "\" && mvn clean test");
        builder.redirectErrorStream(true);

        Process p = builder.start();

        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while (true) {
            line = r.readLine();
            if (line == null) { break; }
        }
    } catch (IOException e){e.printStackTrace();}
}

错误日志:

2020-07-27 20:33:20.246  INFO 7248 --- [       Thread-4] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2020-07-27 20:33:20.250  INFO 7248 --- [       Thread-4] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-07-27 20:33:20.254  INFO 7248 --- [       Thread-4] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-07-27 20:33:20.270  INFO 7248 --- [       Thread-4] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

春天又开始了……

2020-07-27 20:33:29.778  INFO 7248 --- [nio-8080-exec-9] o.a.c.loader.WebappClassLoaderBase       : Illegal access: this web application instance has been stopped already. Could not load [META-INF/services/javax.xml.bind.JAXBContext]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.

java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [META-INF/services/javax.xml.bind.JAXBContext]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.

标签: springmultithreadingspring-bootmavencommand-line

解决方案


您可能在依赖项中添加了spring-boot-devtools。只要 Devtools 发现项目的类路径发生更改,它就会重新启动应用程序。

您正在运行的进程(mvn-clean)导致项目的类路径发生更改,因此您的应用程序正在重新启动。

如果你运行不干扰项目类路径的普通进程,你将不会面临重启或执行器关闭的问题。

从spring 开发工具文档中查看此快照:

As DevTools monitors classpath resources, the only way to trigger a restart is to update the classpath. The way in which you cause the classpath to be updated depends on the IDE that you are using. In Eclipse, saving a modified file will cause the classpath to be updated and trigger a restart. In IntelliJ IDEA, building the project (Build -> Build Project) will have the same effect.


推荐阅读