Merge pull request #27615 from akka/wip-24717-doc-apply-style8-patriknw
doc: stylish cluster-singleton.md, #24717
This commit is contained in:
commit
d4ea22403b
2 changed files with 101 additions and 59 deletions
|
|
@ -5,82 +5,117 @@
|
||||||
package jdocs.akka.cluster.typed;
|
package jdocs.akka.cluster.typed;
|
||||||
|
|
||||||
import akka.actor.typed.*;
|
import akka.actor.typed.*;
|
||||||
|
import akka.actor.typed.javadsl.AbstractBehavior;
|
||||||
|
import akka.actor.typed.javadsl.ActorContext;
|
||||||
import akka.actor.typed.javadsl.Behaviors;
|
import akka.actor.typed.javadsl.Behaviors;
|
||||||
|
import akka.actor.typed.javadsl.Receive;
|
||||||
|
|
||||||
// #import
|
|
||||||
import akka.cluster.typed.*;
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
|
||||||
|
// #import
|
||||||
|
import akka.cluster.typed.ClusterSingleton;
|
||||||
|
import akka.cluster.typed.SingletonActor;
|
||||||
|
|
||||||
// #import
|
// #import
|
||||||
|
|
||||||
public class SingletonCompileOnlyTest {
|
public interface SingletonCompileOnlyTest {
|
||||||
|
|
||||||
// #counter
|
// #counter
|
||||||
interface CounterCommand {}
|
public class Counter extends AbstractBehavior<Counter.Command> {
|
||||||
|
|
||||||
public static class Increment implements CounterCommand {}
|
public interface Command {}
|
||||||
|
|
||||||
public static class GoodByeCounter implements CounterCommand {}
|
public enum Increment implements Command {
|
||||||
|
INSTANCE
|
||||||
public static class GetValue implements CounterCommand {
|
|
||||||
private final ActorRef<Integer> replyTo;
|
|
||||||
|
|
||||||
public GetValue(ActorRef<Integer> replyTo) {
|
|
||||||
this.replyTo = replyTo;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static Behavior<CounterCommand> counter(String entityId, Integer value) {
|
public static class GetValue implements Command {
|
||||||
return Behaviors.receive(CounterCommand.class)
|
private final ActorRef<Integer> replyTo;
|
||||||
.onMessage(Increment.class, msg -> counter(entityId, value + 1))
|
|
||||||
.onMessage(
|
public GetValue(ActorRef<Integer> replyTo) {
|
||||||
GetValue.class,
|
this.replyTo = replyTo;
|
||||||
msg -> {
|
}
|
||||||
msg.replyTo.tell(value);
|
}
|
||||||
return Behaviors.same();
|
|
||||||
})
|
public enum GoodByeCounter implements Command {
|
||||||
.onMessage(GoodByeCounter.class, msg -> Behaviors.stopped())
|
INSTANCE
|
||||||
.build();
|
}
|
||||||
|
|
||||||
|
public static Behavior<Command> create() {
|
||||||
|
return Behaviors.setup(Counter::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
private final ActorContext<Command> context;
|
||||||
|
private int value = 0;
|
||||||
|
|
||||||
|
private Counter(ActorContext<Command> context) {
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Receive<Command> createReceive() {
|
||||||
|
return newReceiveBuilder()
|
||||||
|
.onMessage(Increment.class, msg -> onIncrement())
|
||||||
|
.onMessage(GetValue.class, this::onGetValue)
|
||||||
|
.onMessage(GoodByeCounter.class, msg -> onGoodByCounter())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Behavior<Command> onIncrement() {
|
||||||
|
value++;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Behavior<Command> onGetValue(GetValue msg) {
|
||||||
|
msg.replyTo.tell(value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Behavior<Command> onGoodByCounter() {
|
||||||
|
// Possible async action then stop
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// #counter
|
// #counter
|
||||||
|
|
||||||
ActorSystem system = ActorSystem.create(Behaviors.empty(), "SingletonExample");
|
ActorSystem system = ActorSystem.create(Behaviors.empty(), "SingletonExample");
|
||||||
|
|
||||||
public void example() {
|
public static void example() {
|
||||||
|
|
||||||
// #singleton
|
// #singleton
|
||||||
ClusterSingleton singleton = ClusterSingleton.get(system);
|
ClusterSingleton singleton = ClusterSingleton.get(system);
|
||||||
// Start if needed and provide a proxy to a named singleton
|
// Start if needed and provide a proxy to a named singleton
|
||||||
ActorRef<CounterCommand> proxy =
|
ActorRef<Counter.Command> proxy =
|
||||||
singleton.init(SingletonActor.of(counter("TheCounter", 0), "GlobalCounter"));
|
singleton.init(SingletonActor.of(Counter.create(), "GlobalCounter"));
|
||||||
|
|
||||||
proxy.tell(new Increment());
|
proxy.tell(Counter.Increment.INSTANCE);
|
||||||
// #singleton
|
// #singleton
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void customStopMessage() {
|
public static void customStopMessage() {
|
||||||
ClusterSingleton singleton = ClusterSingleton.get(system);
|
ClusterSingleton singleton = ClusterSingleton.get(system);
|
||||||
// #stop-message
|
// #stop-message
|
||||||
SingletonActor<CounterCommand> counterSingleton =
|
SingletonActor<Counter.Command> counterSingleton =
|
||||||
SingletonActor.of(counter("TheCounter", 0), "GlobalCounter")
|
SingletonActor.of(Counter.create(), "GlobalCounter")
|
||||||
.withStopMessage(new GoodByeCounter());
|
.withStopMessage(Counter.GoodByeCounter.INSTANCE);
|
||||||
ActorRef<CounterCommand> proxy = singleton.init(counterSingleton);
|
ActorRef<Counter.Command> proxy = singleton.init(counterSingleton);
|
||||||
// #stop-message
|
// #stop-message
|
||||||
proxy.tell(new Increment()); // avoid unused warning
|
proxy.tell(Counter.Increment.INSTANCE); // avoid unused warning
|
||||||
}
|
}
|
||||||
|
|
||||||
public void backoff() {
|
public static void backoff() {
|
||||||
// #backoff
|
// #backoff
|
||||||
ClusterSingleton singleton = ClusterSingleton.get(system);
|
ClusterSingleton singleton = ClusterSingleton.get(system);
|
||||||
ActorRef<CounterCommand> proxy =
|
ActorRef<Counter.Command> proxy =
|
||||||
singleton.init(
|
singleton.init(
|
||||||
SingletonActor.of(
|
SingletonActor.of(
|
||||||
Behaviors.supervise(counter("TheCounter", 0))
|
Behaviors.supervise(Counter.create())
|
||||||
.onFailure(
|
.onFailure(
|
||||||
SupervisorStrategy.restartWithBackoff(
|
SupervisorStrategy.restartWithBackoff(
|
||||||
Duration.ofSeconds(1), Duration.ofSeconds(10), 0.2)),
|
Duration.ofSeconds(1), Duration.ofSeconds(10), 0.2)),
|
||||||
"GlobalCounter"));
|
"GlobalCounter"));
|
||||||
// #backoff
|
// #backoff
|
||||||
proxy.tell(new Increment()); // avoid unused warning
|
proxy.tell(Counter.Increment.INSTANCE); // avoid unused warning
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ package docs.akka.cluster.typed
|
||||||
|
|
||||||
import akka.actor.typed.{ ActorRef, ActorSystem, Behavior, SupervisorStrategy }
|
import akka.actor.typed.{ ActorRef, ActorSystem, Behavior, SupervisorStrategy }
|
||||||
import akka.actor.typed.scaladsl.Behaviors
|
import akka.actor.typed.scaladsl.Behaviors
|
||||||
import akka.cluster.typed.SingletonActor
|
|
||||||
|
|
||||||
import scala.concurrent.duration._
|
import scala.concurrent.duration._
|
||||||
|
|
||||||
|
|
@ -15,45 +14,53 @@ object SingletonCompileOnlySpec {
|
||||||
val system = ActorSystem(Behaviors.empty, "Singleton")
|
val system = ActorSystem(Behaviors.empty, "Singleton")
|
||||||
|
|
||||||
//#counter
|
//#counter
|
||||||
trait CounterCommand
|
object Counter {
|
||||||
case object Increment extends CounterCommand
|
trait Command
|
||||||
final case class GetValue(replyTo: ActorRef[Int]) extends CounterCommand
|
case object Increment extends Command
|
||||||
case object GoodByeCounter extends CounterCommand
|
final case class GetValue(replyTo: ActorRef[Int]) extends Command
|
||||||
|
case object GoodByeCounter extends Command
|
||||||
|
|
||||||
def counter(value: Int): Behavior[CounterCommand] =
|
def apply(): Behavior[Command] = {
|
||||||
Behaviors.receiveMessage[CounterCommand] {
|
def updated(value: Int): Behavior[Command] = {
|
||||||
case Increment =>
|
Behaviors.receiveMessage[Command] {
|
||||||
counter(value + 1)
|
case Increment =>
|
||||||
case GetValue(replyTo) =>
|
updated(value + 1)
|
||||||
replyTo ! value
|
case GetValue(replyTo) =>
|
||||||
Behaviors.same
|
replyTo ! value
|
||||||
case GoodByeCounter =>
|
Behaviors.same
|
||||||
// Do async action then stop
|
case GoodByeCounter =>
|
||||||
Behaviors.stopped
|
// Possible async action then stop
|
||||||
|
Behaviors.stopped
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
updated(0)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
//#counter
|
//#counter
|
||||||
|
|
||||||
//#singleton
|
//#singleton
|
||||||
import akka.cluster.typed.ClusterSingleton
|
import akka.cluster.typed.ClusterSingleton
|
||||||
|
import akka.cluster.typed.SingletonActor
|
||||||
|
|
||||||
val singletonManager = ClusterSingleton(system)
|
val singletonManager = ClusterSingleton(system)
|
||||||
// Start if needed and provide a proxy to a named singleton
|
// Start if needed and provide a proxy to a named singleton
|
||||||
val proxy: ActorRef[CounterCommand] = singletonManager.init(
|
val proxy: ActorRef[Counter.Command] = singletonManager.init(
|
||||||
SingletonActor(Behaviors.supervise(counter(0)).onFailure[Exception](SupervisorStrategy.restart), "GlobalCounter"))
|
SingletonActor(Behaviors.supervise(Counter()).onFailure[Exception](SupervisorStrategy.restart), "GlobalCounter"))
|
||||||
|
|
||||||
proxy ! Increment
|
proxy ! Counter.Increment
|
||||||
//#singleton
|
//#singleton
|
||||||
|
|
||||||
//#stop-message
|
//#stop-message
|
||||||
val singletonActor = SingletonActor(counter(0), "GlobalCounter").withStopMessage(GoodByeCounter)
|
val singletonActor = SingletonActor(Counter(), "GlobalCounter").withStopMessage(Counter.GoodByeCounter)
|
||||||
singletonManager.init(singletonActor)
|
singletonManager.init(singletonActor)
|
||||||
//#stop-message
|
//#stop-message
|
||||||
|
|
||||||
//#backoff
|
//#backoff
|
||||||
val proxyBackOff: ActorRef[CounterCommand] = singletonManager.init(
|
val proxyBackOff: ActorRef[Counter.Command] = singletonManager.init(
|
||||||
SingletonActor(
|
SingletonActor(
|
||||||
Behaviors
|
Behaviors
|
||||||
.supervise(counter(0))
|
.supervise(Counter())
|
||||||
.onFailure[Exception](SupervisorStrategy.restartWithBackoff(1.second, 10.seconds, 0.2)),
|
.onFailure[Exception](SupervisorStrategy.restartWithBackoff(1.second, 10.seconds, 0.2)),
|
||||||
"GlobalCounter"))
|
"GlobalCounter"))
|
||||||
//#backoff
|
//#backoff
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue