Tutorial by Examples

To create simple actors without creating a new class, you can use: import akka.actor.ActorDSL._ import akka.actor.ActorSystem implicit val system = ActorSystem("demo") val a = actor(new Act { become { case "hello" ⇒ sender() ! "hi" } })
The two possible ways of issuing a context.become (replacing or adding the new behavior) are offered separately to enable a clutter-free notation of nested receives: val a = actor(new Act { become { // this will replace the initial (empty) behavior case "info" ⇒ sender() ! "A...
Life-cycle hooks are also exposed as DSL elements, where later invocations of the methods shown below will replace the contents of the respective hooks: val a = actor(new Act { whenStarting { testActor ! "started" } whenStopping { testActor ! "stopped" } }) The above i...
It is also possible to create nested actors, i.e. grand-children, like this: // here we pass in the ActorRefFactory explicitly as an example val a = actor(system, "fred")(new Act { val b = actor("barney")(new Act { whenStarting { context.parent ! ("hello from &quot...
It is also possible to assign a supervision strategy to these actors with the following: superviseWith(OneForOneStrategy() { case e: Exception if e.getMessage == "hello" ⇒ Stop case _: Exception ⇒ Resume })
Last but not least there is a little bit of convenience magic built-in, which detects if the runtime class of the statically given actor subtype extends the RequiresMessageQueue trait via the Stash trait (this is a complicated way of saying that new Act with Stash would not work because its runtime ...

Page 1 of 1