Skip to content

Instantly share code, notes, and snippets.

@jboner
Last active March 27, 2019 16:43
Show Gist options
  • Save jboner/9990472 to your computer and use it in GitHub Desktop.
Save jboner/9990472 to your computer and use it in GitHub Desktop.
A game of ping pong using Akka Actors. For a version that is persisted using Event-Sourcing see this gist: https://gist.github.com/jboner/9990435
package demo
import akka.actor.{Actor, Props, ActorSystem}
object PingPong extends App {
case object Ball
class Ping extends Actor {
var counter = 0
def receive = {
case Ball =>
counter += 1
println(s"Ping counter: ${counter}")
Thread.sleep(1000) // you should *never* call Thread.sleep in a real application, just here to slow things down
sender ! Ball
}
}
class Pong extends Actor {
var counter = 0
def receive = {
case Ball =>
counter += 1
println(s"Pong counter: ${counter}")
Thread.sleep(1000)
sender ! Ball
}
}
val system = ActorSystem("pingpong")
val ping = system.actorOf(Props(classOf[Ping]), "ping")
val pong = system.actorOf(Props(classOf[Pong]), "pong")
ping.tell(Ball, pong)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment