0

我打算使用 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 Batch 数据库中。

  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 中启动。

谢谢你的帮助!

4

1 回答 1

0

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

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

于 2021-02-22T11:28:35.923 回答