Skip to content

Instantly share code, notes, and snippets.

@metamorph
Created January 25, 2013 07:49
Show Gist options
  • Save metamorph/4632600 to your computer and use it in GitHub Desktop.
Save metamorph/4632600 to your computer and use it in GitHub Desktop.
case class RegisterDispatcher(ref: ActorRef, matcher: String)
class MasterActor extends Actor {
def receive = {
case RegisterDispatcher(ref, matcher) => //.. add ref to registry. watch the ref to remove the entry should the actor terminate.
case s: String => // look in the registry for a match amd dispatch accordingly.
}
}
class ActorA(master: ActorRef) {
master ! RegisterDispatcher(self, "bar")
def receive = {
//.. Handle message
}
}
class ActorB(master: ActorRef) {
master ! RegisterDispatcher(self, "foo")
def receive = {
//.. Handle message
}
}
object Main extends App {
val sys = ActorSystem("sys")
val master = sys.actorOf(Props[MasterActor])
sys.actorOf(Props(new ActorA(master)))
sys.actorOf(Props(new ActorB(master)))
// Time passes...
master ! "foo"
master ! "bar"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment