spring - 如何从 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.
解决方案
您可能在依赖项中添加了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
.
推荐阅读
- google-chrome - 创建自己的 chrome 扩展:如何获取书签图标并打开本地 html 文件
- javascript - 动态更改 HTML 属性
- python-3.x - 为什么 numpy tobytes() 方法不仅返回十六进制数字,还返回其他字符?
- angular - Electron-Angular open client-side dialog on ipc event
- python - python 上的 user32.GetAsyncKeyState 以捕获键和鼠标点击
- c# - 使用 2 个数组的 RestSharp 反序列化
- docker - 将远程 Docker 容器的端口转发到 localhost
- python - 使用 crm1.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式:
- bash - BASH , for 循环减去一个数字
- c++ - 在不相关的类型上使用 static_cast 运算符时出错