简体   繁体   中英

Spring batch ItemReader executing multiple times for same record

I am trying to implement Spring batch job for database cleanup.It just delete entry from a table in scheduled way.

  1. First we fetch 10 rows from table.( ItemReader )
  2. Removing these 10 entries from table( ItemWriter )

I have scheduled the batch at 15 minute interval.

When we launch the batch,surprisingly 10 thread tries to read the data from table.

Below is the configuration.

<!-- spring batch context -->
  <bean id="jobRepository"
    class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
    <property name="transactionManager" ref="batchTransactionManager" />
  </bean>

  <bean id="batchTransactionManager"
    class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

  <bean id="jobLauncher"
    class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
      <!--<property name="taskExecutor">
      <bean class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
      </property>-->
  </bean>

  <bean
    class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor">
    <property name="jobRegistry" ref="jobRegistry" />
  </bean>

  <bean id="jobRegistry"
    class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
  <!-- spring batch context -->



    <!--<bean id="completionPolicy" class="org.springframework.batch.repeat.policy.DefaultResultCompletionPolicy"/>-->

  <batch:job id="csrfTokenCleanUpBatchJob">
    <batch:step id="step">
      <tasklet>
        <chunk reader="csrfTokenReader" writer="csrfTokenWriter" commit-interval="10"></chunk>
      </tasklet>
    </batch:step>
  </batch:job>

  <!-- run every 10 seconds -->
  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
      <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="jobDetail" />
        <property name="cronExpression" value="* 0/15 * * * ?" />
      </bean>
    </property>
  </bean>

  <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass" value="com.test.oauth.batch.job.CSRFTokenJobLauncher" />
    <property name="group" value="quartz-batch" />
    <property name="jobDataAsMap">
      <map>
        <entry key="jobName" value="csrfTokenCleanUpBatchJob" />
        <entry key="jobLocator" value-ref="jobRegistry" />
        <entry key="jobLauncher" value-ref="jobLauncher" />
      </map>
    </property>
  </bean>

</beans>

It is all by design you want to process each record. The ItemWriter gets as many records as you want but is bound by the commit-interval . Yours is 1 which means each record is individually committed, I suggest you set it to 50. The processor processes each record by it self until the commit interval is reached then the writer is called. As mentioned yours is 1.

Also, make the read method of ItemReader as synchronised.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM