Spring Boot批处理作业标识

时间:2020-05-13 13:20:48

标签: spring-boot spring-batch spring-boot-configuration

我试图找到看似简单但回避性问题的答案。 Spring如何在批处理配置中识别作业。一切都用@Bean注释,没有任何可识别的内容。 Spring是否会在名称本身中标识带有关键字的关键字?像xyzStep xzyJob等?我正在尝试遵循here

上的官方文档

谢谢。

2 个答案:

答案 0 :(得分:1)

当我们尝试启动作业时,Spring会在事件中标识作业。下面是一个小片段供参考,

这是一个bean定义,它返回Job类型,并包含其中所有步骤的编排,

  @Bean
  public Job testJob() throws Exception {

    Job job = jobBuilderFactory.get("testJob").incrementer(new RunIdIncrementer())
        .start(step1()).next(step2()).next(step3()).end()
        .listener(jobCompletionListener()).build();

    ReferenceJobFactory referenceJobFactory = new ReferenceJobFactory(job);
    registry.register(referenceJobFactory);

    return job;
}

下面将利用Bean并启动作业,这意味着定义为作业一部分的工作流将被执行,

@Autowired
JobLauncher jobLauncher;

@Autowired
Job testJob;

// eliminating the method defnition for simplicity,

  try {
        jobLauncher.run(testJob, jobParameters);
  } catch (Exception e) {
        logger.error("Exception while running a batch job {}", e.getMessage());
  } 

答案 1 :(得分:1)

所有内容都用@Bean注释,没有可识别的内容。春天是否会在名称中标识带有关键字的关键字?

是的,如Javadoc of the @Bean annotation中所述:

the default strategy for determining the name of a bean is to use the name of the @Bean method

也就是说,应该注意的是,Spring bean名称与Spring Batch作业/步骤名称之间存在差异(可以有所不同):

@Bean
public Job job(JobBuilderFactory jobBuilderFactory) {
    return jobBuilderFactory.get("myJob")
            //...
            .build();
}

在此示例中,Spring bean名称为job,Spring Batch作业名称为myJob