A very simple Ping Pong example using Akka's local actors.
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._, 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") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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