Spring batch: Retry job if does not complete in pa

2019-03-30 12:20发布

问题:

I am working on a Spring batch application where I have used RetryTemplate with SimpleRetryPolicy.

In this application, ItemProcessor usually takes 30-35 mins to complete a particular task. But sometimes, it takes from than 2hrs to complete that same task.

Is there a way to retry my ItemProcessor, if the assigned task is not completed within given time period?

I am looking for some Java/Spring in-build functionality instead of writing my own timeout logic.

回答1:

You can define transactional-attributes to a given step. (https://docs.spring.io/spring-batch/trunk/reference/htmlsingle/#transactionAttributes)

Transaction attributes can be used to control the isolation, propagation, and timeout settings.

<step id="step1">
  <tasklet>
      <chunk reader="itemReader" writer="itemWriter" commit-interval="2"/>
      <transaction-attributes isolation="DEFAULT"
                              propagation="REQUIRED"
                              timeout="30"/>
  </tasklet>
</step>

If you're using Java configuration check https://stackoverflow.com/a/23921558/1942642.