Removed all 'actorOf' methods that does not take a 'Props', and changed all callers to use 'actorOf(Props(..))'

Signed-off-by: Jonas Bonér <jonas@jonasboner.com>
This commit is contained in:
Jonas Bonér 2011-12-13 14:09:40 +01:00
parent 86a5114d79
commit c9b787f029
85 changed files with 464 additions and 518 deletions

View file

@ -88,7 +88,7 @@ So your listener Actor needs to be able to handle these messages. Example:
class RegistryListener extends Actor {
def receive = {
case event: ActorRegistered =>
EventHandler.info(this, "Actor registered: %s - %s".format(
EventHandler.info(this, "Actor registered: %s - %s".format(
event.actor.actorClassName, event.actor.uuid))
case event: ActorUnregistered =>
// ...
@ -102,5 +102,5 @@ The above actor can be added as listener of registry events:
import akka.actor._
import akka.actor.Actor._
val listener = actorOf[RegistryListener]
val listener = actorOf(Props[RegistryListener]
registry.addListener(listener)

View file

@ -73,7 +73,7 @@ a top level actor, that is supervised by the system (internal guardian actor).
.. includecode:: code/ActorDocSpec.scala#context-actorOf
Actors are automatically started asynchronously when created.
When you create the ``Actor`` then it will automatically call the ``preStart``
When you create the ``Actor`` then it will automatically call the ``preStart``
callback method on the ``Actor`` trait. This is an excellent place to
add initialization code for the actor.
@ -87,7 +87,7 @@ Creating Actors with non-default constructor
--------------------------------------------
If your Actor has a constructor that takes parameters then you can't create it
using ``actorOf[TYPE]``. Instead you can use a variant of ``actorOf`` that takes
using ``actorOf(Props[TYPE]``. Instead you can use a variant of ``actorOf`` that takes
a call-by-name block in which you can create the Actor in any way you like.
Here is an example:
@ -98,7 +98,7 @@ Here is an example:
Creating Actors with Props
--------------------------
``Props`` is a configuration object to specify additional things for the actor to
``Props`` is a configuration object to specify additional things for the actor to
be created, such as the ``MessageDispatcher``.
.. includecode:: code/ActorDocSpec.scala#creating-props
@ -128,7 +128,7 @@ Actor API
The :class:`Actor` trait defines only one abstract method, the above mentioned
:meth:`receive`, which implements the behavior of the actor.
If the current actor behavior does not match a received message, :meth:`unhandled`
If the current actor behavior does not match a received message, :meth:`unhandled`
is called, which by default throws an :class:`UnhandledMessageException`.
In addition, it offers:
@ -145,7 +145,7 @@ In addition, it offers:
You can import the members in the :obj:`context` to avoid prefixing access with ``context.``
.. includecode:: code/ActorDocSpec.scala#import-context
.. includecode:: code/ActorDocSpec.scala#import-context
The remaining visible methods are user-overridable life-cycle hooks which are
described in the following::
@ -195,7 +195,7 @@ processing a message. This restart involves the hooks mentioned above:
An actor restart replaces only the actual actor object; the contents of the
mailbox and the hotswap stack are unaffected by the restart, so processing of
messages will resume after the :meth:`postRestart` hook returns. The message
messages will resume after the :meth:`postRestart` hook returns. The message
that triggered the exception will not be received again. Any message
sent to an actor while it is being restarted will be queued to its mailbox as
usual.
@ -205,9 +205,9 @@ Stop Hook
After stopping an actor, its :meth:`postStop` hook is called, which may be used
e.g. for deregistering this actor from other services. This hook is guaranteed
to run after message queuing has been disabled for this actor, i.e. messages
sent to a stopped actor will be redirected to the :obj:`deadLetters` of the
:obj:`ActorSystem`.
to run after message queuing has been disabled for this actor, i.e. messages
sent to a stopped actor will be redirected to the :obj:`deadLetters` of the
:obj:`ActorSystem`.
Identifying Actors
@ -267,7 +267,7 @@ implicitly passed along with the message and available to the receiving Actor
in its ``sender: ActorRef`` member field. The target actor can use this
to reply to the original sender, by using ``sender ! replyMsg``.
If invoked from an instance that is **not** an Actor the sender will be
If invoked from an instance that is **not** an Actor the sender will be
:obj:`deadLetters` actor reference by default.
Ask: Send-And-Receive-Future
@ -281,11 +281,11 @@ will immediately return a :class:`Future`:
val future = actor ? "hello"
The receiving actor should reply to this message, which will complete the
future with the reply message as value; ``sender ! result``.
future with the reply message as value; ``sender ! result``.
To complete the future with an exception you need send a Failure message to the sender.
This is not done automatically when an actor throws an exception while processing a
message.
To complete the future with an exception you need send a Failure message to the sender.
This is not done automatically when an actor throws an exception while processing a
message.
.. includecode:: code/ActorDocSpec.scala#reply-exception
@ -296,7 +296,7 @@ which is taken from one of the following locations in order of precedence:
#. implicit argument of type :class:`akka.actor.Timeout`, e.g.
::
import akka.actor.Timeout
import akka.util.duration._
@ -306,16 +306,16 @@ which is taken from one of the following locations in order of precedence:
See :ref:`futures-scala` for more information on how to await or query a
future.
The ``onComplete``, ``onResult``, or ``onTimeout`` methods of the ``Future`` can be
used to register a callback to get a notification when the Future completes.
The ``onComplete``, ``onResult``, or ``onTimeout`` methods of the ``Future`` can be
used to register a callback to get a notification when the Future completes.
Gives you a way to avoid blocking.
.. warning::
When using future callbacks, inside actors you need to carefully avoid closing over
the containing actors reference, i.e. do not call methods or access mutable state
on the enclosing actor from within the callback. This would break the actor
encapsulation and may introduce synchronization bugs and race conditions because
the containing actors reference, i.e. do not call methods or access mutable state
on the enclosing actor from within the callback. This would break the actor
encapsulation and may introduce synchronization bugs and race conditions because
the callback will be scheduled concurrently to the enclosing actor. Unfortunately
there is not yet a way to detect these illegal accesses at compile time.
See also: :ref:`jmm-shared-state`
@ -403,17 +403,17 @@ object.
Stopping actors
===============
Actors are stopped by invoking the ``stop`` method of the ``ActorRef``.
Actors are stopped by invoking the ``stop`` method of the ``ActorRef``.
The actual termination of the actor is performed asynchronously, i.e.
``stop`` may return before the actor is stopped.
``stop`` may return before the actor is stopped.
.. code-block:: scala
actor.stop()
Processing of the current message, if any, will continue before the actor is stopped,
Processing of the current message, if any, will continue before the actor is stopped,
but additional messages in the mailbox will not be processed. By default these
messages are sent to the :obj:`deadLetters` of the :obj:`ActorSystem`, but that
messages are sent to the :obj:`deadLetters` of the :obj:`ActorSystem`, but that
depends on the mailbox implementation.
When stop is called then a call to the ``def postStop`` callback method will
@ -540,11 +540,11 @@ messages on that mailbox, will be there as well.
What happens to the actor
-------------------------
If an exception is thrown, the actor instance is discarded and a new instance is
If an exception is thrown, the actor instance is discarded and a new instance is
created. This new instance will now be used in the actor references to this actor
(so this is done invisible to the developer). Note that this means that current
state of the failing actor instance is lost if you don't store and restore it in
``preRestart`` and ``postRestart`` callbacks.
(so this is done invisible to the developer). Note that this means that current
state of the failing actor instance is lost if you don't store and restore it in
``preRestart`` and ``postRestart`` callbacks.
Extending Actors using PartialFunction chaining

View file

@ -2,6 +2,7 @@ package akka.docs.actor
//#imports1
import akka.actor.Actor
import akka.actor.Props
import akka.event.Logging
//#imports1
@ -29,12 +30,12 @@ case class Message(s: String)
//#context-actorOf
class FirstActor extends Actor {
val myActor = context.actorOf[MyActor]
val myActor = context.actorOf(Props[MyActor])
//#context-actorOf
//#anonymous-actor
def receive = {
case m: DoIt
context.actorOf(new Actor {
context.actorOf(Props(new Actor {
def receive = {
case DoIt(msg)
val replyMsg = doSomeDangerousWork(msg)
@ -42,7 +43,7 @@ class FirstActor extends Actor {
self.stop()
}
def doSomeDangerousWork(msg: ImmutableMessage): String = { "done" }
}) ! m
})) ! m
case replyMsg: String sender ! replyMsg
}
@ -52,7 +53,7 @@ class FirstActor extends Actor {
//#system-actorOf
object Main extends App {
val system = ActorSystem("MySystem")
val myActor = system.actorOf[MyActor]
val myActor = system.actorOf(Props[MyActor])
//#system-actorOf
}
@ -94,7 +95,7 @@ class Swapper extends Actor {
object SwapperApp extends App {
val system = ActorSystem("SwapperSystem")
val swap = system.actorOf[Swapper]
val swap = system.actorOf(Props[Swapper])
swap ! Swap // logs Hi
swap ! Swap // logs Ho
swap ! Swap // logs Hi
@ -134,20 +135,20 @@ class ActorDocSpec extends AkkaSpec(Map("akka.loglevel" -> "INFO")) {
//#import-context
class FirstActor extends Actor {
import context._
val myActor = actorOf[MyActor]
val myActor = actorOf(Props[MyActor])
def receive = {
case x myActor ! x
}
}
//#import-context
val first = system.actorOf(new FirstActor)
val first = system.actorOf(Props(new FirstActor))
first.stop()
}
"creating actor with AkkaSpec.actorOf" in {
val myActor = system.actorOf[MyActor]
val myActor = system.actorOf(Props[MyActor])
// testing the actor
@ -178,7 +179,7 @@ class ActorDocSpec extends AkkaSpec(Map("akka.loglevel" -> "INFO")) {
//#creating-constructor
// allows passing in arguments to the MyActor constructor
val myActor = system.actorOf(new MyActor("..."))
val myActor = system.actorOf(Props(new MyActor("...")))
//#creating-constructor
myActor.stop()
@ -203,7 +204,7 @@ class ActorDocSpec extends AkkaSpec(Map("akka.loglevel" -> "INFO")) {
}
}
val myActor = system.actorOf(new MyActor)
val myActor = system.actorOf(Props(new MyActor))
implicit val timeout = system.settings.ActorTimeout
val future = myActor ? "hello"
future.as[String] match {
@ -251,7 +252,7 @@ class ActorDocSpec extends AkkaSpec(Map("akka.loglevel" -> "INFO")) {
}
//#hot-swap-actor
val actor = system.actorOf(new HotSwapActor)
val actor = system.actorOf(Props(new HotSwapActor))
}

View file

@ -26,8 +26,8 @@ To use it you can either create a Router through the ``routerActor()`` factory m
//Two actors, one named Pinger and one named Ponger
//The actor(pf) method creates an anonymous actor and starts it
val pinger = actorOf(new Actor { def receive = { case x => println("Pinger: " + x) } })
val ponger = actorOf(new Actor { def receive = { case x => println("Ponger: " + x) } })
val pinger = actorOf(Props(new Actor { def receive = { case x => println("Pinger: " + x) } })
val ponger = actorOf(Props(new Actor { def receive = { case x => println("Ponger: " + x) } })
//A router that dispatches Ping messages to the pinger
//and Pong messages to the ponger
@ -53,8 +53,8 @@ Or by mixing in akka.routing.Router:
class MyRouter extends Actor with Router {
//Our pinger and ponger actors
val pinger = actorOf(new Actor { def receive = { case x => println("Pinger: " + x) } })
val ponger = actorOf(new Actor { def receive = { case x => println("Ponger: " + x) } })
val pinger = actorOf(Props(new Actor { def receive = { case x => println("Pinger: " + x) } })
val ponger = actorOf(Props(new Actor { def receive = { case x => println("Ponger: " + x) } })
//When we get a ping, we dispatch to the pinger
//When we get a pong, we dispatch to the ponger
def routes = {
@ -64,7 +64,7 @@ Or by mixing in akka.routing.Router:
}
//Create an instance of our router, and start it
val d = actorOf[MyRouter]
val d = actorOf(Props[MyRouter]
d ! Ping //Prints "Pinger: Ping"
d ! Pong //Prints "Ponger: Pong"
@ -90,8 +90,8 @@ Example using the ``loadBalancerActor()`` factory method:
//Two actors, one named Pinger and one named Ponger
//The actor(pf) method creates an anonymous actor and starts it
val pinger = actorOf(new Actor { def receive = { case x => println("Pinger: " + x) } })
val ponger = actorOf(new Actor { def receive = { case x => println("Ponger: " + x) } })
val pinger = actorOf(Props(new Actor { def receive = { case x => println("Pinger: " + x) } })
val ponger = actorOf(Props(new Actor { def receive = { case x => println("Ponger: " + x) } })
//A load balancer that given a sequence of actors dispatches them accordingly
//a CyclicIterator works in a round-robin-fashion
@ -117,14 +117,14 @@ Or by mixing in akka.routing.LoadBalancer
//A load balancer that balances between a pinger and a ponger
class MyLoadBalancer extends Actor with LoadBalancer {
val pinger = actorOf(new Actor { def receive = { case x => println("Pinger: " + x) } })
val ponger = actorOf(new Actor { def receive = { case x => println("Ponger: " + x) } })
val pinger = actorOf(Props(new Actor { def receive = { case x => println("Pinger: " + x) } })
val ponger = actorOf(Props(new Actor { def receive = { case x => println("Ponger: " + x) } })
val seq = new CyclicIterator[ActorRef](List(pinger,ponger))
}
//Create an instance of our loadbalancer, and start it
val d = actorOf[MyLoadBalancer]
val d = actorOf(Props[MyLoadBalancer]
d ! Pong //Prints "Pinger: Pong"
d ! Pong //Prints "Ponger: Pong"

View file

@ -371,7 +371,7 @@ Here is an example of using ``retry`` to block until an account has enough money
val account1 = Ref(100.0)
val account2 = Ref(100.0)
val transferer = Actor.actorOf(new Transferer)
val transferer = Actor.actorOf(Props(new Transferer)
transferer ! Transfer(account1, account2, 500.0)
// INFO Transferer: not enough money - retrying
@ -427,7 +427,7 @@ You can also have two alternative blocking transactions, one of which can succee
val ref1 = Ref(0)
val ref2 = Ref(0)
val brancher = Actor.actorOf(new Brancher)
val brancher = Actor.actorOf(Props(new Brancher)
brancher ! Branch(ref1, ref2, 1)
// INFO Brancher: not enough on left - retrying

View file

@ -106,13 +106,13 @@ and in addition allows access to the internal state::
case Ev("back") => goto(1) using "back"
}
})
assert (fsm.stateName == 1)
assert (fsm.stateData == "")
fsm ! "go" // being a TestActorRef, this runs also on the CallingThreadDispatcher
assert (fsm.stateName == 2)
assert (fsm.stateData == "go")
fsm.setState(stateName = 1)
assert (fsm.stateName == 1)
@ -235,8 +235,8 @@ common task easy:
"An Echo actor" must {
"send back messages unchanged" in {
val echo = Actor.actorOf[EchoActor]
val echo = Actor.actorOf(Props[EchoActor]
echo ! "hello world"
expectMsg("hello world")
@ -352,11 +352,11 @@ with message flows:
* :meth:`receiveWhile[T](max: Duration, idle: Duration)(pf: PartialFunction[Any, T]): Seq[T]`
Collect messages as long as
* they are matching the given partial function
* the given time interval is not used up
* the next message is received within the idle timeout
All collected messages are returned. The maximum duration defaults to the
time remaining in the innermost enclosing :ref:`within <TestKit.within>`
block and the idle duration defaults to infinity (thereby disabling the
@ -370,7 +370,7 @@ with message flows:
:ref:`within <TestKit.within>` block.
* :meth:`ignoreMsg(pf: PartialFunction[AnyRef, Boolean])`
:meth:`ignoreNoMsg`
The internal :obj:`testActor` contains a partial function for ignoring
@ -506,7 +506,7 @@ using a small example::
val probe1 = TestProbe()
val probe2 = TestProbe()
val actor = Actor.actorOf[MyDoubleEcho]
val actor = Actor.actorOf(Props[MyDoubleEcho]
actor ! (probe1.ref, probe2.ref)
actor ! "hello"
probe1.expectMsg(50 millis, "hello")
@ -553,8 +553,8 @@ concerning volume and timing of the message flow while still keeping the
network functioning::
val probe = TestProbe()
val source = Actor.actorOf(new Source(probe))
val dest = Actor.actorOf[Destination]
val source = Actor.actorOf(Props(new Source(probe))
val dest = Actor.actorOf(Props[Destination]
source ! "start"
probe.expectMsg("work")
probe.forward(dest)
@ -710,7 +710,7 @@ by debuggers as well as logging, where the Akka toolkit offers the following
options:
* *Logging of exceptions thrown within Actor instances*
This is always on; in contrast to the other logging mechanisms, this logs at
``ERROR`` level.
@ -723,7 +723,7 @@ options:
import akka.event.LoggingReceive
def receive = LoggingReceive(this) {
case msg => ...
}
}
The first argument to :meth:`LoggingReceive` defines the source to be used in the
logging events, which should be the current actor.

View file

@ -9,7 +9,7 @@ Ray Roestenburg's example code from `his blog <http://roestenburg.agilesquad.com
.. code-block:: scala
package unit.akka
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{WordSpec, BeforeAndAfterAll}
import akka.actor.Actor._
@ -18,21 +18,21 @@ Ray Roestenburg's example code from `his blog <http://roestenburg.agilesquad.com
import java.util.concurrent.TimeUnit
import akka.actor.{ActorRef, Actor}
import util.Random
/**
* a Test to show some TestKit examples
*/
class TestKitUsageSpec extends WordSpec with BeforeAndAfterAll with ShouldMatchers with TestKit {
val echoRef = actorOf(new EchoActor)
val forwardRef = actorOf(new ForwardingActor(testActor))
val filterRef = actorOf(new FilteringActor(testActor))
val echoRef = actorOf(Props(new EchoActor)
val forwardRef = actorOf(Props(new ForwardingActor(testActor))
val filterRef = actorOf(Props(new FilteringActor(testActor))
val randomHead = Random.nextInt(6)
val randomTail = Random.nextInt(10)
val headList = List().padTo(randomHead, "0")
val tailList = List().padTo(randomTail, "1")
val seqRef = actorOf(new SequencingActor(testActor, headList, tailList))
val seqRef = actorOf(Props(new SequencingActor(testActor, headList, tailList))
override protected def afterAll(): scala.Unit = {
stopTestActor
echoRef.stop()
@ -40,7 +40,7 @@ Ray Roestenburg's example code from `his blog <http://roestenburg.agilesquad.com
filterRef.stop()
seqRef.stop()
}
"An EchoActor" should {
"Respond with the same message it receives" in {
within(100 millis) {
@ -70,7 +70,7 @@ Ray Roestenburg's example code from `his blog <http://roestenburg.agilesquad.com
filterRef ! 1
filterRef ! "text"
filterRef ! 1
receiveWhile(500 millis) {
case msg: String => messages = msg :: messages
}
@ -95,7 +95,7 @@ Ray Roestenburg's example code from `his blog <http://roestenburg.agilesquad.com
}
}
}
/**
* An Actor that echoes everything you send to it
*/
@ -106,7 +106,7 @@ Ray Roestenburg's example code from `his blog <http://roestenburg.agilesquad.com
}
}
}
/**
* An Actor that forwards every message to a next Actor
*/
@ -117,7 +117,7 @@ Ray Roestenburg's example code from `his blog <http://roestenburg.agilesquad.com
}
}
}
/**
* An Actor that only forwards certain messages to a next Actor
*/
@ -129,7 +129,7 @@ Ray Roestenburg's example code from `his blog <http://roestenburg.agilesquad.com
case _ => None
}
}
/**
* An actor that sends a sequence of messages with a random head list, an interesting value and a random tail list
* The idea is that you would like to test that the interesting value is received and that you cant be bothered with the rest

View file

@ -64,8 +64,8 @@ Here is an example of coordinating two simple counter Actors so that they both i
}
}
val counter1 = Actor.actorOf[Counter]
val counter2 = Actor.actorOf[Counter]
val counter1 = Actor.actorOf(Props[Counter]
val counter2 = Actor.actorOf(Props[Counter]
counter1 ! Coordinated(Increment(Some(counter2)))