Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring batch: Retry job if does not complete in particular time

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.

like image 502
Sachin Mhetre Avatar asked Apr 30 '18 10:04

Sachin Mhetre


People also ask

How do you retry a step in Spring Batch?

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.

How do I restart a spring batch job?

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.

How does Spring Batch handle exceptions?

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.

What is ExecutionContext in Spring Batch?

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.).


1 Answers

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.

like image 190
Pedro Tavares Avatar answered Sep 30 '22 13:09

Pedro Tavares