Query about Guava ListenableFuture - java

I am writing a service that calls a few external services. I am using futures to represent the result of all those external service calls. I collapse all the futures to a single future using Futures.successfulAsList() method provided by Guava library.
Here is my code
List<ListenableFuture<List<T>>> futureList = new ArrayList<>();
for(int id: shardIds) {
ListeningExecutorService service =
(ListeningExecutorService) _shardMgr.getExecutorService(id);
SelectTask task = new SelectTask(_shardMgr.getReadHandle(id), sql, mapper);
ListenableFuture<List<T>> future = service.submit(task);
//Add Callback
Futures.addCallback(future, new ErrorCallBack(task),
Executors.newFixedThreadPool(1));
futureList.add(future);
}
ListenableFuture<List<List<T>>> combinedFuture =
Futures.successfulAsList(futureList);
int timeout = _dbTimeout.get();
List<T> selectResult = new ArrayList<T>();
try {
List<List<T>> result = combinedFuture.get(timeout, TimeUnit.MILLISECONDS);
for(List<T> sublist: result) {
for(T t : sublist) {
//TODO: Do we want to put a cap on how many results we return here?
//I think we should
selectResult.add(t);
}
}
}
catch(Exception ex) {
log.error("******************* Exception in parallelSelect ",ex);
throw new RuntimeException("Error in parallelSelect");
}
When one of my future ( external service call ) fails ErrorCallBack's onFailure() is called, But i still get past of combinedFuture.get(timeout, TimeUnit.MILLISECONDS); and i get NullPointerException in line for(T t : sublist) ... while iterating over the results.
I expect that when one external service call fails, i should not get past combinedFuture.get()
Am i doing something wrong ? I even tried to throw Exception from ErrorCallBack's onFailure method.
Here is ErrorCallBack's implementation
private class ErrorCallBack<T> implements FutureCallback<List<T>> {
private final SelectTask _task;
public ErrorCallBack(SelectTask task) {
_task = task;
}
#Override
public void onFailure(Throwable t) {
log.error("ErrorCallBack:onFailure(). Enter");
DBErrorType type = DBErrorType.UNKNOWN;
try {
log.error("ErrorCallBack:onFailure(). Exception ",t);
if(t instanceof InterruptedException || t instanceof CancellationException) {
type = DBErrorType.UNKNOWN;
} else if ( t instanceof SQLException || t.getCause() instanceof SQLException) {
type = DBErrorType.SQL_SYNTAX_ERROR;
} else if ( t instanceof MySQLSyntaxErrorException || t.getCause() instanceof MySQLSyntaxErrorException) {
type = DBErrorType.SQL_SYNTAX_ERROR;
} else if ( t instanceof ExecutionException) {
type = DBErrorType.SQL_SYNTAX_ERROR;
} else if (t instanceof TimeoutException) {
type = DBErrorType.NETWORK_ERROR;
} else {
type = DBErrorType.UNKNOWN;
}
ShardHandle handle = _task.getShardHandle();
_shardMgr.reportException(handle, type);
DBException exception = new DBException(handle.getShardInfo(), type, ErrorSeverity.CRITICAL, t);
_alertModule.handleAlert(exception.getAlertContext());
} catch( Exception ex) {
}
}
#Override
public void onSuccess(List<T> result) {}
}

I expect that when one external service call fails, i should not get past combinedFuture.get()
Well, no, since you're calling Futures.succcessfulAsList() which, as its name implies, returns the results of the successful Futures (and null for those that failed). For the behaviour you want, you should call Futures.allAsList() which gives you a Future that fails if any of its components fail.
Since you're not checking for nulls in the results, you get the NPE.

Related

Java Call method with return type and method to be called

I have lots of calls against a REST API that i want handled the same way.
Perform call, check if not authenticated. Refresh Token call again or if we hit rate
limit despite rate limit function. Sleep and perform call again.
I would like to wrap this in a function that can be called in the way
ReturnType returnVal= handleIntegration(ReturnType , functionToBecCalled)
How can this be achieved?
For the example below something like
CustomersResponse customersReponse = handleIntegration(CustomersResponse , connection.customers.findCustomersResponse())
EmployeesReponse employeesRepsonse = handleIntegration(EmployeeResponse , connection.employees.findEmployeesResponse())
//Current Code
bucket.consume();
CustomersResponse customersResponse = null;
try {
customersResponse = connection.customers.findCustomersResponse();
} catch (IntegrationException e) {
if (e.getStatusCode() == 401) {
this.newAccessFromRefreshToken();
customersResponse = connection.customers.findCustomersResponse();
}else if (e.getStatusCode() == 429){
Thread.sleep(500);
customersResponse = connection.customers.findCustomersResponse();
}else
throw e;
}
bucket.consume();
EmployeeResponse employeeResponse = null;
try {
employeeResponse = connection.employees.findEmployeesResponse();
} catch (IntegrationException e) {
if (e.getStatusCode() == 401) {
this.newAccessFromRefreshToken();
employeeResponse = connection.employees.findEmployeesResponse();
}else if (e.getStatusCode() == 429){
Thread.sleep(500);
employeeResponse = connection.employees.findEmployeesResponse();
}else
throw e;
}
You could try something like the following with the help of Java 8+ Functional Interfaces and Generics:
public <T> T handleIntegration(Supplier<T> supplier) {
bucket.consume();
T result = null;
try {
result = supplier.get();
} catch (IntegrationException e) {
if (e.getStatusCode() == 401) {
this.newAccessFromRefreshToken();
result = supplier.get();
} else if (e.getStatusCode() == 429) {
Thread.sleep(500);
result = supplier.get();
} else
throw e;
}
return result;
}
Then you could call the method like this:
CustomersResponse returnVal = handleIntegration(connection.customers::findCustomersResponse)
If you are at least on Java 8 with lambdas, you can do it like this:
<T> T handleIntegration(Supplier<T> method) {
bucket.consume(); // I don't know how this fits - BEWARE!
T response = null;
try {
response = method.get();
} catch (IntegrationException e) {
if (e.getStatusCode() == 401) {
this.newAccessFromRefreshToken();
response = method.get();
} else if (e.getStatusCode() == 429){
Thread.sleep(500);
response = method.get();
} else
throw e;
}
}
// I don't know what you want to do with the response, returning it here for example
return response;
}
And use it e.g. as:
CustomersResponse customersReponse = handleIntegration(
CustomersResponse.class,
() -> connection.customers.findCustomersResponse()
);
Caveat: I assumed IntegrationException is an unchecked exception. If it is checked or in any case, e.g. connection.customers.findCustomersResponse() throws a checked exception, the provided java.util.function.Supplier will not do. You will have to provide a functional interface that throws the specific checked exception.
Unrelated Note: Thread.sleep() is easy, but you may want to consider a better way, because sleep blocks the thread.

Propagating information in completablefuture failure

I'm using completable futures to do a bunch of thing X. X talks to the internet and can either fail or not fail. When I invoke X I pass it a value, let's call it value. X(value).
private void X(String value) {
CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(()-> {
try {
Object response = talkToExternalThing(value);
} catch (InterruptedException e) {
throw new CompletionException(e.getCause());
}
return true;
}).exceptionally(ex -> false);
futures.add(future);
}
Above is a snippet of what I'm playing with. When it comes down to analyzing the result-set, I can see all values that failed/didn't fail in my test (ie. true or false).
Map<Boolean, List<CompletableFuture<Boolean>>> result = futures.stream()
.collect(Collectors.partitioningBy(CompletableFuture::isCompletedExceptionally));
My problem is, I want to not only know if it failed or did not fail, but I also want other metadata, such as the value that caused the failure. My hope is to potentially have an exception object I can analyze as a result. It's worth noting that the exception is a checked exception (interrupt).
This would be my suggestion:
ExecutorService executorService = Executors.newCachedThreadPool();
private void X(String value) {
CompletableFuture<Pair<Boolean, String>> future = new CompletableFuture<>();
executorService.execute(() -> {
try {
Object response = talkToExternalThing(value);
} catch (InterruptedException e) {
// un-successfully, with the value
future.complete(new Pair<>(false, value));
return;
}
// successfully, with the value
future.complete(new Pair<>(true, value));
});
futures.add(future);
}

How to call multiple methods asynchrounosly with different implementation

I am trying to call one method 12 times asynchronously. But before the call I am setting something different for each method call. How can I do this in a more elegant way.
I am using spring as well.
I am aware of #async but how can I change the body 12 times ?
Callable<Object> task4 = () -> {
CallContextHolder.setContext(callContext);
try {
Object m = dbQuery(userId);
if (m == null){
throw new RuntimeException();
}
return m;
}
catch (Exception e) {
throw new IllegalStateException("task interrupted", e);
}
};
Callable<Object> task5 = () -> {
CallContextHolder.setContext(callContext); //here is the difference in every task
try {
Object m = dbQuery(userId);
if (m == null){
throw new RuntimeException();
}
return m;
}
catch (Exception e) {
throw new IllegalStateException("task interrupted", e);
}
You can use something like the following method
public Callable<Object> getCallable(CallContext context, String userId) { //replace types fro parameters to appropriate
return () -> {
CallContextHolder.setContext(callContext);
try {
Object m = dbQuery(userId);
if (m == null){
throw new RuntimeException();
}
return m;
}
catch (Exception e) {
throw new IllegalStateException("task interrupted", e);
}
};
}
And use it like this
Callable<Object> call1 = getCallable(callContext, userId);
Callable<Object> call2 = getCallable(callContext, userId);
You can try to use some type of loop to generate those callables and store them in a list.

How do you implement a re-try-catch?

Try-catch is meant to help in the exception handling. This means somehow that it will help our system to be more robust: try to recover from an unexpected event.
We suspect something might happen when executing and instruction (sending a message), so it gets enclosed in the try. If that something nearly unexpected happens, we can do something: we write the catch. I don't think we called to just log the exception. I thing the catch block is meant to give us the opportunity of recovering from the error.
Now, let's say we recover from the error because we could fix what was wrong. It could be super nice to do a re-try:
try{ some_instruction(); }
catch (NearlyUnexpectedException e){
fix_the_problem();
retry;
}
This would quickly fall in the eternal loop, but let's say that the fix_the_problem returns true, then we retry. Given that there is no such thing in Java, how would YOU solve this problem? What would be your best design code for solving this?
This is like a philosophical question, given that I already know what I'm asking for is not directly supported by Java.
You need to enclose your try-catch inside a while loop like this: -
int count = 0;
int maxTries = 3;
while(true) {
try {
// Some Code
// break out of loop, or return, on success
} catch (SomeException e) {
// handle exception
if (++count == maxTries) throw e;
}
}
I have taken count and maxTries to avoid running into an infinite loop, in case the exception keeps on occurring in your try block.
Obligatory "enterprisy" solution:
public abstract class Operation {
abstract public void doIt();
public void handleException(Exception cause) {
//default impl: do nothing, log the exception, etc.
}
}
public class OperationHelper {
public static void doWithRetry(int maxAttempts, Operation operation) {
for (int count = 0; count < maxAttempts; count++) {
try {
operation.doIt();
count = maxAttempts; //don't retry
} catch (Exception e) {
operation.handleException(e);
}
}
}
}
And to call:
OperationHelper.doWithRetry(5, new Operation() {
#Override public void doIt() {
//do some stuff
}
#Override public void handleException(Exception cause) {
//recover from the Exception
}
});
As usual, the best design depends on the particular circumstances. Usually though, I write something like:
for (int retries = 0;; retries++) {
try {
return doSomething();
} catch (SomeException e) {
if (retries < 6) {
continue;
} else {
throw e;
}
}
}
You can use AOP and Java annotations from jcabi-aspects (I'm a developer):
#RetryOnFailure(attempts = 3, delay = 5)
public String load(URL url) {
return url.openConnection().getContent();
}
You could also use #Loggable and #LogException annotations.
Although try/catch into while is well-known and good strategy I want to suggest you recursive call:
void retry(int i, int limit) {
try {
} catch (SomeException e) {
// handle exception
if (i >= limit) {
throw e; // variant: wrap the exception, e.g. throw new RuntimeException(e);
}
retry(i++, limit);
}
}
Spring AOP and annotation based solution:
Usage (#RetryOperation is our custom annotation for the job):
#RetryOperation(retryCount = 1, waitSeconds = 10)
boolean someMethod() throws Exception {
}
We'll need two things to accomplish this: 1. an annotation interface, and 2. a spring aspect. Here's one way to implement these:
The Annotation Interface:
import java.lang.annotation.*;
#Target(ElementType.METHOD)
#Retention(RetentionPolicy.RUNTIME)
public #interface RetryOperation {
int retryCount();
int waitSeconds();
}
The Spring Aspect:
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
#Aspect #Component
public class RetryAspect {
private static final Logger LOGGER = LoggerFactory.getLogger(RetryAspect.class);
#Around(value = "#annotation(RetryOperation)")
public Object retryOperation(ProceedingJoinPoint joinPoint) throws Throwable {
Object response = null;
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
RetryOperation annotation = method.getAnnotation(RetryOperation.class);
int retryCount = annotation.retryCount();
int waitSeconds = annotation.waitSeconds();
boolean successful = false;
do {
try {
response = joinPoint.proceed();
successful = true;
} catch (Exception ex) {
LOGGER.info("Operation failed, retries remaining: {}", retryCount);
retryCount--;
if (retryCount < 0) {
throw ex;
}
if (waitSeconds > 0) {
LOGGER.info("Waiting for {} second(s) before next retry", waitSeconds);
Thread.sleep(waitSeconds * 1000l);
}
}
} while (!successful);
return response;
}
}
Most of these answers are essentially the same. Mine is also, but this is the form I like
boolean completed = false;
Throwable lastException = null;
for (int tryCount=0; tryCount < config.MAX_SOME_OPERATION_RETRIES; tryCount++)
{
try {
completed = some_operation();
break;
}
catch (UnlikelyException e) {
lastException = e;
fix_the_problem();
}
}
if (!completed) {
reportError(lastException);
}
Use a while loop with local status flag. Initialize the flag as false and set it to true when operation is successful e.g. below:
boolean success = false;
while(!success){
try{
some_instruction();
success = true;
} catch (NearlyUnexpectedException e){
fix_the_problem();
}
}
This will keep retrying until its successful.
If you want to retry only certain number of times then use a counter as well:
boolean success = false;
int count = 0, MAX_TRIES = 10;
while(!success && count++ < MAX_TRIES){
try{
some_instruction();
success = true;
} catch (NearlyUnexpectedException e){
fix_the_problem();
}
}
if(!success){
//It wasn't successful after 10 retries
}
This will try max 10 times if not successful until then an will exit if its successful before hand.
This is an old question but a solution is still relevant. Here is my generic solution in Java 8 without using any third party library:
public interface RetryConsumer<T> {
T evaluate() throws Throwable;
}
public interface RetryPredicate<T> {
boolean shouldRetry(T t);
}
public class RetryOperation<T> {
private RetryConsumer<T> retryConsumer;
private int noOfRetry;
private int delayInterval;
private TimeUnit timeUnit;
private RetryPredicate<T> retryPredicate;
private List<Class<? extends Throwable>> exceptionList;
public static class OperationBuilder<T> {
private RetryConsumer<T> iRetryConsumer;
private int iNoOfRetry;
private int iDelayInterval;
private TimeUnit iTimeUnit;
private RetryPredicate<T> iRetryPredicate;
private Class<? extends Throwable>[] exceptionClasses;
private OperationBuilder() {
}
public OperationBuilder<T> retryConsumer(final RetryConsumer<T> retryConsumer) {
this.iRetryConsumer = retryConsumer;
return this;
}
public OperationBuilder<T> noOfRetry(final int noOfRetry) {
this.iNoOfRetry = noOfRetry;
return this;
}
public OperationBuilder<T> delayInterval(final int delayInterval, final TimeUnit timeUnit) {
this.iDelayInterval = delayInterval;
this.iTimeUnit = timeUnit;
return this;
}
public OperationBuilder<T> retryPredicate(final RetryPredicate<T> retryPredicate) {
this.iRetryPredicate = retryPredicate;
return this;
}
#SafeVarargs
public final OperationBuilder<T> retryOn(final Class<? extends Throwable>... exceptionClasses) {
this.exceptionClasses = exceptionClasses;
return this;
}
public RetryOperation<T> build() {
if (Objects.isNull(iRetryConsumer)) {
throw new RuntimeException("'#retryConsumer:RetryConsumer<T>' not set");
}
List<Class<? extends Throwable>> exceptionList = new ArrayList<>();
if (Objects.nonNull(exceptionClasses) && exceptionClasses.length > 0) {
exceptionList = Arrays.asList(exceptionClasses);
}
iNoOfRetry = iNoOfRetry == 0 ? 1 : 0;
iTimeUnit = Objects.isNull(iTimeUnit) ? TimeUnit.MILLISECONDS : iTimeUnit;
return new RetryOperation<>(iRetryConsumer, iNoOfRetry, iDelayInterval, iTimeUnit, iRetryPredicate, exceptionList);
}
}
public static <T> OperationBuilder<T> newBuilder() {
return new OperationBuilder<>();
}
private RetryOperation(RetryConsumer<T> retryConsumer, int noOfRetry, int delayInterval, TimeUnit timeUnit,
RetryPredicate<T> retryPredicate, List<Class<? extends Throwable>> exceptionList) {
this.retryConsumer = retryConsumer;
this.noOfRetry = noOfRetry;
this.delayInterval = delayInterval;
this.timeUnit = timeUnit;
this.retryPredicate = retryPredicate;
this.exceptionList = exceptionList;
}
public T retry() throws Throwable {
T result = null;
int retries = 0;
while (retries < noOfRetry) {
try {
result = retryConsumer.evaluate();
if (Objects.nonNull(retryPredicate)) {
boolean shouldItRetry = retryPredicate.shouldRetry(result);
if (shouldItRetry) {
retries = increaseRetryCountAndSleep(retries);
} else {
return result;
}
} else {
// no retry condition defined, no exception thrown. This is the desired result.
return result;
}
} catch (Throwable e) {
retries = handleException(retries, e);
}
}
return result;
}
private int handleException(int retries, Throwable e) throws Throwable {
if (exceptionList.contains(e.getClass()) || (exceptionList.isEmpty())) {
// exception is excepted, continue retry.
retries = increaseRetryCountAndSleep(retries);
if (retries == noOfRetry) {
// evaluation is throwing exception, no more retry left. Throw it.
throw e;
}
} else {
// unexpected exception, no retry required. Throw it.
throw e;
}
return retries;
}
private int increaseRetryCountAndSleep(int retries) {
retries++;
if (retries < noOfRetry && delayInterval > 0) {
try {
timeUnit.sleep(delayInterval);
} catch (InterruptedException ignore) {
Thread.currentThread().interrupt();
}
}
return retries;
}
}
Let's have a test case like:
#Test
public void withPredicateAndException() {
AtomicInteger integer = new AtomicInteger();
try {
Integer result = RetryOperation.<Integer>newBuilder()
.retryConsumer(() -> {
int i = integer.incrementAndGet();
if (i % 2 == 1) {
throw new NumberFormatException("Very odd exception");
} else {
return i;
}
})
.noOfRetry(10)
.delayInterval(10, TimeUnit.MILLISECONDS)
.retryPredicate(value -> value <= 6)
.retryOn(NumberFormatException.class, EOFException.class)
.build()
.retry();
Assert.assertEquals(8, result.intValue());
} catch (Throwable throwable) {
Assert.fail();
}
}
A simple way to solve the issue would be to wrap the try/catch in a while loop and maintain a count. This way you could prevent an infinite loop by checking a count against some other variable while maintaining a log of your failures. It isn't the most exquisite solution, but it would work.
In case it's useful, a couple more options to consider, all thrown together (stopfile instead of retries, sleep, continue larger loop) all possibly helpful.
bigLoop:
while(!stopFileExists()) {
try {
// do work
break;
}
catch (ExpectedExceptionType e) {
// could sleep in here, too.
// another option would be to "restart" some bigger loop, like
continue bigLoop;
}
// ... more work
}
If not all exceptions warrant a retry, only some. And if at least one try has to be made, Here is an alternative utility method:
void runWithRetry(Runnable runnable, Class<Exception> exClass, int maxRetries) {
Exception err = null;
do {
maxRetries--;
try {
runnable.run();
err = null;
} catch (Exception e) {
if(exClass.isAssignableFrom(e.getClass())){
err = e;
}else {
throw e;
}
}
} while (err != null && maxRetries > 0);
if (err != null) {
throw err;
}
}
Usage:
runWithRetry(() -> {
// do something
}, TimeoutException.class, 5)
All a Try-Catch does is allow your program to fail gracefully. In a catch statement, you generally try to log the error, and maybe roll back changes if you need to.
bool finished = false;
while(finished == false)
{
try
{
//your code here
finished = true
}
catch(exception ex)
{
log.error("there was an error, ex");
}
}
Use a do-while to design re-try block.
boolean successful = false;
int maxTries = 3;
do{
try {
something();
success = true;
} catch(Me ifUCan) {
maxTries--;
}
} while (!successful || maxTries > 0)
Here a reusable and more generic approach for Java 8+ that does not require external libraries:
public interface IUnreliable<T extends Exception>
{
void tryRun ( ) throws T;
}
public static <T extends Exception> void retry (int retryCount, IUnreliable<T> runnable) throws T {
for (int retries = 0;; retries++) {
try {
runnable.tryRun();
return;
} catch (Exception e) {
if (retries < retryCount) {
continue;
} else {
throw e;
}
}
}
}
Usage:
#Test
public void demo() throws IOException {
retry(3, () -> {
new File("/tmp/test.txt").createNewFile();
});
}
You can use https://github.com/bnsd55/RetryCatch
Example:
RetryCatch retryCatchSyncRunnable = new RetryCatch();
retryCatchSyncRunnable
// For infinite retry times, just remove this row
.retryCount(3)
// For retrying on all exceptions, just remove this row
.retryOn(ArithmeticException.class, IndexOutOfBoundsException.class)
.onSuccess(() -> System.out.println("Success, There is no result because this is a runnable."))
.onRetry((retryCount, e) -> System.out.println("Retry count: " + retryCount + ", Exception message: " + e.getMessage()))
.onFailure(e -> System.out.println("Failure: Exception message: " + e.getMessage()))
.run(new ExampleRunnable());
Instead of new ExampleRunnable() you can pass your own anonymous function.
Simplifying #ach's previous solution into one file and using functional interfaces.
public class OperationHelper {
public static void doWithRetry(int maxAttempts, Runnable operation, Consumer<Exception> handle) {
for (int count = 0; count < maxAttempts; count++) {
try {
operation.run();
count = maxAttempts; //don't retry
} catch (Exception e) {
handle.accept(e);
}
}
}
}
simple
int MAX = 3;
int count = 0;
while (true) {
try {
...
break;
} catch (Exception e) {
if (count++ < MAX) {
continue;
}
...
break;
}
}
https://onlinegdb.com/a-7RsL1Gh
public void doSomething() throws Exception{
final int MAX_TRIES = 10;
int count = 0;
while(count++ < MAX_TRIES){
try{
System.out.println("trying");
causeIssue(count); // throws error/exception till count 2
System.out.println("trying successful");
break; // break on success
} catch (Exception e){
System.out.println("caught, logging Exception:" + count);
} catch (Error e){
System.out.println("caught, logging Error:" + count);
}
}
}
Output:
trying
caught, logging Error:1
trying
caught, logging Error:2
trying
trying successful
I know there are already many similar answers here, and mine is not much different, but I will post it anyway because it deals with a specific case/issue.
When dealing with the facebook Graph API in PHP you sometimes get an error, but immediately re-trying the same thing will give a positive result (for various magical Internet reasons that are beyond the scope of this question). In this case there is no need to fix any error, but to simply try again because there was some kind of "facebook error".
This code is used immediately after creating a facebook session:
//try more than once because sometimes "facebook error"
$attempt = 3;
while($attempt-- > 0)
{
// To validate the session:
try
{
$facebook_session->validate();
$attempt = 0;
}
catch (Facebook\FacebookRequestException $ex)
{
// Session not valid, Graph API returned an exception with the reason.
if($attempt <= 0){ echo $ex->getMessage(); }
}
catch (\Exception $ex)
{
// Graph API returned info, but it may mismatch the current app or have expired.
if($attempt <= 0){ echo $ex->getMessage(); }
}
}
Also, by having the for loop count down to zero ($attempt--) it makes it pretty easy to change the number of attempts in the future.
following is my solution with very simple approach!
while (true) {
try {
/// Statement what may cause an error;
break;
} catch (Exception e) {
}
}
Im not sure if this is the "Professional" way to do it and i'm not entirely sure if it works for everything.
boolean gotError = false;
do {
try {
// Code You're Trying
} catch ( FileNotFoundException ex ) {
// Exception
gotError = true;
}
} while ( gotError = true );
https://github.com/tusharmndr/retry-function-wrapper/tree/master/src/main/java/io
int MAX_RETRY = 3;
RetryUtil.<Boolean>retry(MAX_RETRY,() -> {
//Function to retry
return true;
});
The issue with the remaining solutions is that, the correspondent function tries continuously without a time interval in-between, thus over flooding the stack.
Why not just trying only every second and ad eternum?
Here a solution using setTimeout and a recursive function:
(function(){
try{
Run(); //tries for the 1st time, but Run() as function is not yet defined
}
catch(e){
(function retry(){
setTimeout(function(){
try{
console.log("trying...");
Run();
console.log("success!");
}
catch(e){
retry(); //calls recursively
}
}, 1000); //tries every second
}());
}
})();
//after 5 seconds, defines Run as a global function
var Run;
setTimeout(function(){
Run = function(){};
}, 5000);
Replace the function Run() by the function or code that you'd like to retry every second.
Give it a try using springs #Retryable annotation , the below method will retry for 3 attempts when RuntimeException occurs
#Retryable(maxAttempts=3,value= {RuntimeException.class},backoff = #Backoff(delay = 500))
public void checkSpringRetry(String str) {
if(StringUtils.equalsIgnoreCase(str, "R")) {
LOGGER.info("Inside retry.....!!");
throw new RuntimeException();
}
}
Below snippet execute some code snippet. If you got any error while executing the code snippet, sleep for M milliseconds and retry. Reference link.
public void retryAndExecuteErrorProneCode(int noOfTimesToRetry, CodeSnippet codeSnippet, int sleepTimeInMillis)
throws InterruptedException {
int currentExecutionCount = 0;
boolean codeExecuted = false;
while (currentExecutionCount < noOfTimesToRetry) {
try {
codeSnippet.errorProneCode();
System.out.println("Code executed successfully!!!!");
codeExecuted = true;
break;
} catch (Exception e) {
// Retry after 100 milliseconds
TimeUnit.MILLISECONDS.sleep(sleepTimeInMillis);
System.out.println(e.getMessage());
} finally {
currentExecutionCount++;
}
}
if (!codeExecuted)
throw new RuntimeException("Can't execute the code within given retries : " + noOfTimesToRetry);
}
Here is my solution similar to some others can wrap a function, but allows you to get the functions return value, if it suceeds.
/**
* Wraps a function with retry logic allowing exceptions to be caught and retires made.
*
* #param function the function to retry
* #param maxRetries maximum number of retires before failing
* #param delay time to wait between each retry
* #param allowedExceptionTypes exception types where if caught a retry will be performed
* #param <V> return type of the function
* #return the value returned by the function if successful
* #throws Exception Either an unexpected exception from the function or a {#link RuntimeException} if maxRetries is exceeded
*/
#SafeVarargs
public static <V> V runWithRetriesAndDelay(Callable<V> function, int maxRetries, Duration delay, Class<? extends Exception>... allowedExceptionTypes) throws Exception {
final Set<Class<? extends Exception>> exceptions = new HashSet<>(Arrays.asList(allowedExceptionTypes));
for(int i = 1; i <= maxRetries; i++) {
try {
return function.call();
} catch (Exception e) {
if(exceptions.contains(e.getClass())){
// An exception of an expected type
System.out.println("Attempt [" + i + "/" + maxRetries + "] Caught exception [" + e.getClass() + "]");
// Pause for the delay time
Thread.sleep(delay.toMillis());
}else {
// An unexpected exception type
throw e;
}
}
}
throw new RuntimeException(maxRetries + " retries exceeded");
}
This Solution allows you to configure a reusable functionality for retrying based on certain exception without using any external libraries
// Create a Function that suits your need .
#FunctionalInterface
public interface ThrowableBiFunction<U,T,R> {
R apply(U u ,T t) throws Exception;
}
//Here's the crux of the solution
public interface ExceptionRetryable<T, U, R> {
int getRetries();
List<Class<? extends Exception>> getRetryableExceptions();
default R execute(ThrowableBiFunction<T, U, R> function, T t, U u) throws Exception {
int numberOfRetries = getRetries();
return execute(function, t, u, numberOfRetries);
}
default R execute(ThrowableBiFunction<T, U, R> function, T t, U u, int retryCount) throws Exception {
try {
log.info(" Attempting to execute ExceptionRetryable#execute ,Number of remaining retries {} ",retryCount);
return function.apply(t, u);
} catch (Exception e) {
log.info(" error occurred in ExceptionRetryable#execute",e);
if (retryCount == 0)
throw e;
for (Class exp : getRetryableExceptions()) {
if (e.getClass() == exp) {
return execute(function, t, u, retryCount - 1);
}
}
throw e;
}
}
}
// create an implementation for exception retryable
public class TestRetryable implements ExceptionRetryable<String, String, List<String>> {
#Override
public int getRetries() {
return 10;
}
#Override
public List<Class<? extends Exception>> getRetryableExceptions() {
return Arrays.asList(new Exception1().getClass(), new Exception2().getClass());
;
}
}
// Finally create a ThrowableBiFunction that encapsulates that piece of code that needs to be retried on exception and an instance of ExceptionRetryable
TestRetryable retryable = new TestRetryable();
ThrowableBiFunction<Integer,Long, String> testRetrablefcn = { i, l ->
// your code goes here
};
Integer i = 0;
Long l = 1l;
String output = testRetrablefcn.execute(testRetrablefcn,i,l);
Production ready code:
#FunctionalInterface
public interface Operation {
void doCall() throws IOException;
default void handleException(Exception e) {
//Your custom default implementation
}
public class OperationHelper {
public static void doWithRetry(int maxAttempts, Operation operation) {
for (int count = 0; count <= maxAttempts; count++) {
try {
operation.doCall();
return;
} catch (Exception e) {
if (count == maxAttempts) {
e.printStackTrace();
return;
} else {
operation.handleException(e);
}
}
}
}
}
Usage with default implementation in code:
OperationHelper.doWithRetry(10,
() -> //do your job );
Usage when custom exception handle is needed:
OperationHelper.doWithRetry(10, new Operation() {
#Override public void doIt() {
//do some stuff
}
#Override public void handleException(Exception cause) {
//recover from the Exception
}
});

Java Task Execution and Reflection

I am having the code this way..
1) Invoking the updatedb method using reflection...
for (String uniqueSym : activeSymbolsSet) {
futureTaskUtil.submiteTask(new Helper(),
Helper.class.getDeclaredMethod("updateDb",
new Class<?>[] { String.class }), new Object[] { uniqueSym }, 60);
}
- futureTaskUtil:
2) My question is this updatedb is executed as an run time task...
public Object submiteTask(final Object obj, final Method method, final Object[] params, int timeoutSeconds) throws Exception {
if (null != obj && method != null) {
Callable<Object> task = new Callable<Object>() {
public Object call() {
try {
method.setAccessible(true);
Object resultObj = method.invoke(obj, params);
return resultObj;
} catch (Exception e) {
logger.fatal("Exception occured while invoking future task.", e);
}
return null;
}
};
Future<Object> future = executor.submit(task);
try {
Object result = null;
if (timeoutSeconds < 0) {
result = future.get(timoutsec, TimeUnit.SECONDS);
} else {
result = future.get(timeoutSeconds, TimeUnit.SECONDS);
}
logger.info("Result of method execution is :: " + result);
return result;
} catch (TimeoutException e) {
} catch (Exception e) {
logger.fatal("Exception occured while executing future tas : " + obj, e);
} finally {
future.cancel(true); // may or may not desire this
}
}
return null;
}
can some one explain why this is executed as an seperate task and invoking the method?
Future<Object> future = executor.submit(task); This is your culprit You can read about Executer Framework here
What if the method never returns? Then the application would hang at this point.
Using this wrapper, the method will be canceled after 60 seconds.

Categories