improved actors doc
This commit is contained in:
parent
2142ca1309
commit
bb8824296a
2 changed files with 15 additions and 13 deletions
|
|
@ -198,7 +198,7 @@ An Actor has to implement the ‘receive’ method to receive messages:
|
|||
|
||||
protected def receive: PartialFunction[Any, Unit]
|
||||
|
||||
Note: Akka has an alias to the 'PartialFunction[Any, Unit]' type called 'Receive', so you can use this type instead for clarity. But most often you don't need to spell it out.
|
||||
Note: Akka has an alias to the 'PartialFunction[Any, Unit]' type called 'Receive' (akka.actor.Actor.Receive), so you can use this type instead for clarity. But most often you don't need to spell it out.
|
||||
|
||||
This method should return a PartialFunction, e.g. a ‘match/case’ clause in which the message can be matched against the different case clauses using Scala pattern matching. Here is an example:
|
||||
|
||||
|
|
@ -545,24 +545,28 @@ In generic base Actor:
|
|||
|
||||
.. code-block:: scala
|
||||
|
||||
import akka.actor.Actor.Receive
|
||||
|
||||
abstract class GenericActor extends Actor {
|
||||
|
||||
// to be defined in subclassing actor
|
||||
def specificMessageHandler: PartialFunction[Any, Unit]
|
||||
|
||||
def specificMessageHandler: Receive
|
||||
|
||||
// generic message handler
|
||||
def genericMessageHandler = {
|
||||
... // generic message handler
|
||||
def genericMessageHandler: Receive = {
|
||||
case event => printf("generic: %s\n", event)
|
||||
}
|
||||
|
||||
|
||||
def receive = specificMessageHandler orElse genericMessageHandler
|
||||
}
|
||||
|
||||
In subclassing Actor:
|
||||
|
||||
`<code format="scala">`_
|
||||
class SpecificActor extends GenericActor {
|
||||
def specificMessageHandler = {
|
||||
... // specific message handler
|
||||
case event: MyMsg => printf("specific: %s\n", event.subject)
|
||||
}
|
||||
}
|
||||
`<code>`_
|
||||
|
||||
case class MyMsg(subject: String)
|
||||
`<code>`_
|
||||
|
|
@ -26,8 +26,6 @@ Here is an example:
|
|||
}
|
||||
}
|
||||
|
||||
The 'UntypedActor' class inherits from the 'akka.util.Logging' class which defines a logger in the 'log' field that you can use to log. The logging uses SLF4j backed by logback - for more information on how to configure the logger see `Logging <logging>`_.
|
||||
|
||||
Creating Actors
|
||||
^^^^^^^^^^^^^^^
|
||||
|
||||
|
|
@ -35,7 +33,7 @@ Creating an Actor is done using the 'akka.actor.Actors.actorOf' factory method.
|
|||
|
||||
.. code-block:: java
|
||||
|
||||
ActorRef actor = Actors.actorOf(SampleUntypedActor.class);
|
||||
ActorRef myActor = Actors.actorOf(SampleUntypedActor.class);
|
||||
myActor.start();
|
||||
|
||||
Normally you would want to import the 'actorOf' method like this:
|
||||
|
|
@ -51,7 +49,7 @@ You can also create & start the actor in one statement:
|
|||
|
||||
.. code-block:: java
|
||||
|
||||
ActorRef actor = actorOf(SampleUntypedActor.class).start();
|
||||
ActorRef myActor = actorOf(SampleUntypedActor.class).start();
|
||||
|
||||
The call to 'actorOf' returns an instance of 'ActorRef'. This is a handle to the 'UntypedActor' instance which you can use to interact with the Actor, like send messages to it etc. more on this shortly. The 'ActorRef' is immutble and has a one to one relationship with the Actor it represents. The 'ActorRef' is also serializable and network-aware. This means that you can serialize it, send it over the wire and use it on a remote host and it will still be representing the same Actor on the original node, across the network.
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue