In some scenarios, method calls can fail – but would succeed when retrying the call after some seconds have passed.
That is often the case with remote method calls, where the remote application could be temporarily unavaillable.
I needed a simple way to define something like
“call method foo(). In case the method throws an Exception, retry to call it up to 10 times with a delay of 3 seconds”.
I implemented a simple Groovy util class which enabled me to write code as follows:
RetryUtil.retry(10, 3000){ foo() }
Here is the sourcecode of the RetryUtil Groovy class:
import org.apache.commons.logging.LogFactory
import org.apache.commons.logging.Log
class RetryUtil {
private static Log log = LogFactory.getLog(RetryUtil);
static retry(int times, long sleeptime, Closure c){
Throwable catchedThrowable = null
for(int i=0; i
Pingback: Grails cette semaine (2011-12) – Traduction de l’article original