Skip to content

Instantly share code, notes, and snippets.

@johanandren
Created August 20, 2015 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johanandren/2d562af9a6cde399aec4 to your computer and use it in GitHub Desktop.
Save johanandren/2d562af9a6cde399aec4 to your computer and use it in GitHub Desktop.
import akka.actor.{ActorSystem, ActorRef}
import akka.pattern.ask
import akka.pattern.AskTimeoutException
import akka.util.Timeout
import scala.concurrent.Future
import scala.concurrent.duration._
import scala.io.StdIn
object AskWithRetry extends App {
implicit val system = ActorSystem("test")
implicit val timeout: Timeout = 10 millis
import system.dispatcher
retry(system.deadLetters, "wat", 2000000, 0)
println("Enter to exit")
StdIn.readLine()
system.shutdown()
def retry(actorRef: ActorRef, message: Any, maxAttempts: Int, attempt: Int): Future[Any] = {
println(s"Retrying attempt $attempt")
val future = (actorRef ? message) recover {
case e: AskTimeoutException =>
if (attempt <= maxAttempts) retry(actorRef, message, maxAttempts, attempt + 1)
else None // Return default result according to your requirement, if actor is non-reachable.
}
future
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment