I am trying to learn Scala and it seems like a very powerful language so far, but some things seem hard to achieve, may be its just my learning curve. I have researched online for a few days but could not find a good solution to do what I want to do.
I have multiple methods (with different signatures, including return type) which I want to wrap around in a retry logic.
I want to keep calling a method for a predefined number of times until the method succeeds.
Here is an example:
def downloadLocal(memory: Boolean, userName: Name, version: Int): Int
def downloadRemote(
memory: Boolean, userName: Name, masterNodeId: String, masterNodeId: String
): Pair(Int, Int)
I want wrap these two method inside the retry logic. Here is my attempt at the retry logic:
trait WithRetry{
def withRetry(retry :Int){
try{
callBack
}catch{
case exception:Throwable =>
if(retry>0){
logger.warn(exec+" method failed with an exception. retry number is:" + retry);
logger.warn(exception);
withRetry(retry-1)
}
else{
throw exception
}
}
}
def callBack():Any
}
The problem I have is that I am not able to find a clean way of wrapping around my methods (downloadRemote
and downloadLocal
) inside the retry logic.
Any suggestions/thoughts?