我在我的项目中使用 Spring Batch 应用程序。一旦我启动了 spring Batch main 方法,在 main 方法的末尾,我将用来关闭 classpathxmlapplicationcontext。
- 是否需要关闭classpathxmlapplicationcontext?
- 主要问题之一是,如果我在批处理应用程序中触发了任何异步调用,那么当主方法代码到达 classpathxmlapplicationcontext.close() 时,这些异步调用将被终止?
- 如果我注释了 classpathxmlapplicationcontext.close() 那么,即使所有逻辑都已完成,我的程序也会连续运行而不会终止。
- 如何解决这个问题?我需要在关闭我的 classpathxmlapplicationcontext 之前执行所有异步调用。
- 考虑到我所有的异步都会花费一点额外的时间。
最佳答案
是的,我们必须关闭 classpathxmlapplicationcontext。
尝试下面的代码
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(...);
try {
[...]
} finally {
ctx.close();
}
或者,在 Java 7 或更高版本中
try(ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(...)) {
[...]
}
关于java - Spring批处理ApplicationContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61775698/