public
Last active

PostStart implementation for Akka

  • Download Gist
PostStart.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
trait PostStart { actor: Actor =>
def postStart: Unit
override def preStart {
actor.become {
case "PostStart" => try { postStart } finally { actor.unbecome }
}
actor.self ! "PostStart"
}
}
 
/*Usage: class MyActor extends Actor with PostStart {
def postStart = {
...
}
 
def receive = {
... your usual business stuff goes here ...
}
}*/
override def preStart {
  super.preStart
  ...

?

Alaz: Care to write that in code?

The pleasure is all mine

Hi,

trait PostStart { actor: Actor =>
  def postStart: Unit

  override def preStart {
    context.become {
      case "PostStart" => try { postStart } finally { context.unbecome }
    }
    actor.self ! "PostStart"
  }
}

with Akka 2.1 (I didn't see this version)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.