failed JUnit test reruns once and stops - java

My failed Junit test reruns once and stops but i am looking to have it run 3 times. I guess my annotation is screwed in some way. Pls what is the proper format for annotation. Note: I will also appreciate info about correct annotation for testing SuiteClasses. Below is my annotation
#Rule
public Retry retry = new Retry(3);
any help will be appreciated
public class HostTest {
/**
* Test method for {#link com.ibm.ws.ui.collective.internal.rest.resource.Host}.
*/
public class RetryTest {
/**
* #param i
*/
public RetryTest(int i) {
// TODO Auto-generated constructor stub
}
public class Retry extends TestRule {
private final int retryCount;
public Retry(int retryCount) {
this.retryCount = retryCount;
}
#Override
public Statement apply(Statement base, Description description) {
return statement(base, description);
}
private Statement statement(final Statement base, final Description description) {
return new Statement() {
#Override
public void evaluate() throws Throwable {
Throwable caughtThrowable = null;
// implement retry logic here
for (int i = 0; i < retryCount; i++) {
try {
base.evaluate();
return;
} catch (Throwable t) {
caughtThrowable = t;
System.err.println(description.getDisplayName() + ": run " + (i + 1) + " failed");
}
}
System.err.println(description.getDisplayName() + ": giving up after " + retryCount + " failures");
throw caughtThrowable;
}
};
}
}
}
#Test
public void sanityCheck_withHostPathsNull() {
Host h = new Host("myHost");
Map<String, Object> mapFromController = new HashMap<String, Object>();
h.setHostPaths(mapFromController);
assertFalse("it is not empty", h.getHostPaths().isEmpty());
}
}

Are you sure the failed tests were only running once? You might want to put a counter or print statement to confirm.
I'm not sure what you thought the Retry Rule would do but this code:
for (int i = 0; i < retryCount; i++) {
try {
base.evaluate();
return;
} catch (Throwable t) {
caughtThrowable = t;
System.err.println(description.getDisplayName() + ": run " + (i + 1) + " failed");
}
}
means that a test is only retried if it throws anything (meaning the test failed). Therefore, Retry(3) means to run each test and if any fail, then try running the failed tests upto 2 more times to see if it eventually passes. If after the final retry, if it still fails, then that final failure message is rethrown which will fail the test.
If you instead wanted to retry the tests over and over again even if the test passes, then you should get rid of that return statement inside the for loop. But then you have to be careful that you only rethrow the throwable at the end if it's not null. (meaning don't throw if the test passed the last time)
To make the test run over and over no matter if it passes:
#Override
public void evaluate() throws Throwable {
Throwable caughtThrowable = null;
// implement retry logic here
for (int i = 0; i < retryCount; i++) {
try {
base.evaluate();
} catch (Throwable t) {
caughtThrowable = t;
System.err.println(description.getDisplayName() + ": run " + (i + 1) + " failed");
}
}
if(caughtThrowable !=null){
System.err.println(description.getDisplayName() + ": giving up after " + retryCount + " failures");
throw caughtThrowable;
}
}

Related

Code is executed after return in recursive method

I am trying to implement retry-mechanism using a recursive method. Although the method already returned the list, the consecutive lines of code are executed as well.
I am sure I am missing something very obvious which a trained eye can spot in a second. How can I rework the recursion, so I get rid of the last return null statement. It's not needed at all.
This is the entire code:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class Retryer {
private static final int RETRY_COUNT = 3;
private static final long RETRY_DELAY = 2; // in seconds
private static int tryCounter = 0;
public Retryer() {}
public void execute() {
List<String> downloadedFiles;
try {
Callable<List<String>> c = new AsyncFileDownloader();
downloadedFiles = c.call();
} catch (Exception e) {
System.out.println("Ops! Starting retry");
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
downloadedFiles = retry(scheduledExecutorService, 0);
System.out.println("Retry ended");
if (downloadedFiles == null || downloadedFiles.isEmpty()) {
System.out.println("Sorry. Will exit now");
System.exit(0);
}
}
System.out.println("Finished successfully with " + downloadedFiles.size() + " in list.");
}
private List<String> retry (ScheduledExecutorService ses, int retryCounter) {
if (retryCounter == RETRY_COUNT) {
System.out.println("Retry limit reached. Exiting");
ses.shutdown();
return null;
}
try {
System.out.println("Retry attempt # " + (retryCounter+1) + " will start in " + RETRY_DELAY + " seconds ...");
ScheduledFuture sf = ses.schedule(new AsyncFileDownloader(), RETRY_DELAY, TimeUnit.SECONDS);
List<String> downloadedFiles = (List<String>) sf.get();
ses.shutdown();
System.out.println("In retry. success " + downloadedFiles );
return downloadedFiles;
} catch (Exception e) {
System.out.println("--recursive calling");
retry(ses, ++retryCounter);
}
System.out.println("--returning null!!! This shouldn't happen");
return null;
}
class AsyncFileDownloader implements Callable<List<String>> {
#Override
public List<String> call() throws Exception {
synchronized (this) {
tryCounter++;
System.out.println("--in pull");
if (tryCounter < 3) throw new IOException("Some bad thing happened");
List<String> retList = new ArrayList<>();
retList.add("file1");
retList.add("file1");
return retList;
}
}
}
}
And here the output:
--in pull
Ops! Starting retry
Retry attempt # 1 will start in 2 seconds ...
--in pull
--recursive calling
Retry attempt # 2 will start in 2 seconds ...
--in pull
In retry. success [file1, file1]
--returning null!!! This shouldn't happen
Retry ended
Sorry. Will exit now
If the first execution goes into the catch block, you recursively call retry. As soon as this second call returns, you continue executing the code after the catch block in the first call, leading to you hitting the print "returning null".
What you actually want is probably to return the value of the recursive call. Remember that return returns a value to the function that called another function, not necessarily the first function in a chain of calls.

How to simulate throwing an exception only once in retry with JUnit/Mockito test?

I put a simple retry because the operation can rarely fail. The simplified code is below. The method putObject can accidentally throw an exception, in this case the retry should allow to invoke this method again. Is it possible to write a JUnit test for this?
I know that with Mockito library we can force to throw an Exception invoking a method but how to force this exception to be thrown only once?
public class RetryExample {
Bucket bucket = new Bucket();
static int INTERNAL_EXCEPTION_CODE = 100;
class AException extends RuntimeException {
private static final long serialVersionUID = 1L;
int statusCode;
public int getStatusCode() {
return statusCode;
}
}
class Bucket {
public void putObject(String fileName, byte[] data) throws AException {
System.out.println("PutObject=" + fileName + " data=" + data);
}
}
public void process(String fileName, byte[] data) throws AException {
try {
retryOperation((f, d) -> bucket.putObject(f, d), fileName, data);
} catch (Exception ex) {
throw new AException("Failed to write data", ex);
}
}
private <T, U> void retryOperation(BiConsumer<T, U> biConsumer, T t, U u) {
int retries = 0;
boolean retry = false;
AException lastServiceException = null;
do {
try {
biConsumer.accept(t, u);
} catch (AException e) {
lastServiceException = e;
int statusCode = e.getStatusCode();
if (statusCode == INTERNAL_EXCEPTION_CODE) {
throw e;
} else {
break;
}
}
retries++;
if (retries >= 3) {
retry = false;
}
} while (retry);
if (lastServiceException != null) {
throw lastServiceException;
}
}
Test Class:
public class RetryExampleTest {
...
#Test
public void test() {
RetryExample retryExample = new RetryExample();
String fileName = "TestFile";
byte[] data = simulatedPayload(10000);
try {
retryExample.process(fileName, data);
} catch (Exception e) {
fail("Exception thrown=" + e);
}
}
According to the Mockito documentation you can set different behavior for consecutive method calls.
when(mock.someMethod("some arg"))
.thenThrow(new RuntimeException())
.thenReturn("foo");
In case of a void method you can do something similar (Mockito documentation)
doThrow(new RuntimeException())
.doNothing()
.when(mock).doSomething();
I think you can use a global data object to store the times of throw Exceptions, so in the Mockito library invoke the Exception method just taken the global data object to record the times. It would be simple. Just all by your control.

How to corect stop thread while inserting to database in Java?

This question continues this question.
I am trying to stop thread that inserts rows to dabase, but I always got an exception
This part of code shows my thread's logic
public void run() {
String number = "";
try {
while (!Thread.currentThread().isInterrupted()) {
number = generateNumber();
database.insertOrUpdateRow(number);
}
} catch (SQLException ex) {
LOGGER.error("exception while inserting number " + number + " " + ex.getMessage());
}
}
I have googled how to stop thread correctly and found that I should just flag that thread is stopped.
So, this is what I got
public boolean stop() {
try {
Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
for (Thread t : threadSet) {
if (t != null) {
if (t.getName().contains("generate")) {
t.interrupt();
LOGGER.info(t.getName()+" is stopped");
}
}
}
} catch (Exception e) {
LOGGER.error("Exception when call stop: " + e.toString());
}
return true;
}
My stacktrace
[INFO] [qtp1804418913-32] INFO se.homework.hwbs.tasks.un.server.NumberQtyService
Impl - generate37 is stopped
[INFO] [generate37] ERROR se.homework.hwbs.tasks.un.server.threads.InsertRowThre
ad - exception while inserting number 34587 transaction rollback: serialization
failure
[INFO] [qtp1804418913-33] ERROR se.homework.hwbs.tasks.un.server.database.Databa
se - error select cnt by numberjava.sql.SQLTransactionRollbackException: transac
tion rollback: serialization failure
[INFO] [qtp1804418913-34] ERROR se.homework.hwbs.tasks.un.server.database.Databa
se - error select cnt by numberjava.sql.SQLTransactionRollbackException: transac
tion rollback: serialization failure
Thread stops, everything works, but this exceptions make me feel bad. Why are they happens? How to stop thread when I work with database in correct way?
Database is HsqlDB.
Instead of enumerating threads and selecting one by name, you might consider using a stop variable in your insert thread which can be directly controlled.
Consider this GeneratorThread:
public class GeneratorThread extends Thread {
protected static volatile boolean stop;
public static void stopExecution() {
stop = true;
}
public GeneratorThread() {
stop = false;
}
public void run() {
String number = "";
try {
while (!stop) {
number = generateNumber();
database.insertOrUpdateRow(number);
}
} catch (SQLException ex) {
//Any SQLException will terminate your thread. Is this wanted?!
LOGGER.error("exception while inserting number " + number + " " + ex.getMessage());
}
}
}
You can stop this by calling GeneratorThread.stopExecution() which will set the flag to false causing the current number to be the last inserted.
Of cause this implies that there will be only one GeneratorThread ever in your application.
Just as well, you might rethink your while and try-catch positions in run. This thread would end on any SQLException whatsoever. If you want to keep the thread running even if there are (temporary?) errors, you might want to do
while (!stop) {
String number = "";
try {
number = generateNumber();
database.insertOrUpdateRow(number);
} catch (SQLException ex) {
LOGGER.error("exception while inserting number " + number + " " + ex.getMessage());
}
}
I combined ideas of #Jan and #Fildor.
So, this is what I got
public class GeneratingThread extends Thread {
private final Logger LOGGER = LoggerFactory.getLogger(InsertRowThread.class);
private final Database database;
private boolean stop = false;
public GeneratingThread(Database database) {
this.database = database;
}
#Override
public void run() {
String number = "";
try {
while (!stop) {
number = generateNumber();
database.insertOrUpdateRow(number);
}
} catch (SQLException ex) {
LOGGER.error("exception while inserting number " + number + " " + ex.getMessage());
}
}
public void stopThread() {
this.stop = true;
}
private String generateNumber() {
int number = ThreadLocalRandom.current().nextInt(0, 100000);
String sb = Integer.toString(number);
while (sb.length() < 5) {
sb = "0" + sb;
}
return sb;
}
}
And this is my starting of thread
private final List<GeneratingThread> generatingThreads = new ArrayList<>();
GeneratingThread thread = new GeneratingThread(db);
thread.start();
generatingThreads.add(thread);
And stop it
for (GeneratingThread gt : generatingThreads) {
if (gt != null) {
gt.stopThread();
LOGGER.info("Thread is stopped");
}
}
Worked perfect for me. Thanks for discussion!

Compilation error:Exception is never thrown in body of corresponding try statement

In my quest to learn Java better, I have been trying to understand exception handling. I cannot understand why the following code fails to compile.
The compiler message is:
TestExceptionHandling.java:12: error: exception StupidException is never thrown in body of corresponding try statement
catch (StupidException stupidEx) {
^
In the try block, method exTest.doExTest() is called. In this method I catch an InterruptedException and in its catch block I throw a new StupidException.
So why does the compiler say it's not thrown? What am I missing? Can any expert help me see my mistake please?
public class TestExceptionHandling {
public static void main(String argv[]) {
String output = "";
try {
output = "\nCalling method doExTest:\n";
exTest.doExTest();
}
catch (StupidException stupidEx) {
System.out.println("\nJust caught a StupidException:\n " + stupidEx.toString());
}
System.out.println(output);
}
}
class exTest {
static long mainThreadId;
protected static void doExTest() { //throws StupidException
mainThreadId = Thread.currentThread().getId();
Thread t = new Thread(){
public void run(){
System.out.println("Now in run method, going to waste time counting etc. then interrupt main thread.");
// Keep the cpu busy for a while, so other thread gets going...
for (int i = 0; i < Integer.MAX_VALUE; i++) {
int iBoxed = (int)new Integer(String.valueOf(i));
String s = new String("This is a string" + String.valueOf(iBoxed));
}
// find thread to interrupt...
Thread[] threads = new Thread[0];
Thread.enumerate(threads);
for (Thread h: threads) {
if (h.getId() == mainThreadId) {
h.interrupt();
}
}
}
};
t.start();
try {
Thread.sleep(5000);
}
catch (InterruptedException e){
System.out.println("\nAn InterruptedException " + e.toString() + " has occurred. Exiting...");
throw new StupidException("Got an InterruptedException ", e); // ("Got an InterruptedException. Mutated to StupidException and throwing from doExTest to caller...", e);
}
}
}
class StupidException extends Exception {
public StupidException(String message, Throwable t) {
super(message + " " + t.toString());
}
public String toString() {
return "Stupid Exception: " + super.toString();
}
}
Methods need to explicitly declare that they throw exceptions (with the exception of runtime exceptions, which are a bit different). Try declaring your doExTest method as
protected static void doExTest() throws StupidException {
...
}

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
}
});

Categories