The example below shows how you can use a RetryTemplate to lookup a remote object. If the remote call fails, it will be retried five times with exponential backoff.
// import the necessary classesimport org.springframework.batch.retry.RetryCallback;import org.springframework.batch.retry.RetryContext;import org.springframework.batch.retry.backoff.ExponentialBackOffPolicy;import org.springframework.batch.retry.policy.SimpleRetryPolicy;import org.springframework.batch.retry.support.RetryTemplate;...// create the retry templatefinal RetryTemplate template = new RetryTemplate();template.setRetryPolicy(new SimpleRetryPolicy(5));final ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();backOffPolicy.setInitialInterval(1000L);template.setBackOffPolicy(backOffPolicy);// execute the operation using the retry templatetemplate.execute(new RetryCallback<Remote>() { @Override public Remote doWithRetry(final RetryContext context) throws Exception { }}); |