I have a generic utility in scala to Retry a certain piece of code logic n number of times.
RetryCode.scala
object RetryCode {
def retry[T](timesRetry: Int)(block: => T): T = {
def doBlock() = block
breakable {
for (i <- 0 until timesRetry) {
try {
...
doBlock()
} catch {
...
}
}
}
}
}
Test Unit in Scala:
class RetryCodeTest {
test("success on 1st retry") {
var i = 0
val result = retry(5) {
i = i + 1
i
}
assert(result==1)
}
}
I'd like to extend this functionality to a Java program
private void myMethod(int x) {
try {
// Code block to Retry 10 times
...
} catch(Exception ex) {
System.out.println("Error: " + ex.getMessage());
}
}
How do I send a block of Java 7 code to my RetryCode object in Scala?
myMethodwithin the loop?retry()method in Scala with one that takes a JavaCallable, and passes it to your real retry method. Then in your Java you could create an anonymousCallable(or a lamda expression in Java 8). Something along those lines.