http://docs.spring.io/spring-batch/trunk/reference/html/retry.html
To make processing more robust and less prone to failure, sometimes it helps to automatically retry a failed operation in case it might succeed on a subsequent attempt. Errors that are susceptible to this kind of treatment are transient in nature.
http://fahdshariff.blogspot.com/2013/02/retrying-operations-using-springs.html
http://www.mscharhag.com/spring/spring-retry
@EnableRetry
http://ilkerkonar.blogspot.com/2015/01/spring-retry-sample.html
[Java Tip] Retrying operation with Guava Retrying
retrier.call(new UrlFetcher("http://www.google.com", "normal"));
retrier.call(new UrlFetcher("http://www.doesnotexist.com", "fail"));
To make processing more robust and less prone to failure, sometimes it helps to automatically retry a failed operation in case it might succeed on a subsequent attempt. Errors that are susceptible to this kind of treatment are transient in nature.
http://fahdshariff.blogspot.com/2013/02/retrying-operations-using-springs.html
If your application is using Spring then it is easier to use the Spring Framework's
RetryTemplate
.
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 classes import 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 template final 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 template template.execute( new RetryCallback<Remote>() { @Override public Remote doWithRetry( final RetryContext context) throws Exception { } }); |
http://www.mscharhag.com/spring/spring-retry
@EnableRetry
@Service
public
class
MyService {
@Retryable
(value = {FooException.
class
, BarException.
class
}, maxAttempts =
5
)
public
void
retryWithException() {
// perform operation that is likely to fail
}
@Recover
public
void
recover(FooException exception) {
// recover from FooException
}
}
Here we tell Spring to apply retry handling only if a Exception of type FooException or BarException is thrown. Other exceptions will not cause a retry. maxAttempts = 5 tells Spring to retry the method up to 5 times if it fails.
With @Recover we define a separate recovery method for FooException. This allows us to run special recovery code when a retryable method fails with FooException.
Indeed, ExceptionClassifierRetryPolicy is the way to go. I didn't manage to make it work with the policyMap though. Here is how I've used it :
@Component("yourRetryPolicy")
public class YourRetryPolicy extends ExceptionClassifierRetryPolicy
{
@PostConstruct
public void init()
{
final SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
simpleRetryPolicy.setMaxAttempts( 3 );
this.setExceptionClassifier( new Classifier<Throwable, RetryPolicy>()
{
@Override
public RetryPolicy classify( Throwable classifiable )
{
if ( classifiable instanceof YourException )
{
return new NeverRetryPolicy();
}
// etc...
return simpleRetryPolicy;
}
} );
}
Then, you just have to set it on the retry template :
@Autowired
@Qualifier("yourRetryPolicy")
private YourRetryPolicy yourRetryPolicy;
//...
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setRetryPolicy( yourRetryPolicy );
Cassandra: Datastax Java driver retry policyCluster cluster = Cluster.builder().addContactPoint("127.0.0.1").withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE).build();
[Java Tip] Retrying operation with Guava Retrying
Retryer<Boolean> retrier = RetryerBuilder.<Boolean>newBuilder()
02.
.retryIfExceptionOfType(TimeoutException.
class
)
03.
.retryIfRuntimeException()
04.
.withStopStrategy(StopStrategies.stopAfterAttempt(
3
))
05.
.build();
retrier.call(new UrlFetcher("http://www.doesnotexist.com", "fail"));