Created
August 20, 2015 06:52
-
-
Save johanandren/2d562af9a6cde399aec4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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