【问题标题】:Spring boot batch job identificationSpring Boot 批处理作业识别
【发布时间】:2020-08-29 17:04:57
【问题描述】:

我试图找到看似简单但回避问题的答案。 spring 如何识别批处理配置中的作业。一切都用@Bean 注释,没有什么可识别的。春天是否会识别名称中带有关键字的人?像 xyzStep xzyJob 等?我正在尝试关注here的官方文档

提前致谢。

【问题讨论】:

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


    【解决方案1】:

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

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

      @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());
      } 
    

    【讨论】:

      【解决方案2】:

      所有内容都使用@Bean 进行注释,没有可识别的内容。 spring 会识别那些名称中带有关键字的人吗?

      是的,如 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

      【讨论】:

        猜你喜欢
        • 2014-06-20
        • 2019-11-14
        • 1970-01-01
        • 1970-01-01
        • 2014-08-10
        • 2018-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多