Skip to content

Instantly share code, notes, and snippets.

@ConnorDoyle
Created December 26, 2011 17:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ConnorDoyle/1521658 to your computer and use it in GitHub Desktop.
A very simple Ping Pong example using Akka's local actors.
import akka.actor._, akka.actor.Actor._
case object Ping
case object Pong
class PongActor extends Actor {
def receive = {
case Ping {
println(self.path + ": Received Ping!")
sender ! Pong
}
case _ ()
}
}
class PingActor extends Actor {
context.actorSelection("../Pong*") ! Ping // starts things off
def receive = {
case Pong {
println(self.path + ": Received Pong!")
sender ! Ping
}
case _ ()
}
}
object PingPong extends App {
val system = ActorSystem()
system.actorOf(Props[PongActor], name="Pong")
system.actorOf(Props[PingActor], name="Ping")
}
object PingPongPong extends App {
val system = ActorSystem()
system.actorOf(Props[PongActor], name="Pong1")
system.actorOf(Props[PongActor], name="Pong2")
system.actorOf(Props[PingActor], name="Ping")
}
@ConnorDoyle
Copy link
Author

This example must be compiled and run with the appropriate Akka jars in the class path.

I made a simple shell script to run the example:
export CLASSPATH=$CLASSPATH:lib/akka-2_0-M1/akka-actor-2.0-M1.jar:lib/akka-2_0-M1/akka-remote-2.0-M1.jar scala -cp $CLASSPATH PingPong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment