In Spring Batch, when a step fails, you might want to retry the step after a delay. For scenarios where you want to add a fixed delay (such as 1 hour) between retries, you can use a RetryTemplate with a custom BackOffPolicy.
This guide will walk you through how to configure a 1-hour delay between retries using Spring Batch's retry and backoff policies.
🛠️ Configuring Retry After 1 Hour with Spring Batch
To introduce a delay between retries, you'll use RetryTemplate and a FixedBackOffPolicy. The FixedBackOffPolicy allows you to specify a fixed amount of time (in milliseconds) between retry attempts.
Example: Configuring Retry with 1-Hour Delay
java
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.retry.policy.SimpleRetryPolicy;
import org.springframework.batch.retry.support.RetryTemplate;
import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BatchConfig {
@Bean
public Step step(StepBuilderFactory stepBuilderFactory) {
return stepBuilderFactory.get("stepName")
.<InputType, OutputType>chunk(10)
.reader(reader())
.processor(processor())
.writer(writer())
.faultTolerant()
.retryPolicy(retryPolicy()) // Retry policy to handle retry attempts
.retryTemplate(retryTemplate()) // Attach the custom retry template
.build();
}
@Bean
public RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
// Setting up a simple retry policy with max 2 attempts
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(2); // Retry up to 2 times
retryTemplate.setRetryPolicy(retryPolicy);
// Setting up a fixed back-off policy with a delay of 1 hour (3600000 milliseconds)
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(3600000); // 1 hour delay between retries
retryTemplate.setBackOffPolicy(backOffPolicy);
return retryTemplate;
}
}
📚 Explanation
- Retry Policy:
- The retry policy is defined using
SimpleRetryPolicy
with a maximum of 2 attempts. The retry will be attempted once after the first failure.
- BackOff Policy:
- The back-off policy is defined using FixedBackOffPolicy, which introduces a 1-hour delay (3600000 milliseconds) between retry attempts.
- RetryTemplate:
- The RetryTemplate combines both the retry policy and the back-off policy, controlling the retry behavior.
- Usage:
- If the step fails, Spring Batch will retry after a 1-hour delay and will attempt to retry once more. If it fails again, it will stop as per the retry policy.
🧠 Key Points
FeatureApproachDescriptionRetry PolicySimpleRetryPolicy
Specifies the number of retry attempts (in this case, 2).BackOff PolicyFixedBackOffPolicy
Introduces a fixed delay (e.g., 1 hour) between retries.RetryTemplateCombines retry and back-off policiesManages retry logic with customizable policies.
🚀 Use Cases and Applications
- ✅ Transient Errors: Retry a task after a failure (e.g., network timeout) with a controlled delay, such as retrying after 1 hour.
- ✅ Rate Limiting: When interacting with external services, retrying after a delay can help prevent hitting rate limits.
- ✅ Job Recovery: In case of a failure, you can give time for external systems or resources to stabilize before retrying.
🔧 Summary
- Retry Policy: Set up a retry policy to define how many times to retry after a failure.
- BackOff Policy: Use
FixedBackOffPolicy
to specify a fixed delay (1 hour) between retry attempts. - RetryTemplate: Combines both policies to control retry behavior within your Spring Batch job.
With this configuration, you can handle retries in Spring Batch with precise control over timing, improving the resilience and stability of your batch jobs.