Updated docs with changes to 'actorOf(Props(..))'

Signed-off-by: Jonas Bonér <jonas@jonasboner.com>
This commit is contained in:
Jonas Bonér 2011-12-13 14:53:18 +01:00
parent c9b787f029
commit d725c9c681
21 changed files with 100 additions and 98 deletions

View file

@ -328,7 +328,7 @@ created actor::
val clusterNode = Cluster.newNode(NodeAddress("test-cluster", "node1")).start
val hello = actorOf(Props[HelloActor].start.asInstanceOf[LocalActorRef]
val hello = actorOf(Props[HelloActor]).asInstanceOf[LocalActorRef]
val serializeMailbox = false
val nrOfInstances = 5

View file

@ -117,8 +117,7 @@ object Pi extends App {
val latch = new CountDownLatch(1)
// create the master
val master = actorOf(
new Master(nrOfWorkers, nrOfMessages, nrOfElements, latch))
val master = actorOf(Props(new Master(nrOfWorkers, nrOfMessages, nrOfElements, latch)))
// start the calculation
master ! Calculate

View file

@ -36,7 +36,7 @@ Example of creating a listener from Scala (from Java you just have to create an
case EventHandler.Debug(instance, message) => ...
case genericEvent => ...
}
})
}))
To add the listener:

View file

@ -221,6 +221,7 @@ We start by creating a ``Pi.java`` file and adding these import statements at th
import static akka.actor.Actors.poisonPill;
import static java.util.Arrays.asList;
import akka.actor.Props;
import akka.actor.ActorRef;
import akka.actor.UntypedActor;
import akka.actor.UntypedActorFactory;
@ -337,15 +338,15 @@ The master actor is a little bit more involved. In its constructor we need to cr
// create the workers
final ActorRef[] workers = new ActorRef[nrOfWorkers];
for (int i = 0; i < nrOfWorkers; i++) {
workers[i] = actorOf(Worker.class);
workers[i] = actorOf(new Props(Worker.class));
}
// wrap them with a load-balancing router
ActorRef router = actorOf(Props(new UntypedActorFactory() {
ActorRef router = actorOf(new Props(new UntypedActorFactory() {
public UntypedActor create() {
return new PiRouter(workers);
}
});
}));
}
}
@ -404,15 +405,15 @@ Here is the master actor::
// create the workers
final ActorRef[] workers = new ActorRef[nrOfWorkers];
for (int i = 0; i < nrOfWorkers; i++) {
workers[i] = actorOf(Worker.class);
workers[i] = actorOf(new Props(Worker.class));
}
// wrap them with a load-balancing router
router = actorOf(Props(new UntypedActorFactory() {
router = actorOf(new Props(new UntypedActorFactory() {
public UntypedActor create() {
return new PiRouter(workers);
}
});
}));
}
// message handler
@ -495,11 +496,11 @@ Now the only thing that is left to implement is the runner that should bootstrap
final CountDownLatch latch = new CountDownLatch(1);
// create the master
ActorRef master = actorOf(Props(new UntypedActorFactory() {
ActorRef master = actorOf(new Props(new UntypedActorFactory() {
public UntypedActor create() {
return new Master(nrOfWorkers, nrOfMessages, nrOfElements, latch);
}
});
}));
// start the calculation
master.tell(new Calculate());
@ -519,6 +520,7 @@ Before we package it up and run it, let's take a look at the full code now, with
import static akka.actor.Actors.poisonPill;
import static java.util.Arrays.asList;
import akka.actor.Props;
import akka.actor.ActorRef;
import akka.actor.UntypedActor;
import akka.actor.UntypedActorFactory;
@ -629,15 +631,15 @@ Before we package it up and run it, let's take a look at the full code now, with
// create the workers
final ActorRef[] workers = new ActorRef[nrOfWorkers];
for (int i = 0; i < nrOfWorkers; i++) {
workers[i] = actorOf(Worker.class);
workers[i] = actorOf(new Props(Worker.class));
}
// wrap them with a load-balancing router
router = actorOf(Props(new UntypedActorFactory() {
router = actorOf(new Props(new UntypedActorFactory() {
public UntypedActor create() {
return new PiRouter(workers);
}
});
}));
}
// message handler
@ -691,11 +693,11 @@ Before we package it up and run it, let's take a look at the full code now, with
final CountDownLatch latch = new CountDownLatch(1);
// create the master
ActorRef master = actorOf(Props(new UntypedActorFactory() {
ActorRef master = actorOf(new Props(new UntypedActorFactory() {
public UntypedActor create() {
return new Master(nrOfWorkers, nrOfMessages, nrOfElements, latch);
}
});
}));
// start the calculation
master.tell(new Calculate());

View file

@ -364,8 +364,7 @@ The ``Pi`` object is a perfect container module for our actors and messages, so
val latch = new CountDownLatch(1)
// create the master
val master = actorOf(
new Master(nrOfWorkers, nrOfMessages, nrOfElements, latch))
val master = actorOf(Props(new Master(nrOfWorkers, nrOfMessages, nrOfElements, latch)))
// start the calculation
master ! Calculate

View file

@ -318,7 +318,7 @@ Erlang actor's PID.
The actor's life-cycle is:
- Created & Started -- ``Actor.actorOf(Props[MyActor]`` -- can receive messages
- Created & Started -- ``Actor.actorOf(Props[MyActor])`` -- can receive messages
- Stopped -- ``actorRef.stop()`` -- can **not** receive messages
Once the actor has been stopped it is dead and can not be started again.

View file

@ -73,13 +73,14 @@ So your listener Actor needs to be able to handle these two messages. Example:
import akka.actor.TypedActorRegistered;
import akka.actor.TypedActorUnregistered;
import akka.actor.UntypedActor;
import akka.actor.Props;
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",
EventHandler.info(this, String.format("Actor registered: %s - %s",
event.actor().actorClassName(), event.actor().getUuid()));
event.actor().actorClassName(), event.actor().getUuid()));
} else if (message instanceof ActorUnregistered) {
@ -94,5 +95,5 @@ The above actor can be added as listener of registry events:
import static akka.actor.Actors.*;
ActorRef listener = actorOf(RegistryListener.class);
ActorRef listener = actorOf(new Props(RegistryListener.class));
registry().addListener(listener);

View file

@ -6,7 +6,7 @@ Dispatchers (Java)
.. sidebar:: Contents
.. contents:: :local:
Module stability: **SOLID**
The Dispatcher is an important piece that allows you to configure the right semantics and parameters for optimal performance, throughput and scalability. Different Actors have different needs.
@ -129,7 +129,7 @@ should have, as shown above. This defines the number of messages for a specific
Actor the dispatcher should process in one single sweep; in other words, the
dispatcher will bunch up to ``throughput`` message invocations together when
having elected an actor to run. Setting this to a higher number will increase
throughput but lower fairness, and vice versa. If you don't specify it explicitly
throughput but lower fairness, and vice versa. If you don't specify it explicitly
then it uses the value (5) defined for ``default-dispatcher`` in the :ref:`configuration`.
Browse the :ref:`scaladoc` or look at the code for all the options available.
@ -145,10 +145,10 @@ Creating a Dispatcher with a priority mailbox using PriorityGenerator:
.. code-block:: java
package some.pkg;
import akka.actor.*;
import akka.dispatch.*;
public class Main {
// A simple Actor that just prints the messages it processes
public static class MyActor extends UntypedActor {
@ -167,7 +167,7 @@ Creating a Dispatcher with a priority mailbox using PriorityGenerator:
}
public static void main(String[] args) {
// Create a new PriorityGenerator, lower prio means more important
// Create a new PriorityGenerator, lower prio means more important
PriorityGenerator gen = new PriorityGenerator() {
public int gen(Object message) {
if (message.equals("highpriority")) return 0; // "highpriority" messages should be treated first if possible
@ -177,7 +177,7 @@ Creating a Dispatcher with a priority mailbox using PriorityGenerator:
};
// We create an instance of the actor that will print out the messages it processes
// We create a new Priority dispatcher and seed it with the priority generator
ActorRef ref = Actors.actorOf((new Props()).withCreator(MyActor.class).withDispatcher(new Dispatcher("foo", 5, new UnboundedPriorityMailbox(gen))));
ActorRef ref = Actors.actorOf(new Props(MyActor.class).withDispatcher(new Dispatcher("foo", 5, new UnboundedPriorityMailbox(gen))));
}
}

View file

@ -27,8 +27,8 @@ An UntypedDispatcher is an actor that routes incoming messages to outbound actor
}
public class MyRouter extends UntypedRouter {
private ActorRef pinger = actorOf(Pinger.class);
private ActorRef ponger = actorOf(Ponger.class);
private ActorRef pinger = actorOf(new Props(Pinger.class));
private ActorRef ponger = actorOf(new Props(Ponger.class));
//Route Ping-messages to the pinger, and Pong-messages to the ponger
public ActorRef route(Object message) {
@ -38,7 +38,7 @@ An UntypedDispatcher is an actor that routes incoming messages to outbound actor
}
}
ActorRef router = actorOf(MyRouter.class);
ActorRef router = actorOf(new Props(MyRouter.class));
router.tell("Ping"); //Prints "Pinger: Ping"
router.tell("Pong"); //Prints "Ponger: Pong"
@ -71,8 +71,8 @@ An UntypedLoadBalancer is an actor that forwards messages it receives to a bound
//Our load balancer, sends messages to a pinger, then a ponger, rinse and repeat.
public class MyLoadBalancer extends UntypedLoadBalancer {
private InfiniteIterator<ActorRef> actors = new CyclicIterator<ActorRef>(asList(
actorOf(Pinger.class),
actorOf(Ponger.class)
actorOf(new Props(Pinger.class)),
actorOf(new Props(Ponger.class))
));
public InfiniteIterator<ActorRef> seq() {
@ -80,7 +80,7 @@ An UntypedLoadBalancer is an actor that forwards messages it receives to a bound
}
}
ActorRef balancer = actorOf(MyLoadBalancer.class);
ActorRef balancer = actorOf(new Props(MyLoadBalancer.class));
balancer.tell("Pong"); //Prints "Pinger: Pong"
balancer.tell("Ping"); //Prints "Ponger: Ping"
balancer.tell("Ping"); //Prints "Pinger: Ping"

View file

@ -6,7 +6,7 @@ Software Transactional Memory (Java)
.. sidebar:: Contents
.. contents:: :local:
Module stability: **SOLID**
Overview of STM
@ -287,7 +287,7 @@ Here is an example of using ``retry`` to block until an account has enough money
final Ref<Double> account1 = new Ref<Double>(100.0);
final Ref<Double> account2 = new Ref<Double>(100.0);
ActorRef transferer = Actors.actorOf(Transferer.class);
ActorRef transferer = Actors.actorOf(new Props(Transferer.class));
transferer.tell(new Transfer(account1, account2, 500.0));
// Transferer: not enough money - retrying
@ -410,7 +410,7 @@ You can also have two alternative blocking transactions, one of which can succee
final Ref<Integer> left = new Ref<Integer>(100);
final Ref<Integer> right = new Ref<Integer>(100);
ActorRef brancher = Actors.actorOf(Brancher.class);
ActorRef brancher = Actors.actorOf(new Props(Brancher.class));
brancher.tell(new Branch(left, right, 500));
// not enough on left - retrying

View file

@ -102,8 +102,8 @@ Here is an example of coordinating two simple counter UntypedActors so that they
.. code-block:: java
ActorRef counter1 = actorOf(Counter.class);
ActorRef counter2 = actorOf(Counter.class);
ActorRef counter1 = actorOf(new Props(Counter.class));
ActorRef counter2 = actorOf(new Props(Counter.class));
counter1.tell(new Coordinated(new Increment(counter2)));

View file

@ -4,7 +4,7 @@ Typed Actors (Java)
.. sidebar:: Contents
.. contents:: :local:
Module stability: **SOLID**
The Typed Actors are implemented through `Typed Actors <http://en.wikipedia.org/wiki/Active_object>`_. It uses AOP through `AspectWerkz <http://aspectwerkz.codehaus.org/>`_ to turn regular POJOs into asynchronous non-blocking Actors with semantics of the Actor Model. Each method dispatch is turned into a message that is put on a queue to be processed by the Typed Actor sequentially one by one.
@ -36,7 +36,7 @@ If you have a POJO with an interface implementation separation like this:
.. code-block:: java
import akka.actor.TypedActor;
public class RegistrationServiceImpl extends TypedActor implements RegistrationService {
public void register(User user, Credentials cred) {
... // register user
@ -137,7 +137,7 @@ Here is an example:
}
}
MathTypedActor math = TypedActor.actorOf(MathTypedActor .class, MathTypedActorImpl.class);
MathTypedActor math = TypedActor.typedActorOf(MathTypedActor.class, MathTypedActorImpl.class);
// This method will return immediately when called, caller should wait on the Future for the result
Future<Integer> future = math.square(10);

View file

@ -62,7 +62,7 @@ one-liner. Here's an example.
}
// start and expose actor via tcp
val myActor = actorOf(Props[MyActor]
val myActor = actorOf(Props[MyActor])
The above example exposes an actor over a tcp endpoint on port 6200 via Apache
Camel's `Mina component`_. The actor implements the endpointUri method to define
@ -362,7 +362,7 @@ after the ActorRef method returned.
import akka.actor.Actor._
val actor = actorOf(Props[Consumer1] // create Consumer actor and activate endpoint in background
val actor = actorOf(Props[Consumer1]) // create Consumer actor and activate endpoint in background
**Java**
@ -371,7 +371,7 @@ after the ActorRef method returned.
import static akka.actor.Actors.*;
import akka.actor.ActorRef;
ActorRef actor = actorOf(Consumer1.class); // create Consumer actor and activate endpoint in background
ActorRef actor = actorOf(new Props(Consumer1.class)); // create Consumer actor and activate endpoint in background
Typed actors
@ -544,7 +544,7 @@ still in progress after the ``ActorRef.stop`` method returned.
import akka.actor.Actor._
val actor = actorOf(Props[Consumer1] // create Consumer actor
val actor = actorOf(Props[Consumer1]) // create Consumer actor
actor // activate endpoint in background
// ...
actor.stop // deactivate endpoint in background
@ -556,7 +556,7 @@ still in progress after the ``ActorRef.stop`` method returned.
import static akka.actor.Actors.*;
import akka.actor.ActorRef;
ActorRef actor = actorOf(Consumer1.class); // create Consumer actor and activate endpoint in background
ActorRef actor = actorOf(new Props(Consumer1.class)); // create Consumer actor and activate endpoint in background
// ...
actor.stop(); // deactivate endpoint in background
@ -872,7 +872,7 @@ actor and register it at the remote server.
// ...
startCamelService
val consumer = val consumer = actorOf(Props[RemoteActor1]
val consumer = val consumer = actorOf(Props[RemoteActor1])
remote.start("localhost", 7777)
remote.register(consumer) // register and start remote consumer
@ -888,7 +888,7 @@ actor and register it at the remote server.
// ...
CamelServiceManager.startCamelService();
ActorRef actor = actorOf(RemoteActor1.class);
ActorRef actor = actorOf(new Props(RemoteActor1.class));
remote().start("localhost", 7777);
remote().register(actor); // register and start remote consumer
@ -1028,7 +1028,7 @@ used.
import akka.actor.Actor._
import akka.actor.ActorRef
val producer = actorOf(Props[Producer1]
val producer = actorOf(Props[Producer1])
val response = (producer ? "akka rocks").get
val body = response.bodyAs[String]
@ -1040,7 +1040,7 @@ used.
import static akka.actor.Actors.*;
import akka.camel.Message;
ActorRef producer = actorOf(Producer1.class);
ActorRef producer = actorOf(new Props(Producer1.class));
Message response = (Message)producer.sendRequestReply("akka rocks");
String body = response.getBodyAs(String.class)
@ -1156,7 +1156,7 @@ argument.
import akka.actor.ActorRef;
ActorRef target = ...
ActorRef producer = actorOf(Props(new Producer1Factory(target));
ActorRef producer = actorOf(Props(new Producer1Factory(target)));
producer;
Before producing messages to endpoints, producer actors can pre-process them by
@ -1946,7 +1946,7 @@ ends at the target actor.
import akka.camel.{Message, CamelContextManager, CamelServiceManager}
object CustomRouteExample extends Application {
val target = actorOf(Props[CustomRouteTarget]
val target = actorOf(Props[CustomRouteTarget])
CamelServiceManager.startCamelService
CamelContextManager.mandatoryContext.addRoutes(new CustomRouteBuilder(target.uuid))
@ -1982,7 +1982,7 @@ ends at the target actor.
public class CustomRouteExample {
public static void main(String... args) throws Exception {
ActorRef target = actorOf(CustomRouteTarget.class);
ActorRef target = actorOf(new Props(CustomRouteTarget.class));
CamelServiceManager.startCamelService();
CamelContextManager.getMandatoryContext().addRoutes(new CustomRouteBuilder(target.getUuid()));
}
@ -2545,9 +2545,9 @@ as shown in the following snippet (see also `sample.camel.Boot`_).
}
// Wire and start the example actors
val httpTransformer = actorOf(Props(new HttpTransformer)
val httpProducer = actorOf(Props(new HttpProducer(httpTransformer))
val httpConsumer = actorOf(Props(new HttpConsumer(httpProducer))
val httpTransformer = actorOf(Props(new HttpTransformer))
val httpProducer = actorOf(Props(new HttpProducer(httpTransformer)))
val httpConsumer = actorOf(Props(new HttpConsumer(httpProducer)))
The `jetty endpoints`_ of HttpConsumer and HttpProducer support asynchronous
in-out message exchanges and do not allocate threads for the full duration of
@ -2637,9 +2637,9 @@ follows.
CamelContextManager.init()
CamelContextManager.mandatoryContext.addRoutes(new CustomRouteBuilder)
val producer = actorOf(Props[Producer1]
val mediator = actorOf(Props(new Transformer(producer))
val consumer = actorOf(Props(new Consumer3(mediator))
val producer = actorOf(Props[Producer1])
val mediator = actorOf(Props(new Transformer(producer)))
val consumer = actorOf(Props(new Consumer3(mediator)))
}
class CustomRouteBuilder extends RouteBuilder {
@ -2741,11 +2741,11 @@ Wiring these actors to implement the above example is as simple as
// Setup publish/subscribe example
val jmsUri = "jms:topic:test"
val jmsSubscriber1 = actorOf(Props(new Subscriber("jms-subscriber-1", jmsUri))
val jmsSubscriber2 = actorOf(Props(new Subscriber("jms-subscriber-2", jmsUri))
val jmsPublisher = actorOf(Props(new Publisher("jms-publisher", jmsUri))
val jmsSubscriber1 = actorOf(Props(new Subscriber("jms-subscriber-1", jmsUri)))
val jmsSubscriber2 = actorOf(Props(new Subscriber("jms-subscriber-2", jmsUri)))
val jmsPublisher = actorOf(Props(new Publisher("jms-publisher", jmsUri)))
val jmsPublisherBridge = actorOf(Props(new PublisherBridge("jetty:http://0.0.0.0:8877/camel/pub/jms", jmsPublisher))
val jmsPublisherBridge = actorOf(Props(new PublisherBridge("jetty:http://0.0.0.0:8877/camel/pub/jms", jmsPublisher)))
}
To publish messages to subscribers one could of course also use the JMS API
@ -2838,10 +2838,10 @@ to be changed.
// Setup publish/subscribe example
val cometdUri = "cometd://localhost:8111/test/abc?resourceBase=target"
val cometdSubscriber = actorOf(Props(new Subscriber("cometd-subscriber", cometdUri))
val cometdPublisher = actorOf(Props(new Publisher("cometd-publisher", cometdUri))
val cometdSubscriber = actorOf(Props(new Subscriber("cometd-subscriber", cometdUri)))
val cometdPublisher = actorOf(Props(new Publisher("cometd-publisher", cometdUri)))
val cometdPublisherBridge = actorOf(Props(new PublisherBridge("jetty:http://0.0.0.0:8877/camel/pub/cometd", cometdPublisher))
val cometdPublisherBridge = actorOf(Props(new PublisherBridge("jetty:http://0.0.0.0:8877/camel/pub/cometd", cometdPublisher)))
}
@ -2884,7 +2884,7 @@ seconds:
startCamelService
// create and start a quartz actor
val myActor = actorOf(Props[MyQuartzActor]
val myActor = actorOf(Props[MyQuartzActor])
} // end main

View file

@ -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(Props[RegistryListener]
val listener = actorOf(Props[RegistryListener])
registry.addListener(listener)

View file

@ -54,7 +54,7 @@ Creating Actors with default constructor
----------------------------------------
.. includecode:: code/ActorDocSpec.scala
:include: imports2,system-actorOf
:include: imports2,system-actorOf
The call to :meth:`actorOf` returns an instance of ``ActorRef``. This is a handle to
the ``Actor`` instance which you can use to interact with the ``Actor``. The
@ -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(Props[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:

View file

@ -253,7 +253,5 @@ class ActorDocSpec extends AkkaSpec(Map("akka.loglevel" -> "INFO")) {
//#hot-swap-actor
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(Props(new Actor { def receive = { case x => println("Pinger: " + x) } })
val ponger = actorOf(Props(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(Props(new Actor { def receive = { case x => println("Pinger: " + x) } })
val ponger = actorOf(Props(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(Props[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(Props(new Actor { def receive = { case x => println("Pinger: " + x) } })
val ponger = actorOf(Props(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(Props(new Actor { def receive = { case x => println("Pinger: " + x) } })
val ponger = actorOf(Props(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(Props[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(Props(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(Props(new Brancher)
val brancher = Actor.actorOf(Props(new Brancher))
brancher ! Branch(ref1, ref2, 1)
// INFO Brancher: not enough on left - retrying

View file

@ -236,7 +236,7 @@ common task easy:
"send back messages unchanged" in {
val echo = Actor.actorOf(Props[EchoActor]
val echo = Actor.actorOf(Props[EchoActor])
echo ! "hello world"
expectMsg("hello world")
@ -431,7 +431,7 @@ maximum time bound, the overall block may take arbitrarily longer in this case.
class SomeSpec extends WordSpec with MustMatchers with TestKit {
"A Worker" must {
"send timely replies" in {
val worker = actorOf(...)
val worker = ActorSystem().actorOf(...)
within (500 millis) {
worker ! "some work"
expectMsg("some result")
@ -471,7 +471,7 @@ simply mix in `ÌmplicitSender`` into your test.
class SomeSpec extends WordSpec with MustMatchers with TestKit with ImplicitSender {
"A Worker" must {
"send timely replies" in {
val worker = actorOf(...)
val worker = ActorSystem().actorOf(...)
within (500 millis) {
worker ! "some work" // testActor is the "sender" for this message
expectMsg("some result")
@ -506,7 +506,7 @@ using a small example::
val probe1 = TestProbe()
val probe2 = TestProbe()
val actor = Actor.actorOf(Props[MyDoubleEcho]
val actor = ActorSystem().actorOf(Props[MyDoubleEcho])
actor ! (probe1.ref, probe2.ref)
actor ! "hello"
probe1.expectMsg(50 millis, "hello")
@ -553,8 +553,9 @@ concerning volume and timing of the message flow while still keeping the
network functioning::
val probe = TestProbe()
val source = Actor.actorOf(Props(new Source(probe))
val dest = Actor.actorOf(Props[Destination]
val system = ActorSystem()
val source = system.actorOf(Props(new Source(probe)))
val dest = system.actorOf(Props[Destination])
source ! "start"
probe.expectMsg("work")
probe.forward(dest)
@ -613,7 +614,7 @@ or from the client code
.. code-block:: scala
val ref = Actor.actorOf(Props[MyActor].withDispatcher(CallingThreadDispatcher.global))
val ref = system.actorOf(Props[MyActor].withDispatcher(CallingThreadDispatcher.global))
As the :class:`CallingThreadDispatcher` does not have any configurable state,
you may always use the (lazily) preallocated one as shown in the examples.

View file

@ -24,14 +24,16 @@ Ray Roestenburg's example code from `his blog <http://roestenburg.agilesquad.com
*/
class TestKitUsageSpec extends WordSpec with BeforeAndAfterAll with ShouldMatchers with TestKit {
val echoRef = actorOf(Props(new EchoActor)
val forwardRef = actorOf(Props(new ForwardingActor(testActor))
val filterRef = actorOf(Props(new FilteringActor(testActor))
val system = ActorSystem()
import system._
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(Props(new SequencingActor(testActor, headList, tailList))
val seqRef = actorOf(Props(new SequencingActor(testActor, headList, tailList)))
override protected def afterAll(): scala.Unit = {
stopTestActor

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(Props[Counter]
val counter2 = Actor.actorOf(Props[Counter]
val counter1 = Actor.actorOf(Props[Counter])
val counter2 = Actor.actorOf(Props[Counter])
counter1 ! Coordinated(Increment(Some(counter2)))