2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2020-01-02 07:24:59 -05:00
|
|
|
* Copyright (C) 2015-2020 Lightbend Inc. <https://www.lightbend.com>
|
2015-05-17 12:28:47 +02:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.ddata;
|
2015-05-17 12:28:47 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #data-bot
|
2018-04-11 16:47:36 +02:00
|
|
|
import java.time.Duration;
|
2015-09-25 08:39:02 +02:00
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
2015-05-17 12:28:47 +02:00
|
|
|
|
|
|
|
|
import akka.actor.AbstractActor;
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.Cancellable;
|
|
|
|
|
import akka.cluster.Cluster;
|
|
|
|
|
import akka.cluster.ddata.DistributedData;
|
|
|
|
|
import akka.cluster.ddata.Key;
|
|
|
|
|
import akka.cluster.ddata.ORSet;
|
|
|
|
|
import akka.cluster.ddata.ORSetKey;
|
|
|
|
|
import akka.cluster.ddata.Replicator;
|
|
|
|
|
import akka.cluster.ddata.Replicator.Changed;
|
|
|
|
|
import akka.cluster.ddata.Replicator.Subscribe;
|
|
|
|
|
import akka.cluster.ddata.Replicator.Update;
|
|
|
|
|
import akka.cluster.ddata.Replicator.UpdateResponse;
|
|
|
|
|
import akka.event.Logging;
|
|
|
|
|
import akka.event.LoggingAdapter;
|
|
|
|
|
|
|
|
|
|
public class DataBot extends AbstractActor {
|
2019-01-10 03:13:56 +08:00
|
|
|
|
2015-05-17 12:28:47 +02:00
|
|
|
private static final String TICK = "tick";
|
2019-01-10 03:13:56 +08:00
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
private final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
|
2015-05-17 12:28:47 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
private final ActorRef replicator = DistributedData.get(getContext().getSystem()).replicator();
|
2017-03-16 09:30:00 +01:00
|
|
|
private final Cluster node = Cluster.get(getContext().getSystem());
|
2015-05-17 12:28:47 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
private final Cancellable tickTask =
|
|
|
|
|
getContext()
|
|
|
|
|
.getSystem()
|
|
|
|
|
.scheduler()
|
2019-05-27 11:53:26 +02:00
|
|
|
.scheduleWithFixedDelay(
|
2019-01-12 04:00:53 +08:00
|
|
|
Duration.ofSeconds(5),
|
|
|
|
|
Duration.ofSeconds(5),
|
|
|
|
|
getSelf(),
|
|
|
|
|
TICK,
|
|
|
|
|
getContext().getDispatcher(),
|
|
|
|
|
getSelf());
|
2015-05-17 12:28:47 +02:00
|
|
|
|
|
|
|
|
private final Key<ORSet<String>> dataKey = ORSetKey.create("key");
|
2019-01-10 03:13:56 +08:00
|
|
|
|
2015-07-01 09:46:58 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
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()
|
2019-01-12 04:00:53 +08:00
|
|
|
.match(String.class, a -> a.equals(TICK), a -> receiveTick())
|
|
|
|
|
.match(
|
|
|
|
|
Changed.class,
|
|
|
|
|
c -> c.key().equals(dataKey),
|
|
|
|
|
c -> receiveChanged((Changed<ORSet<String>>) c))
|
|
|
|
|
.match(UpdateResponse.class, r -> receiveUpdateResponse())
|
|
|
|
|
.build();
|
2015-07-01 09:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void receiveTick() {
|
|
|
|
|
String s = String.valueOf((char) ThreadLocalRandom.current().nextInt(97, 123));
|
|
|
|
|
if (ThreadLocalRandom.current().nextBoolean()) {
|
|
|
|
|
// add
|
|
|
|
|
log.info("Adding: {}", s);
|
2019-01-12 04:00:53 +08:00
|
|
|
Update<ORSet<String>> update =
|
|
|
|
|
new Update<>(dataKey, ORSet.create(), Replicator.writeLocal(), curr -> curr.add(node, s));
|
|
|
|
|
replicator.tell(update, getSelf());
|
2015-07-01 09:46:58 +02:00
|
|
|
} else {
|
|
|
|
|
// remove
|
|
|
|
|
log.info("Removing: {}", s);
|
2019-01-12 04:00:53 +08:00
|
|
|
Update<ORSet<String>> update =
|
|
|
|
|
new Update<>(
|
|
|
|
|
dataKey, ORSet.create(), Replicator.writeLocal(), curr -> curr.remove(node, s));
|
2017-03-16 09:30:00 +01:00
|
|
|
replicator.tell(update, getSelf());
|
2015-07-01 09:46:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void receiveChanged(Changed<ORSet<String>> c) {
|
|
|
|
|
ORSet<String> data = c.dataValue();
|
|
|
|
|
log.info("Current elements: {}", data.getElements());
|
|
|
|
|
}
|
2019-01-10 03:13:56 +08:00
|
|
|
|
2017-12-19 08:23:45 +01:00
|
|
|
private void receiveUpdateResponse() {
|
2015-07-01 09:46:58 +02:00
|
|
|
// ignore
|
2015-05-17 12:28:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void preStart() {
|
2017-03-16 09:30:00 +01:00
|
|
|
Subscribe<ORSet<String>> subscribe = new Subscribe<>(dataKey, getSelf());
|
2015-05-17 12:28:47 +02:00
|
|
|
replicator.tell(subscribe, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-10 03:13:56 +08:00
|
|
|
@Override
|
2019-01-12 04:00:53 +08:00
|
|
|
public void postStop() {
|
2015-05-17 12:28:47 +02:00
|
|
|
tickTask.cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
// #data-bot
|