scala retry 참조 코드

scala 2018. 1. 23. 11:33

scala retry 참조 코드 



https://stackoverflow.com/questions/7930814/whats-the-scala-way-to-implement-a-retry-able-call-like-this-one





import util._

object RetryUtils {

@annotation.tailrec
def retry[T](n: Int)(fn: => T): T = {
Try { fn } match {
case Success(x) => x
case _ if n > 1 => retry(n - 1)(fn)
case Failure(e) => throw e
}
}
}



예제 코드


val retryResult = retry(3) {
if (validateObject(esClient)) {
esClient
} else {
null
}
}


Posted by '김용환'
,