Spring Batch Job taking previous execution parameters
Asked Answered
A

1

8

I am using spring cloud dataflow and have created a spring cloud task which contains a job. This job has a parameter called last_modified_date, which is optional. In the code, I have specified which date to take in case last_modified_date is null, that is, it has not been passed as a parameter. The issue is that if for one instance of the job I pass the last_modified_date but for the next one I don't, it picks up the one in the last execution rather than passing it as null and getting it from the code.

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

Is there a way I can stop the new job instance from taking parameters from the previous job instance?

Absent answered 10/6, 2019 at 10:21 Comment(7)
Can you provide how you are launching the job as well as the job definition itself?Selfeffacing
@MichaelMinella I'm simply creating the bean of the job. On running the jar the job starts.Absent
Where does last_modified_date come from initially?Lawana
@PhilipWrage last_modified_date might come when someone wants to run the job for different dates. So maybe like once a month we give the last_modified_date ourselves.Absent
I mean programmatically how is the value set? Command line argument? Properties file? REST controller? Etc.Lawana
@PhilipWrage Like in the above code I have used Value annotation to fetch it from job parameters. So while running the job we pass it as a parameter. But when its not passed I assign it yesterdays date.Absent
I have similar problem I guess. Any solution ? I just posted this question #63584178Pinball
P
0

I think that you didn't provide JobParametersIncrementer to your JobBuilder. Example:

Job job = jobBuilderFactory.get(jobName)
    .incrementer(new RunIdIncrementer())
    .start(step)
    .end()
    .build();
Paulita answered 10/6, 2019 at 13:25 Comment(1)
The RunIdIncrementer will not help with @SiddhantSorann's question because the getNext method only increments run.id parameter and passes remaining JobParameters from prior execution through to next execution.Lawana

© 2022 - 2024 — McMap. All rights reserved.