1
미래를위한 재시도 메커니즘을 구현하고 싶습니다. 예를 들어Scala 암시 적 클래스에 형식 매개 변수를 사용하는 방법?
:
myFuture.map { data =>
println(data)
// ... do other stuff
}.recover {
case e: MyException => logger.error("Something went wrong with XYZ", e)
case _ => logger.error("Error!")
}.retry(Seq(1.seconds, 10.seconds, 30.seconds))
그래서 미래는 일정 간격 후 다시 시도해야합니다.
import akka.pattern.after
import akka.actor.Scheduler
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration.FiniteDuration
object FutureExt {
implicit class FutureUtils(f: Future[T]) {
def retry[T](delays: Seq[FiniteDuration])(implicit ec: ExecutionContext, s: Scheduler): Future[T] = {
f recoverWith { case _ if delays.nonEmpty => after(delays.head, s)(f.retry(delays.tail)) }
}
}
}
불행하게도, 형식 매개 변수
T
는 암시 적 클래스 선언에서 해결 될 수없는 다음과 같이
내 구현 보인다. 그게 뭐가 잘못 됐는지 알아?