Created
January 22, 2011 20:48
-
-
Save teamon/791459 to your computer and use it in GitHub Desktop.
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
// akka-actor cheatsheet | |
// extract from http://doc.akka.io/actors-scala | |
class MyActor extends Actor { // Logging is included in Actor | |
// messages | |
def receive = { | |
case "foo" => | |
doBar | |
case "logging" => | |
log.trace("trace msg") | |
log.debug("debug msg") | |
log.info("info msg") | |
log.warn("warn msg") | |
log.error("error msg") | |
case "plz reply" => | |
self reply "success" | |
case msg @ "plz forward me" => | |
otherActorRef forward msg | |
case "plz start waiting" => | |
become(waitingForSomething) | |
case msg => | |
log.warn("I dont know %s", msg) | |
} | |
// alternative receive | |
def waitingForSomething: Receive = { | |
case "go" => unbecome | |
} | |
// hooks | |
override def preStart { } // when actor starting | |
override def postStop { } // when invoking actor.stop | |
override def preRestart(reason: Throwable) { } // when restarting by supervisor | |
override def postRestart(reason: Throwable) { } // when restarting by supervisor | |
} | |
// Creating actors | |
val myActor = actorOf[MyActor].start | |
val myActor = actorOf(new MyActor("param")).start | |
// fire and forget, do not care about reply | |
myActor ! "msg" | |
// Get response af future option | |
(myActor !! "msg") match { | |
case Some(reply) => // handle reply | |
case None => // handle timeout | |
} | |
// Send and receive (see trait Future for docs) | |
myActor !!! "msg" | |
// akka actor registry | |
// shutdown all | |
registry.shutdownAll | |
// find actors | |
registrt.actors: Array[ActorRef] | |
registry.actorFor[MyActor]: Option[ActorRef] | |
registry.actorsFor[MyActor]: Array[ActorRef] | |
registry.filter(p: (ActorRef) => Boolean): Array[ActorRef] | |
registry.find[T](f: PartialFunction[ActorRef, T]): Option[T] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment