春季批处理ApplicationContext

时间:2020-05-13 13:18:05

标签: java asynchronous spring-batch applicationcontext

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

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

1 个答案:

答案 0 :(得分:0)

是的,我们必须关闭classpathxmlapplicationcontext。

尝试以下代码

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(...);
try {
    [...]
} finally {
    ctx.close();
}

或者,在Java 7或更高版本中

try(ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(...)) {
    [...]
}