2013-04-06 16:22:30 +02:00
|
|
|
/**
|
2017-01-04 17:37:10 +01:00
|
|
|
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
|
2013-04-06 16:22:30 +02:00
|
|
|
*/
|
|
|
|
|
|
2015-04-27 14:25:10 +02:00
|
|
|
package akka.cluster.pubsub;
|
2013-04-06 16:22:30 +02:00
|
|
|
|
2015-05-07 11:01:59 +02:00
|
|
|
import com.typesafe.config.ConfigFactory;
|
|
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
import akka.testkit.AkkaJUnitActorSystemResource;
|
2015-05-07 11:01:59 +02:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
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;
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
import akka.actor.AbstractActor;
|
2013-04-06 16:22:30 +02:00
|
|
|
import akka.event.Logging;
|
|
|
|
|
import akka.event.LoggingAdapter;
|
2016-04-06 01:23:21 +02:00
|
|
|
import org.scalatest.junit.JUnitSuite;
|
2013-04-06 16:22:30 +02:00
|
|
|
|
2016-04-06 01:23:21 +02:00
|
|
|
public class DistributedPubSubMediatorTest extends JUnitSuite {
|
2013-04-06 16:22:30 +02:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
@ClassRule
|
|
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource =
|
2015-09-11 15:48:49 +02:00
|
|
|
new AkkaJUnitActorSystemResource("DistributedPubSubMediatorTest",
|
2015-05-07 11:01:59 +02:00
|
|
|
ConfigFactory.parseString(
|
2016-06-10 15:04:13 +02:00
|
|
|
"akka.actor.provider = \"cluster\"\n" +
|
2016-12-01 18:49:38 +01:00
|
|
|
"akka.remote.netty.tcp.port=0\n" +
|
|
|
|
|
"akka.remote.artery.canonical.port=0"));
|
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
|
2015-09-11 15:48:49 +02:00
|
|
|
public void demonstratePublishUsage() {
|
2013-04-06 16:22:30 +02:00
|
|
|
//#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
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-11 15:48:49 +02:00
|
|
|
public void demonstrateSendUsage() {
|
|
|
|
|
//#start-send-destinations
|
|
|
|
|
system.actorOf(Props.create(Destination.class), "destination");
|
|
|
|
|
//another node
|
|
|
|
|
system.actorOf(Props.create(Destination.class), "destination");
|
|
|
|
|
//#start-send-destinations
|
|
|
|
|
|
|
|
|
|
//#send-message
|
|
|
|
|
//somewhere else
|
|
|
|
|
ActorRef sender = system.actorOf(Props.create(Publisher.class), "sender");
|
|
|
|
|
// after a while the destinations are replicated
|
|
|
|
|
sender.tell("hello", null);
|
|
|
|
|
//#send-message
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-06 16:22:30 +02:00
|
|
|
static//#subscriber
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
public class Subscriber extends AbstractActor {
|
2013-04-06 16:22:30 +02:00
|
|
|
LoggingAdapter log = Logging.getLogger(getContext().system(), this);
|
|
|
|
|
|
|
|
|
|
public Subscriber() {
|
2015-09-11 15:48:49 +02:00
|
|
|
ActorRef mediator =
|
2015-05-07 11:01:59 +02:00
|
|
|
DistributedPubSub.get(getContext().system()).mediator();
|
2013-04-06 16:22:30 +02:00
|
|
|
// subscribe to the topic named "content"
|
2015-09-11 15:48:49 +02:00
|
|
|
mediator.tell(new DistributedPubSubMediator.Subscribe("content", getSelf()),
|
2013-05-29 17:20:18 +02:00
|
|
|
getSelf());
|
2013-04-06 16:22:30 +02:00
|
|
|
}
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.match(String.class, msg ->
|
|
|
|
|
log.info("Got: {}", msg))
|
|
|
|
|
.match(DistributedPubSubMediator.SubscribeAck.class, msg ->
|
2017-07-07 11:29:58 +02:00
|
|
|
log.info("subscribed"))
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
.build();
|
2013-04-06 16:22:30 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#subscriber
|
|
|
|
|
|
|
|
|
|
static//#publisher
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
public class Publisher extends AbstractActor {
|
2013-04-06 16:22:30 +02:00
|
|
|
|
|
|
|
|
// activate the extension
|
2015-09-11 15:48:49 +02:00
|
|
|
ActorRef mediator =
|
2015-05-07 11:01:59 +02:00
|
|
|
DistributedPubSub.get(getContext().system()).mediator();
|
2013-04-06 16:22:30 +02:00
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.match(String.class, in -> {
|
|
|
|
|
String out = in.toUpperCase();
|
|
|
|
|
mediator.tell(new DistributedPubSubMediator.Publish("content", out),
|
|
|
|
|
getSelf());
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2013-04-06 16:22:30 +02:00
|
|
|
}
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
|
2013-04-06 16:22:30 +02:00
|
|
|
}
|
|
|
|
|
//#publisher
|
2015-09-11 15:48:49 +02:00
|
|
|
|
|
|
|
|
static//#send-destination
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
public class Destination extends AbstractActor {
|
2015-09-11 15:48:49 +02:00
|
|
|
LoggingAdapter log = Logging.getLogger(getContext().system(), this);
|
|
|
|
|
|
|
|
|
|
public Destination() {
|
|
|
|
|
ActorRef mediator =
|
|
|
|
|
DistributedPubSub.get(getContext().system()).mediator();
|
|
|
|
|
// register to the path
|
|
|
|
|
mediator.tell(new DistributedPubSubMediator.Put(getSelf()), getSelf());
|
|
|
|
|
}
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.match(String.class, msg ->
|
|
|
|
|
log.info("Got: {}", msg))
|
|
|
|
|
.build();
|
2015-09-11 15:48:49 +02:00
|
|
|
}
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
|
2015-09-11 15:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#send-destination
|
|
|
|
|
|
|
|
|
|
static//#sender
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
public class Sender extends AbstractActor {
|
2015-09-11 15:48:49 +02:00
|
|
|
|
|
|
|
|
// activate the extension
|
|
|
|
|
ActorRef mediator =
|
|
|
|
|
DistributedPubSub.get(getContext().system()).mediator();
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.match(String.class, in -> {
|
|
|
|
|
String out = in.toUpperCase();
|
|
|
|
|
boolean localAffinity = true;
|
|
|
|
|
mediator.tell(new DistributedPubSubMediator.Send("/user/destination", out,
|
|
|
|
|
localAffinity), getSelf());
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2015-09-11 15:48:49 +02:00
|
|
|
}
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
|
2015-09-11 15:48:49 +02:00
|
|
|
}
|
|
|
|
|
//#sender
|
2013-04-06 16:22:30 +02:00
|
|
|
}
|