Skip to content

Instantly share code, notes, and snippets.

@rtfpessoa
Created December 11, 2013 11:48
Show Gist options
  • Save rtfpessoa/689aba7c420d106bca41 to your computer and use it in GitHub Desktop.
Save rtfpessoa/689aba7c420d106bca41 to your computer and use it in GitHub Desktop.
Simple example implementation of routing with akka typed actors
val defaultAkkaTimeout = 60
val defaultNumberOfActors = 10
def getComponentActor[T <: AnyRef](timeout: akka.util.Timeout = Timeout(Duration(defaultAkkaTimeout, SECONDS)))(implicit m: Manifest[T]): T = {
val className = this.classCache.get(m.runtimeClass.getName)
val actorCount = defaultNumberOfActors
val actors = createRouterActors(actorCount,className,timeout)
val router = system.actorOf(Props.empty.withRouter(SmallestMailboxRouter.create(actors.toIterable.asJava)))
TypedActor(system).typedActorOf(TypedProps(Reflect(system).actorClassFor(className.get)).withTimeout(timeout), router)
}
def createRouterActors[T <: AnyRef](actorCount:Int, className:Option[String], timeout:akka.util.Timeout)(implicit m: Manifest[T]): Seq[ActorRef] = {
(1 to actorCount).map {
i =>
TypedActor.get(system).getActorRefFor(TypedActor.get(system).typedActorOf(TypedProps(Reflect(system).actorClassFor(className.get)).withTimeout(timeout)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment