'git mv' rst resources to md
This is mainly intended to keep the git history as neat as possible. No actual conversion yet, so now both the rst and the paradox docs are broken, which will be fixed in the next commits.
This commit is contained in:
parent
5507147073
commit
a4a0d308ad
553 changed files with 0 additions and 0 deletions
101
akka-docs/src/main/paradox/java/code/jdocs/ddata/DataBot.java
Normal file
101
akka-docs/src/main/paradox/java/code/jdocs/ddata/DataBot.java
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
/**
|
||||
* Copyright (C) 2015-2017 Lightbend Inc. <http://www.lightbend.com>
|
||||
*/
|
||||
package jdocs.ddata;
|
||||
|
||||
//#data-bot
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
|
||||
import scala.concurrent.duration.Duration;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
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 {
|
||||
|
||||
private static final String TICK = "tick";
|
||||
|
||||
private final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
|
||||
|
||||
private final ActorRef replicator =
|
||||
DistributedData.get(getContext().getSystem()).replicator();
|
||||
private final Cluster node = Cluster.get(getContext().getSystem());
|
||||
|
||||
private final Cancellable tickTask = getContext().getSystem().scheduler().schedule(
|
||||
Duration.create(5, SECONDS), Duration.create(5, SECONDS), getSelf(), TICK,
|
||||
getContext().dispatcher(), getSelf());
|
||||
|
||||
private final Key<ORSet<String>> dataKey = ORSetKey.create("key");
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.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())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
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, getSelf());
|
||||
} 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, getSelf());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void receiveChanged(Changed<ORSet<String>> c) {
|
||||
ORSet<String> data = c.dataValue();
|
||||
log.info("Current elements: {}", data.getElements());
|
||||
}
|
||||
|
||||
private void receiveUpdateResoponse() {
|
||||
// ignore
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void preStart() {
|
||||
Subscribe<ORSet<String>> subscribe = new Subscribe<>(dataKey, getSelf());
|
||||
replicator.tell(subscribe, ActorRef.noSender());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postStop(){
|
||||
tickTask.cancel();
|
||||
}
|
||||
|
||||
}
|
||||
//#data-bot
|
||||
Loading…
Add table
Add a link
Reference in a new issue