Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PingPongRouter
import akka.actor._
import akka.routing.{ Broadcast, DefaultResizer, RoundRobinPool }
case object Init
case object Pong
case object Ping
class PingActor(pongActorRef: ActorRef) extends Actor {
def receive: Receive = {
case Init =>
println("Init")
pongActorRef.tell(Ping, context.parent)
case Pong =>
println("pong")
println(self)
pongActorRef.tell(Ping, context.parent)
case _ => println("I don't get it ping")
}
}
class PongActor() extends Actor {
def receive: Receive = {
case Ping =>
println("ping")
println(self)
Thread.sleep(1000)
sender.tell(Pong, context.parent)
case _ => println("I don't get it pong")
}
}
object PingPongRouter extends App {
val actorSystem = ActorSystem("main")
val resizer =
DefaultResizer(lowerBound = 5, upperBound = 15, messagesPerResize = 1)
val pongRouter: ActorRef =
actorSystem.actorOf(
RoundRobinPool(2, Some(resizer)).props(Props(classOf[PongActor])),
"pongRouter")
val pingRouter: ActorRef =
actorSystem.actorOf(RoundRobinPool(2, Some(resizer))
.props(Props(new PingActor(pongRouter))),
"pingRouter")
pingRouter ! Broadcast(Init)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.