Retrier: The guava-retrying module provides a [general purpose method] for retrying java arbitary code with specific stop,retry and exception handling capabilities that are enhanced by Gauva's predicate matching.
1) API calls,Network calls are failing are normal part of our programs. We handle them in Application specific ways.
2) Many times,we need to [retry] the operations before logging it as failure.
3) Data Fetcher ----------> Remote Server
For demonstration, we have a Remote Server from where we need to fetch the data.
It could be [streaming] the data or a [REST API] call or any other [RPC] call.
static class UrlFetcher implements Callable<Boolean>{
private String url;
private String opMode;
UrlFetcher(String url,String opMode){
this.url = url;
this.opMode = opMode;
}
@Override
public Boolean call() throws Exception{
if("fail".equals(opMode)){
throw new TimeoutException("Connection timed out");
}
return true;
}
}
Retryer<Boolean> retrier = RetryerBuilder.<Boolean>newBuilder()
.RetryIfExceptionOfType(TimeoutException.class)
.retryIfRuntimeException()
.withStopStrategy(StopStrategies.stopAfterAttempt(3))
.build();
try{
retrier.call(new UrlFetcher("http://www.google.com","normal"));
retrier.call(new UrlFetcher("http://www.google.com","fail"));
} catch(RetryException e){
e.printStackTrace();
} catch(ExecutionException e){
e.printStackTrace();
}
4) a) We need to create retryer using builder.
b) We can specify the conditons on which to retry.
c) Here we have specified RuntimeException and TimeoutException, if there is any exception,it won't retry.
d) We also need to specify strategy to stop. Here we have specified,retry 3 times and then abort.
5) Wait Strategy:
------------------
1) Here we retried immediately after failure, what if we want to change the behaviour.
.withWaitStrategy(WaitStrategies.exponentailWait(1000,5,TimeUnit.SECONDS))
1) API calls,Network calls are failing are normal part of our programs. We handle them in Application specific ways.
2) Many times,we need to [retry] the operations before logging it as failure.
3) Data Fetcher ----------> Remote Server
For demonstration, we have a Remote Server from where we need to fetch the data.
It could be [streaming] the data or a [REST API] call or any other [RPC] call.
static class UrlFetcher implements Callable<Boolean>{
private String url;
private String opMode;
UrlFetcher(String url,String opMode){
this.url = url;
this.opMode = opMode;
}
@Override
public Boolean call() throws Exception{
if("fail".equals(opMode)){
throw new TimeoutException("Connection timed out");
}
return true;
}
}
Retryer<Boolean> retrier = RetryerBuilder.<Boolean>newBuilder()
.RetryIfExceptionOfType(TimeoutException.class)
.retryIfRuntimeException()
.withStopStrategy(StopStrategies.stopAfterAttempt(3))
.build();
try{
retrier.call(new UrlFetcher("http://www.google.com","normal"));
retrier.call(new UrlFetcher("http://www.google.com","fail"));
} catch(RetryException e){
e.printStackTrace();
} catch(ExecutionException e){
e.printStackTrace();
}
4) a) We need to create retryer using builder.
b) We can specify the conditons on which to retry.
c) Here we have specified RuntimeException and TimeoutException, if there is any exception,it won't retry.
d) We also need to specify strategy to stop. Here we have specified,retry 3 times and then abort.
5) Wait Strategy:
------------------
1) Here we retried immediately after failure, what if we want to change the behaviour.
.withWaitStrategy(WaitStrategies.exponentailWait(1000,5,TimeUnit.SECONDS))
No comments:
Post a Comment