2015-05-17 12:28:47 +02:00
|
|
|
/**
|
2017-01-04 17:37:10 +01:00
|
|
|
* Copyright (C) 2015-2017 Lightbend Inc. <http://www.lightbend.com>
|
2015-05-17 12:28:47 +02:00
|
|
|
*/
|
|
|
|
|
package docs.ddata;
|
|
|
|
|
|
|
|
|
|
//#data-bot
|
|
|
|
|
import static java.util.concurrent.TimeUnit.SECONDS;
|
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-05-17 12:28:47 +02:00
|
|
|
import scala.concurrent.duration.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;
|
|
|
|
|
import akka.japi.pf.ReceiveBuilder;
|
|
|
|
|
|
|
|
|
|
public class DataBot extends AbstractActor {
|
|
|
|
|
|
|
|
|
|
private static final String TICK = "tick";
|
|
|
|
|
|
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
|
|
|
private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);
|
2015-05-17 12:28:47 +02:00
|
|
|
|
|
|
|
|
private final ActorRef replicator =
|
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
|
|
|
DistributedData.get(getContext().system()).replicator();
|
|
|
|
|
private final Cluster node = Cluster.get(getContext().system());
|
2015-05-17 12:28:47 +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
|
|
|
private final Cancellable tickTask = getContext().system().scheduler().schedule(
|
2015-05-17 12:28:47 +02:00
|
|
|
Duration.create(5, SECONDS), Duration.create(5, SECONDS), self(), TICK,
|
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
|
|
|
getContext().dispatcher(), self());
|
2015-05-17 12:28:47 +02:00
|
|
|
|
|
|
|
|
private final Key<ORSet<String>> dataKey = ORSetKey.create("key");
|
|
|
|
|
|
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()
|
2015-07-01 09:46:58 +02: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 -> receiveUpdateResoponse())
|
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();
|
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);
|
|
|
|
|
Update<ORSet<String>> update = new Update<>(
|
|
|
|
|
dataKey,
|
|
|
|
|
ORSet.create(),
|
|
|
|
|
Replicator.writeLocal(),
|
|
|
|
|
curr -> curr.add(node, s));
|
|
|
|
|
replicator.tell(update, self());
|
|
|
|
|
} else {
|
|
|
|
|
// remove
|
|
|
|
|
log.info("Removing: {}", s);
|
|
|
|
|
Update<ORSet<String>> update = new Update<>(
|
|
|
|
|
dataKey,
|
|
|
|
|
ORSet.create(),
|
|
|
|
|
Replicator.writeLocal(),
|
|
|
|
|
curr -> curr.remove(node, s));
|
|
|
|
|
replicator.tell(update, self());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void receiveChanged(Changed<ORSet<String>> c) {
|
|
|
|
|
ORSet<String> data = c.dataValue();
|
|
|
|
|
log.info("Current elements: {}", data.getElements());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void receiveUpdateResoponse() {
|
|
|
|
|
// ignore
|
2015-05-17 12:28:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void preStart() {
|
|
|
|
|
Subscribe<ORSet<String>> subscribe = new Subscribe<>(dataKey, self());
|
|
|
|
|
replicator.tell(subscribe, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void postStop(){
|
|
|
|
|
tickTask.cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//#data-bot
|