java - Spring批处理ApplicationContext

标签 java asynchronous spring-batch applicationcontext

我在我的项目中使用 Spring Batch 应用程序。一旦我启动了 spring Batch main 方法,在 main 方法的末尾,我将用来关闭 classpathxmlapplicationcontext。

  1. 是否需要关闭classpathxmlapplicationcontext?
  2. 主要问题之一是,如果我在批处理应用程序中触发了任何异步调用,那么当主方法代码到达 classpathxmlapplicationcontext.close() 时,这些异步调用将被终止?
  3. 如果我注释了 classpathxmlapplicationcontext.close() 那么,即使所有逻辑都已完成,我的程序也会连续运行而不会终止。
  4. 如何解决这个问题?我需要在关闭我的 classpathxmlapplicationcontext 之前执行所有异步调用。
  5. 考虑到我所有的异步都会花费一点额外的时间。

最佳答案

是的,我们必须关闭 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/

相关文章:

java - Gradle未在intelliJ idea 2016.2中下载依赖项

java - 在 byte[] 之外的内存中创建 zip 文件。 Zip 文件总是损坏

spring-batch - spring 批处理流作业与 spring 组合任务

java - Spring Batch Java 配置 : Skip step when exception and go to next steps

c++ - 虚拟 ASIO 服务回调?

java - Spring Batch 从监听器类调用 DAO 类

java - Eclipse 如何运行 map reduce 作业?

java - java/android中将两个组件附加到同一个事件如何区分每个组件

c# - 为 REST 服务异步返回 http 状态码 202?

android import android-async-http-master 有错误