sparkr sparkr - 1 year ago 127
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 Source

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

context.system.scheduler.scheduleOnce(10 seconds, self, Init)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download