【问题标题】:Spring Batch: How to start a job but not execute it, but execute it in an other java instanceSpring Batch:如何启动一个作业但不执行它,而是在另一个java实例中执行它
【发布时间】:2021-02-22 19:39:48
【问题描述】:

我打算使用 Spring Batch。我们喜欢在响应前端请求的 pod 中启动新的作业执行。

伪代码:

@PostMapping(path = "/request-report/{id}")
public void requestReport(String id){
  this.jobOperator.start("reportJob", new Properties("1"));
}

但我们不希望作业在前端 pod 中执行。为此,我们喜欢构建一个单独的微服务 pod。

我看到以下解决方案:

  1. 从前端 pod 到 spring-batch pod 进行一次休息调用,然后在那里开始工作。我可以这样做,但如果可能的话,我想跳过这一步并将其集成到 spring 批处理数据库中。

  2. 在前端 pod 中,我创建了具有大小为零的 SimpleAsyncTaskExecutor 的 JobLauncher。所以它永远不会执行作业。

https://docs.spring.io/spring-batch/docs/current/reference/html/job.html#configuringJobLauncher

@Bean
public JobLauncher jobLauncher() {
    SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
    jobLauncher.setJobRepository(jobRepository());
    jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
    jobLauncher.afterPropertiesSet();
    return jobLauncher;
}
  1. 在前端 pod 中,我不使用 BatchAutoConfiguration 而是留下一些东西,但是什么?

我想我还必须编写一些软件来扫描作业表并检查是否存在未启动的作业,并在 spring-batch-pod 中再次启动。

感谢您的帮助!

【问题讨论】:

    标签: spring-batch


    【解决方案1】:

    如果您使用 SimpleAsyncTaskExecutor 配置作业启动器,它将在同一 JVM 中的不同线程中启动作业(因此在同一容器和 pod 中)。

    因此,除非您提供自定义 TaskExecutor 以在单独的 JVM/容器/pod 中启动作业,否则您需要更改架构以使用作业队列。参考文档中的Launching Batch Jobs through Messages 部分显示了如何设置这种模式。

    【讨论】:

      猜你喜欢
      • 2012-06-12
      • 2014-12-10
      • 1970-01-01
      • 2020-02-25
      • 1970-01-01
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      相关资源
      最近更新 更多