joesan
joesan

Reputation: 15345

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?

Upvotes: 2

Views: 1994

Answers (1)

mattinbits
mattinbits

Reputation: 10428

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

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

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

Upvotes: 4

Related Questions