2011-04-09 19:55:46 -06:00
|
|
|
Routing (Java)
|
|
|
|
|
==============
|
|
|
|
|
|
2011-05-06 10:09:16 +02:00
|
|
|
UntypedDispatcher
|
|
|
|
|
-----------------
|
2011-04-09 19:55:46 -06:00
|
|
|
|
|
|
|
|
An UntypedDispatcher is an actor that routes incoming messages to outbound actors.
|
|
|
|
|
|
|
|
|
|
.. code-block:: java
|
|
|
|
|
|
|
|
|
|
import static akka.actor.Actors.*;
|
|
|
|
|
import akka.actor.*;
|
|
|
|
|
import akka.routing.*;
|
|
|
|
|
|
|
|
|
|
//A Pinger is an UntypedActor that prints "Pinger: <message>"
|
|
|
|
|
class Pinger extends UntypedActor {
|
|
|
|
|
public void onReceive(Object message) throws Exception {
|
|
|
|
|
System.out.println("Pinger: " + message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//A Ponger is an UntypedActor that prints "Ponger: <message>"
|
|
|
|
|
class Ponger extends UntypedActor {
|
|
|
|
|
public void onReceive(Object message) throws Exception {
|
|
|
|
|
System.out.println("Ponger: " + message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-20 12:29:48 +02:00
|
|
|
public class MyRouter extends UntypedRouter {
|
2011-09-08 11:02:17 +02:00
|
|
|
private ActorRef pinger = actorOf(Pinger.class);
|
|
|
|
|
private ActorRef ponger = actorOf(Ponger.class);
|
2011-04-09 19:55:46 -06:00
|
|
|
|
|
|
|
|
//Route Ping-messages to the pinger, and Pong-messages to the ponger
|
|
|
|
|
public ActorRef route(Object message) {
|
|
|
|
|
if("Ping".equals(message)) return pinger;
|
|
|
|
|
else if("Pong".equals(message)) return ponger;
|
|
|
|
|
else throw new IllegalArgumentException("I do not understand " + message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-08 11:02:17 +02:00
|
|
|
ActorRef router = actorOf(MyRouter.class);
|
2011-08-01 17:07:12 +02:00
|
|
|
router.tell("Ping"); //Prints "Pinger: Ping"
|
|
|
|
|
router.tell("Pong"); //Prints "Ponger: Pong"
|
2011-04-09 19:55:46 -06:00
|
|
|
|
2011-05-06 10:09:16 +02:00
|
|
|
UntypedLoadBalancer
|
|
|
|
|
-------------------
|
2011-04-09 19:55:46 -06:00
|
|
|
|
|
|
|
|
An UntypedLoadBalancer is an actor that forwards messages it receives to a boundless sequence of destination actors.
|
|
|
|
|
|
|
|
|
|
.. code-block:: java
|
|
|
|
|
|
|
|
|
|
import static akka.actor.Actors.*;
|
|
|
|
|
import akka.actor.*;
|
|
|
|
|
import akka.routing.*;
|
|
|
|
|
import static java.util.Arrays.asList;
|
|
|
|
|
|
|
|
|
|
//A Pinger is an UntypedActor that prints "Pinger: <message>"
|
|
|
|
|
class Pinger extends UntypedActor {
|
|
|
|
|
public void onReceive(Object message) throws Exception {
|
|
|
|
|
System.out.println("Pinger: " + message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//A Ponger is an UntypedActor that prints "Ponger: <message>"
|
|
|
|
|
class Ponger extends UntypedActor {
|
|
|
|
|
public void onReceive(Object message) throws Exception {
|
|
|
|
|
System.out.println("Ponger: " + message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//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(
|
2011-09-08 11:02:17 +02:00
|
|
|
actorOf(Pinger.class),
|
|
|
|
|
actorOf(Ponger.class)
|
2011-04-09 19:55:46 -06:00
|
|
|
));
|
|
|
|
|
|
|
|
|
|
public InfiniteIterator<ActorRef> seq() {
|
|
|
|
|
return actors;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-08 11:02:17 +02:00
|
|
|
ActorRef balancer = actorOf(MyLoadBalancer.class);
|
2011-08-01 17:07:12 +02:00
|
|
|
balancer.tell("Pong"); //Prints "Pinger: Pong"
|
|
|
|
|
balancer.tell("Ping"); //Prints "Ponger: Ping"
|
|
|
|
|
balancer.tell("Ping"); //Prints "Pinger: Ping"
|
|
|
|
|
balancer.tell("Pong"); //Prints "Ponger: Pong
|
2011-04-09 19:55:46 -06:00
|
|
|
|
|
|
|
|
You can also send a 'new Routing.Broadcast(msg)' message to the router to have it be broadcasted out to all the actors it represents.
|
|
|
|
|
|
2011-05-06 10:09:16 +02:00
|
|
|
.. code-block:: java
|
|
|
|
|
|
2011-08-01 17:07:12 +02:00
|
|
|
balancer.tell(new Routing.Broadcast(new PoisonPill()));
|
2011-05-06 10:09:16 +02:00
|
|
|
|