Spring Batch Job采用先前的执行参数

时间:2019-06-10 10:21:50

标签: spring spring-boot spring-batch batch-processing spring-cloud

我正在使用Spring Cloud数据流,并创建了一个包含作业的Spring Cloud任务。该作业具有一个名为last_modified_date的参数,该参数是可选的。在代码中,我指定了last_modified_date为null(即尚未作为参数传递)的日期。问题是,如果我为作业的一个实例传递了last_modified_date,但是我没有传递下一个,则它将在上一次执行时选择一个,而不是将其传递为null并从代码中获取。

@Component
@StepScope
public class SalesforceAdvertiserLoadTasklet implements Tasklet {

  @Value("#{jobParameters['last_modified_date']}")
  protected Date lastModifiedDate;

  private static final Logger logger =
      LoggerFactory.getLogger(SalesforceAdvertiserLoadTasklet.class);

  @Override
  public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
      throws Exception {

    if(lastModifiedDate == null) {
      lastModifiedDate =
          Date.from(LocalDate.now().minusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
    }
    logger.info("In Method: runSalesforceAdvertiserLoadJob launch started on last_modified_date {}",
        lastModifiedDate);

    logger.info("Getting advertisers from SalesForce");
    try {
      getAdvertisersFromSalesforceAndAddtoDb();
    } catch (JsonSyntaxException | IOException | ParseException e) {
      logger.error("ERROR--> {}", e.getMessage());
    }
    return RepeatStatus.FINISHED;
  }
@Bean
  public JobParametersIncrementer runIdIncrementor() {
    return new RunIdIncrementer();
  }
@Bean
  public Job salesforceAdvertiserLoadJob() {
    return jobBuilderFactory.get(SalesforceJobName.salesforceAdvertiserLoadJob.name())
        .incrementer(runIdIncrementor())
        .listener(batchJobsExecutionListener)
        .start(stepsConfiguration.salesforceAdvertiserLoadStep()).build();
  }

有没有办法阻止新作业实例从先前作业实例获取参数?

1 个答案:

答案 0 :(得分:0)

我认为您没有向JobParametersIncrementer提供JobBuilder。示例:

Job job = jobBuilderFactory.get(jobName)
    .incrementer(new RunIdIncrementer())
    .start(step)
    .end()
    .build();