Skip to content

Instantly share code, notes, and snippets.

@shankarshastri
Created December 18, 2020 13:37
Show Gist options
  • Save shankarshastri/03f36797f20b484635f78744739f0929 to your computer and use it in GitHub Desktop.
Save shankarshastri/03f36797f20b484635f78744739f0929 to your computer and use it in GitHub Desktop.
PingPongActorMain
import akka.NotUsed
import akka.actor.typed.scaladsl.Behaviors
import akka.actor.typed.{ActorRef, ActorSystem, Behavior}
import scala.concurrent.duration.DurationInt
object PingPongActorMain extends App {
case class Ping(counter: Int, replyTo: ActorRef[Pong])
case class Pong(counter: Int, replyTo: ActorRef[Ping])
val pingBehaviour = Behaviors.receive[Ping] {
(context, message) => {
println(s"Received Ping ${message.counter}")
context.scheduleOnce(2 second, message.replyTo, Pong(message.counter, context.self))
Behaviors.same[Ping]
}
}
val pongBehaviour = Behaviors.receive[Pong] {
(context, message) => {
println(s"Received Pong ${message.counter}")
context.scheduleOnce(2 second, message.replyTo, Ping(message.counter + 1, context.self))
Behaviors.same[Pong]
}
}
def setupPingPong(): Behavior[NotUsed] = {
Behaviors.setup[NotUsed] {
e => {
val pingActor = e.spawn(pingBehaviour, "PingActor")
val pongActor = e.spawn(pongBehaviour, "PongActor")
pingActor.tell(Ping(0, pongActor))
Behaviors.same
}
}
}
val system = ActorSystem(setupPingPong(), "hello")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment