From f753e2aef1975f7ed932024ab842b77326836adb Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Mon, 11 Apr 2011 12:15:08 +0200 Subject: [PATCH 1/7] typo --- akka-docs/pending/slf4j.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/akka-docs/pending/slf4j.rst b/akka-docs/pending/slf4j.rst index 6b7ea8affb..99e9256cde 100644 --- a/akka-docs/pending/slf4j.rst +++ b/akka-docs/pending/slf4j.rst @@ -11,7 +11,7 @@ You can use the 'akka.event.slf4j.Logging' trait to mix in logging behavior into Event Handler ------------- -This module also includes an SLF4J Event Handler that works with Akka's standar Event Handler. You enabled it in the 'event-handlers' element in akka.conf. Here you can also define the log level. +This module also includes an SLF4J Event Handler that works with Akka's standard Event Handler. You enabled it in the 'event-handlers' element in akka.conf. Here you can also define the log level. .. code-block:: ruby From 909e90d019d4f93048b0c9902aca69e875638f9e Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Mon, 11 Apr 2011 12:51:47 +0200 Subject: [PATCH 2/7] removed Logging trait --- akka-docs/pending/slf4j.rst | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/akka-docs/pending/slf4j.rst b/akka-docs/pending/slf4j.rst index 99e9256cde..780030a543 100644 --- a/akka-docs/pending/slf4j.rst +++ b/akka-docs/pending/slf4j.rst @@ -3,15 +3,10 @@ SLF4J This module is available in the 'akka-slf4j.jar'. It has one single dependency; the slf4j-api jar. -Logging trait -------------- - -You can use the 'akka.event.slf4j.Logging' trait to mix in logging behavior into your classes and use the 'log' Logger member variable. But the preferred way is to use the event handler (see below). - Event Handler ------------- -This module also includes an SLF4J Event Handler that works with Akka's standard Event Handler. You enabled it in the 'event-handlers' element in akka.conf. Here you can also define the log level. +This module includes a SLF4J Event Handler that works with Akka's standard Event Handler. You enabled it in the 'event-handlers' element in akka.conf. Here you can also define the log level. .. code-block:: ruby From 2142ca13098b60808a9e5cd0555a2a00fb1f925b Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Mon, 11 Apr 2011 12:52:12 +0200 Subject: [PATCH 3/7] fixed wrong code block syntax --- akka-docs/pending/actor-registry-java.rst | 30 ++++++++++---------- akka-docs/pending/actor-registry-scala.rst | 32 +++++++++++----------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/akka-docs/pending/actor-registry-java.rst b/akka-docs/pending/actor-registry-java.rst index 005d9eb4bb..925b51d012 100644 --- a/akka-docs/pending/actor-registry-java.rst +++ b/akka-docs/pending/actor-registry-java.rst @@ -51,27 +51,27 @@ The messages sent to this Actor are: So your listener Actor needs to be able to handle these two messages. Example: .. code-block:: java -import akka.actor.ActorRegistered; -import akka.actor.ActorUnregistered; -import akka.actor.UntypedActor; -import akka.event.EventHandler; + import akka.actor.ActorRegistered; + import akka.actor.ActorUnregistered; + import akka.actor.UntypedActor; + import akka.event.EventHandler; -public class RegistryListener extends UntypedActor { - public void onReceive(Object message) throws Exception { - if (message instanceof ActorRegistered) { - ActorRegistered event = (ActorRegistered) message; - EventHandler.info(this, String.format("Actor registered: %s - %s", + public class RegistryListener extends UntypedActor { + public void onReceive(Object message) throws Exception { + if (message instanceof ActorRegistered) { + ActorRegistered event = (ActorRegistered) message; + EventHandler.info(this, String.format("Actor registered: %s - %s", event.actor().actorClassName(), event.actor().getUuid())); - } else if (message instanceof ActorUnregistered) { - // ... + } else if (message instanceof ActorUnregistered) { + // ... + } } } -} -.. code-block:: java + The above actor can be added as listener of registry events: .. code-block:: java -import static akka.actor.Actors.*; + import static akka.actor.Actors.*; ActorRef listener = actorOf(RegistryListener.class).start(); registry().addListener(listener); -.. code-block:: java + diff --git a/akka-docs/pending/actor-registry-scala.rst b/akka-docs/pending/actor-registry-scala.rst index 83a77689d4..70defb918c 100644 --- a/akka-docs/pending/actor-registry-scala.rst +++ b/akka-docs/pending/actor-registry-scala.rst @@ -78,27 +78,27 @@ The messages sent to this Actor are: So your listener Actor needs to be able to handle these two messages. Example: .. code-block:: scala -import akka.actor.Actor -import akka.actor.ActorRegistered; -import akka.actor.ActorUnregistered; -import akka.actor.UntypedActor; -import akka.event.EventHandler; + import akka.actor.Actor + import akka.actor.ActorRegistered; + import akka.actor.ActorUnregistered; + import akka.actor.UntypedActor; + import akka.event.EventHandler; -class RegistryListener extends Actor { - def receive = { - case event: ActorRegistered => - EventHandler.info(this, "Actor registered: %s - %s".format( + class RegistryListener extends Actor { + def receive = { + case event: ActorRegistered => + EventHandler.info(this, "Actor registered: %s - %s".format( event.actor.actorClassName, event.actor.uuid)) - case event: ActorUnregistered => - // ... + case event: ActorUnregistered => + // ... + } } -} -.. code-block:: scala + The above actor can be added as listener of registry events: .. code-block:: scala -import akka.actor._ -import akka.actor.Actor._ + import akka.actor._ + import akka.actor.Actor._ val listener = actorOf[RegistryListener].start registry.addListener(listener) -.. code-block:: scala + From bb8824296ae9c93576d5cf1618df9d97df80871c Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Mon, 11 Apr 2011 14:41:17 +0200 Subject: [PATCH 4/7] improved actors doc --- akka-docs/pending/actors-scala.rst | 22 +++++++++++++--------- akka-docs/pending/untyped-actors-java.rst | 6 ++---- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/akka-docs/pending/actors-scala.rst b/akka-docs/pending/actors-scala.rst index 58220055a4..fa24e91db3 100644 --- a/akka-docs/pending/actors-scala.rst +++ b/akka-docs/pending/actors-scala.rst @@ -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: + ``_ class SpecificActor extends GenericActor { def specificMessageHandler = { - ... // specific message handler + case event: MyMsg => printf("specific: %s\n", event.subject) } } -``_ + +case class MyMsg(subject: String) +``_ \ No newline at end of file diff --git a/akka-docs/pending/untyped-actors-java.rst b/akka-docs/pending/untyped-actors-java.rst index 6c2a665929..be6e35f2cd 100644 --- a/akka-docs/pending/untyped-actors-java.rst +++ b/akka-docs/pending/untyped-actors-java.rst @@ -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 `_. - 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. From 4aba589eee3e7e95b650f78a31b9aa574ce8bf2d Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Mon, 11 Apr 2011 15:12:32 +0200 Subject: [PATCH 5/7] cleanup --- akka-docs/pending/actor-registry-java.rst | 4 ++-- akka-docs/pending/actor-registry-scala.rst | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/akka-docs/pending/actor-registry-java.rst b/akka-docs/pending/actor-registry-java.rst index 925b51d012..ef815d857a 100644 --- a/akka-docs/pending/actor-registry-java.rst +++ b/akka-docs/pending/actor-registry-java.rst @@ -7,6 +7,7 @@ ActorRegistry: Finding Actors ----------------------------- Actors can be looked up using the 'akka.actor.Actors.registry()' object. Through this registry you can look up actors by: + * uuid com.eaio.uuid.UUID – this uses the ‘uuid’ field in the Actor class, returns the actor reference for the actor with specified uuid, if one exists, otherwise None * id string – this uses the ‘id’ field in the Actor class, which can be set by the user (default is the class name), returns all actor references to actors with specified id * parameterized type - returns a 'ActorRef[]' with all actors that are a subtype of this specific type @@ -73,5 +74,4 @@ The above actor can be added as listener of registry events: import static akka.actor.Actors.*; ActorRef listener = actorOf(RegistryListener.class).start(); - registry().addListener(listener); - + registry().addListener(listener); \ No newline at end of file diff --git a/akka-docs/pending/actor-registry-scala.rst b/akka-docs/pending/actor-registry-scala.rst index 70defb918c..a87dcc3039 100644 --- a/akka-docs/pending/actor-registry-scala.rst +++ b/akka-docs/pending/actor-registry-scala.rst @@ -7,6 +7,7 @@ ActorRegistry: Finding Actors ----------------------------- Actors can be looked up by using the **akka.actor.Actor.registry: akka.actor.ActorRegistry**. Lookups for actors through this registry can be done by: + * uuid akka.actor.Uuid – this uses the ‘**uuid**’ field in the Actor class, returns the actor reference for the actor with specified uuid, if one exists, otherwise None * id string – this uses the ‘**id**’ field in the Actor class, which can be set by the user (default is the class name), returns all actor references to actors with specified id * specific actor class - returns an '**Array[Actor]**' with all actors of this exact class @@ -78,6 +79,7 @@ The messages sent to this Actor are: So your listener Actor needs to be able to handle these two messages. Example: .. code-block:: scala + import akka.actor.Actor import akka.actor.ActorRegistered; import akka.actor.ActorUnregistered; @@ -95,10 +97,12 @@ So your listener Actor needs to be able to handle these two messages. Example: } The above actor can be added as listener of registry events: + .. code-block:: scala + import akka.actor._ import akka.actor.Actor._ - val listener = actorOf[RegistryListener].start - registry.addListener(listener) + val listener = actorOf[RegistryListener].start + registry.addListener(listener) From f878ee4c5d854c6ce1a4fce51c38f6d7588de4a0 Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Mon, 11 Apr 2011 15:27:45 +0200 Subject: [PATCH 6/7] minor improvements --- akka-docs/pending/actors-scala.rst | 2 +- akka-docs/pending/untyped-actors-java.rst | 27 +++++++++++------------ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/akka-docs/pending/actors-scala.rst b/akka-docs/pending/actors-scala.rst index fa24e91db3..f07dbb0ca4 100644 --- a/akka-docs/pending/actors-scala.rst +++ b/akka-docs/pending/actors-scala.rst @@ -263,7 +263,7 @@ If you want to send a message back to the original sender of the message you jus val result = process(request) self.reply(result) -In this case the 'result' will be send back to the Actor that send the 'request'. +In this case the 'result' will be send back to the Actor that sent the 'request'. The 'reply' method throws an 'IllegalStateException' if unable to determine what to reply to, e.g. the sender is not an actor. You can also use the more forgiving 'reply_?' method which returns 'true' if reply was sent, and 'false' if unable to determine what to reply to. diff --git a/akka-docs/pending/untyped-actors-java.rst b/akka-docs/pending/untyped-actors-java.rst index be6e35f2cd..760b5fd324 100644 --- a/akka-docs/pending/untyped-actors-java.rst +++ b/akka-docs/pending/untyped-actors-java.rst @@ -41,7 +41,7 @@ Normally you would want to import the 'actorOf' method like this: .. code-block:: java import static akka.actor.Actors.*; - ActorRef actor = actorOf(SampleUntypedActor.class); + ActorRef myActor = actorOf(SampleUntypedActor.class); To avoid prefix it with 'Actors' every time you use it. @@ -154,7 +154,7 @@ Using 'sendRequestReplyFuture' will send a message to the receiving Actor asynch .. code-block:: java - Future future= actorRef.sendRequestReplyFuture("Hello", getContext(), 1000); + Future future = actorRef.sendRequestReplyFuture("Hello", getContext(), 1000); The 'Future' interface looks like this: @@ -175,7 +175,7 @@ So the normal way of working with futures is something like this: .. code-block:: java - Future future= actorRef.sendRequestReplyFuture("Hello", getContext(), 1000); + Future future = actorRef.sendRequestReplyFuture("Hello", getContext(), 1000); future.await(); if (future.isCompleted()) { Option resultOption = future.result(); @@ -188,13 +188,6 @@ So the normal way of working with futures is something like this: The 'onComplete' callback can be used to register a callback to get a notification when the Future completes. Gives you a way to avoid blocking. -We also have a utility class 'Futures' that have a couple of convenience methods: - -.. code-block:: java - - void awaitAll(Future[] futures); - Future awaitOne(Future[] futures) - Forward message ^^^^^^^^^^^^^^^ @@ -207,7 +200,7 @@ You can forward a message from one actor to another. This means that the origina Receive messages ---------------- -When an actor receives a message is passed into the 'onReceive' method, this is an abstract method on the 'UntypedActor' base class that needs to be defined. +When an actor receives a message it is passed into the 'onReceive' method, this is an abstract method on the 'UntypedActor' base class that needs to be defined. Here is an example: @@ -216,8 +209,10 @@ Here is an example: public class SampleUntypedActor extends UntypedActor { public void onReceive(Object message) throws Exception { - if (message instanceof String) log.info("Received String message: %s", message); - else throw new IllegalArgumentException("Unknown message: " + message); + if (message instanceof String) + EventHandler.info(this, String.format("Received String message: %s", message)); + else + throw new IllegalArgumentException("Unknown message: " + message); } } @@ -241,7 +236,7 @@ If you want to send a message back to the original sender of the message you jus } } -In this case we will a reply back to the Actor that send the message. +In this case we will a reply back to the Actor that sent the message. The 'replyUnsafe' method throws an 'IllegalStateException' if unable to determine what to reply to, e.g. the sender has not been passed along with the message when invoking one of 'send*' methods. You can also use the more forgiving 'replySafe' method which returns 'true' if reply was sent, and 'false' if unable to determine what to reply to. @@ -391,6 +386,8 @@ Use it like this: .. code-block:: java + import static akka.actor.Actors.*; + actor.sendOneWay(poisonPill()); Killing an Actor @@ -402,6 +399,8 @@ Use it like this: .. code-block:: java + import static akka.actor.Actors.*; + // kill the actor called 'victim' victim.sendOneWay(kill()); From 9c08ca7b3d4a92a3b518faff7e49404edd8c714a Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Mon, 11 Apr 2011 16:50:22 +0200 Subject: [PATCH 7/7] fixed bugs in typed actors doc --- akka-docs/pending/typed-actors-java.rst | 17 +++++++++++------ akka-docs/pending/typed-actors-scala.rst | 12 ++++++++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/akka-docs/pending/typed-actors-java.rst b/akka-docs/pending/typed-actors-java.rst index a1d0d3594c..2322698ed1 100644 --- a/akka-docs/pending/typed-actors-java.rst +++ b/akka-docs/pending/typed-actors-java.rst @@ -23,13 +23,15 @@ If you have a POJO with an interface implementation separation like this: .. code-block:: java interface RegistrationService { - void register(User user, Credentials cred) - User getUserFor(String username) + void register(User user, Credentials cred); + User getUserFor(String username); } .. code-block:: java - public class RegistrationServiceImpl implements RegistrationService extends TypedActor { + import akka.actor.TypedActor; + + public class RegistrationServiceImpl extends TypedActor implements RegistrationService { public void register(User user, Credentials cred) { ... // register user } @@ -69,9 +71,12 @@ Using a configuration object: .. code-block:: java + import static java.util.concurrent.TimeUnit.MILLISECONDS; + import akka.actor.TypedActorConfiguration; + import akka.util.FiniteDuration; + TypedActorConfiguration config = new TypedActorConfiguration() - .timeout(3000) - .makeTransactionRequired(); + .timeout(new FiniteDuration(3000, MILLISECONDS)); RegistrationService service = (RegistrationService) TypedActor.newInstance(RegistrationService.class, config); @@ -161,7 +166,7 @@ Here is an example how you can use it to in a 'void' (e.g. fire-forget) method t class PingImpl implements Ping extends TypedActor { public void hit(int count) { - Pong pong = (Pong) context.getSender(); + Pong pong = (Pong) getContext().getSender(); pong.hit(count++); } } diff --git a/akka-docs/pending/typed-actors-scala.rst b/akka-docs/pending/typed-actors-scala.rst index 74a18527e9..3d03cc93b1 100644 --- a/akka-docs/pending/typed-actors-scala.rst +++ b/akka-docs/pending/typed-actors-scala.rst @@ -10,7 +10,7 @@ If you are using the `Spring Framework `_ then take a l Creating Typed Actors --------------------- -**IMPORTANT:** The Typed Actors class must have access modifier 'public' and can't be an inner class (unless it is an inner class in an 'object'). +**IMPORTANT:** The Typed Actors class must have access modifier 'public' (which is default) and can't be an inner class (unless it is an inner class in an 'object'). Akka turns POJOs with interface and implementation into asynchronous (Typed) Actors. Akka is using `AspectWerkz’s Proxy `_ implementation, which is the `most performant `_ proxy implementation there exists. @@ -22,6 +22,8 @@ If you have a POJO with an interface implementation separation like this: .. code-block:: scala + import akka.actor.TypedActor + trait RegistrationService { def register(user: User, cred: Credentials): Unit def getUserFor(username: String): User @@ -64,10 +66,12 @@ Configuration factory class Using a configuration object: .. code-block:: scala + import akka.actor.TypedActorConfiguration + import akka.util.Duration + import akka.util.duration._ - val config = new TypedActorConfiguration - .timeout(3000) - .makeTransactionRequired + val config = TypedActorConfiguration() + .timeout(3000 millis) val service = TypedActor.newInstance(classOf[RegistrationService], classOf[RegistrationServiceImpl], config)