sparkr sparkr - 1 month ago 19
Scala Question

Akka Actor Retry on Failure

I have an Actor that gets a message called Init and upon receiving this message, it tries to create a connection to an external service. Now this connection could succeed or fail and I know this information in my Actor:

def receive = {
case Init => {
val someConn: Option[Connection] = createConnection(...)
someConn match {
case Some(conn) => {
// do something
}
case None => // I want to re-try Init, but after a few seconds delay!
}
}


Upon None, I would like to send a self message to this actor with an Init message again, but I would not like to do it immediately. I would like to have a delay of few seconds, say 10 seconds. Any suggestions?

Answer

You can send a message to yourself once, on a delay, using scheduleOnce, see my answer here:

http://stackoverflow.com/a/31658345/5142537

context.system.scheduler.scheduleOnce(10 seconds, self, Init)
Comments