Spring Batch Item Reader is executing only once

后端 未结 2 1287
星月不相逢
星月不相逢 2021-01-03 07:12

Trying to implement Spring batch,but facing a strange problem,Our ItemReader class is executing only once.

Here below is the detail.

相关标签:
2条回答
  • 2021-01-03 07:54

    After hours of time wasting,the problem seems to be solved now,i have configured allow-start-if-complete="true" in tasklet.Now Batch Item Reader is executing as per schedule.

    <batch:job id="csrfTokenBatchJob">
        <batch:step id="step1">
          <batch:tasklet allow-start-if-complete="true">
            <batch:chunk reader="csrfTokenReader" writer="csrfTokenWriter" commit-interval="1"></batch:chunk>
          </batch:tasklet>
        </batch:step>
      </batch:job>
    
    0 讨论(0)
  • 2021-01-03 07:56

    Spring batch records every job execution in database. Which is why spring batch need to differentiate every job run. It checks whether the job is already executed on the same day and it would not start again unless any job parameter varies from previous run or allow start if complete setting is enabled.

    OPTION1:- As mentioned above answer we can use allow-start-if-complete="true"

    OPTION2:- Always pass a job parameter which is a current date time stamp. This way job parameter value is always unique.

        JobExecution jobExecution = jobLauncher.run(reportJob, new JobParametersBuilder()
                        .addDate("now", new Date()).build());
    

    OPTION3:- Use an incrementor for example RunIdIncrementer so we do not need to make sure to pass unique job parameter every time.

        @Bean
        public Job job1(JobBuilderFactory jobs, Step s1) {
            return jobs.get("job1")
                    .incrementer(new RunIdIncrementer())
                    .flow(s1)
                    .end()
                    .build();
        }
    
    0 讨论(0)
提交回复
热议问题