小编Sid*_*ann的帖子

Spring Batch Job 获取先前的执行参数

我正在使用 Spring Cloud Dataflow 并创建了一个包含作业的 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;
  } …
Run Code Online (Sandbox Code Playgroud)

spring batch-processing spring-batch spring-boot spring-cloud

7
推荐指数
1
解决办法
1658
查看次数