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.
Controlling retry with a retry policyBy default, Spring Batch lets you configure retriable exceptions and the retry count. Sometimes, retry is more complex: some exceptions deserve more attempts than others, or you want to keep retrying as long as the operation doesn't exceed a given timeout.
Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input project info. Press Next then Finish, a Spring Boot project will be created successfully.
Restart. By default , if there's an uncaught exception when processing the job, spring batch will stop the job. If the job is restarted with the same job parameters, it will pick up where it left off. The way it knows where the job status is by checking the job repository where it saves all the spring batch job status.
An ExecutionContext is a set of key-value pairs containing information that is scoped to either StepExecution or JobExecution . Spring Batch persists the ExecutionContext , which helps in cases where you want to restart a batch run (e.g., when a fatal error has occurred, etc.).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With