Java and Simple Retry

Java and Simple Retry

This is really nice.
Add this in pom.xml
<dependency>
    <groupId>net.jodah</groupId>
    <artifactId>failsafe</artifactId>
    <version>1.0.5</version>
</dependency>

I am retrying that on top of the result.
private RetryPolicy getPolicy()
{
    RetryPolicy retryPolicy = new RetryPolicy();
    retryPolicy.retryIf( result -> result.equals( false ) );
    retryPolicy.withDelay( 10, TimeUnit.SECONDS );
    retryPolicy.withMaxRetries( 3 );
    return retryPolicy;
}

public boolean uploadFile( String bucketName, String key, String fileToUpload )
{
    boolean result =
      Failsafe.with( this.getPolicy() ).get( () -> this.uploadFileOnce( bucketName, key, fileToUpload ) );
    return result;
}

Maybe it should be better in AKKA, crash and recover mode. But I do not have AKKA here, maybe this solution is simple and useful.

References:
https://github.com/jhalterman/failsafe#asynchronous-api-integration




猜你喜欢

转载自sillycat.iteye.com/blog/2410401