Last active
June 14, 2019 05:33
-
-
Save shankarshastri/2a513e78ac22571cd2d9169aad6e8c44 to your computer and use it in GitHub Desktop.
PingPongRouter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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