Skip to content

Instantly share code, notes, and snippets.

@sdether
Last active March 19, 2018 15:54
Show Gist options
  • Save sdether/15bc96e27ed965bd89c3 to your computer and use it in GitHub Desktop.
Save sdether/15bc96e27ed965bd89c3 to your computer and use it in GitHub Desktop.
An async initialization mix-in for Akka Actors
import akka.actor.{ActorRef, Actor, Stash}
import akka.pattern.pipe
import scala.concurrent.Future
trait AsyncInitializer[DEPENDENCIES] extends Stash {
import context._
case class Initialized(dependencies: DEPENDENCIES)
def running(dependencies: DEPENDENCIES): Receive
def initialize(): Future[DEPENDENCIES]
override def preStart() = initialize().map(Initialized) pipeTo self
def receive = uninitialized
def uninitialized: Receive = {
case Initialized(dependencies) =>
unstashAll()
become(running(dependencies))
case msg => stash()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment