2013-04-06 16:22:30 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.contrib.pattern;
|
|
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
import akka.testkit.AkkaJUnitActorSystemResource;
|
|
|
|
|
import org.junit.ClassRule;
|
2013-04-06 16:22:30 +02:00
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.actor.Props;
|
|
|
|
|
import akka.actor.UntypedActor;
|
|
|
|
|
import akka.event.Logging;
|
|
|
|
|
import akka.event.LoggingAdapter;
|
|
|
|
|
|
|
|
|
|
public class DistributedPubSubMediatorTest {
|
|
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
@ClassRule
|
|
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource =
|
|
|
|
|
new AkkaJUnitActorSystemResource("DistributedPubSubMediatorTest");
|
2013-04-06 16:22:30 +02:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
private final ActorSystem system = actorSystemResource.getSystem();
|
2013-04-06 16:22:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void demonstrateUsage() {
|
|
|
|
|
//#start-subscribers
|
2013-04-17 22:14:19 +02:00
|
|
|
system.actorOf(Props.create(Subscriber.class), "subscriber1");
|
2013-04-06 16:22:30 +02:00
|
|
|
//another node
|
2013-04-17 22:14:19 +02:00
|
|
|
system.actorOf(Props.create(Subscriber.class), "subscriber2");
|
|
|
|
|
system.actorOf(Props.create(Subscriber.class), "subscriber3");
|
2013-04-06 16:22:30 +02:00
|
|
|
//#start-subscribers
|
|
|
|
|
|
|
|
|
|
//#publish-message
|
|
|
|
|
//somewhere else
|
2013-04-17 22:14:19 +02:00
|
|
|
ActorRef publisher = system.actorOf(Props.create(Publisher.class), "publisher");
|
2013-04-06 16:22:30 +02:00
|
|
|
// after a while the subscriptions are replicated
|
|
|
|
|
publisher.tell("hello", null);
|
|
|
|
|
//#publish-message
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static//#subscriber
|
|
|
|
|
public class Subscriber extends UntypedActor {
|
|
|
|
|
LoggingAdapter log = Logging.getLogger(getContext().system(), this);
|
|
|
|
|
|
|
|
|
|
public Subscriber() {
|
2013-04-17 22:14:19 +02:00
|
|
|
ActorRef mediator = DistributedPubSubExtension.get(getContext().system()).mediator();
|
2013-04-06 16:22:30 +02:00
|
|
|
// subscribe to the topic named "content"
|
2013-04-17 22:14:19 +02:00
|
|
|
mediator.tell(new DistributedPubSubMediator.Subscribe("content", getSelf()), getSelf());
|
2013-04-06 16:22:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onReceive(Object msg) {
|
|
|
|
|
if (msg instanceof String)
|
|
|
|
|
log.info("Got: {}", msg);
|
|
|
|
|
else if (msg instanceof DistributedPubSubMediator.SubscribeAck)
|
|
|
|
|
log.info("subscribing");
|
|
|
|
|
else
|
|
|
|
|
unhandled(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#subscriber
|
|
|
|
|
|
|
|
|
|
static//#publisher
|
|
|
|
|
public class Publisher extends UntypedActor {
|
|
|
|
|
|
|
|
|
|
// activate the extension
|
2013-04-17 22:14:19 +02:00
|
|
|
ActorRef mediator = DistributedPubSubExtension.get(getContext().system()).mediator();
|
2013-04-06 16:22:30 +02:00
|
|
|
|
|
|
|
|
public void onReceive(Object msg) {
|
|
|
|
|
if (msg instanceof String) {
|
|
|
|
|
String in = (String) msg;
|
|
|
|
|
String out = in.toUpperCase();
|
2013-04-17 22:14:19 +02:00
|
|
|
mediator.tell(new DistributedPubSubMediator.Publish("content", out), getSelf());
|
2013-04-06 16:22:30 +02:00
|
|
|
} else {
|
|
|
|
|
unhandled(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#publisher
|
|
|
|
|
}
|