Passivate for Typed Sharding, #24478

* also moved singleton doc sample to own file
* and mv /doc/ to /docs/ as in other places
This commit is contained in:
Patrik Nordwall 2018-06-11 11:53:02 +02:00
parent c9ea0309c9
commit 0f56a6d1ed
12 changed files with 432 additions and 117 deletions

View file

@ -0,0 +1,69 @@
/**
* Copyright (C) 2018 Lightbend Inc. <https://www.lightbend.com>
*/
package jdocs.akka.cluster.typed;
import akka.actor.typed.ActorRef;
import akka.actor.typed.ActorSystem;
import akka.actor.typed.Behavior;
import akka.actor.typed.Props;
import akka.actor.typed.javadsl.Behaviors;
//#import
import akka.cluster.typed.ClusterSingleton;
import akka.cluster.typed.ClusterSingletonSettings;
//#import
public class SingletonCompileOnlyTest {
//#counter
interface CounterCommand {}
public static class Increment implements CounterCommand { }
public static class GoodByeCounter implements CounterCommand { }
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) {
return Behaviors.receive(CounterCommand.class)
.onMessage(Increment.class, (ctx, msg) -> {
return counter(entityId,value + 1);
})
.onMessage(GetValue.class, (ctx, msg) -> {
msg.replyTo.tell(value);
return Behaviors.same();
})
.onMessage(GoodByeCounter.class, (ctx, msg) -> {
return Behaviors.stopped();
})
.build();
}
//#counter
public static void example() {
ActorSystem system = ActorSystem.create(
Behaviors.empty(), "SingletonExample"
);
//#singleton
ClusterSingleton singleton = ClusterSingleton.get(system);
// Start if needed and provide a proxy to a named singleton
ActorRef<CounterCommand> proxy = singleton.spawn(
counter("TheCounter", 0),
"GlobalCounter",
Props.empty(),
ClusterSingletonSettings.create(system),
new GoodByeCounter()
);
proxy.tell(new Increment());
//#singleton
}
}

View file

@ -0,0 +1,52 @@
/**
* Copyright (C) 2018 Lightbend Inc. <https://www.lightbend.com>
*/
package docs.akka.cluster.typed
import akka.actor.typed.ActorRef
import akka.actor.typed.ActorSystem
import akka.actor.typed.Behavior
import akka.actor.typed.Props
import akka.actor.typed.scaladsl.Behaviors
object SingletonCompileOnlySpec {
val system = ActorSystem(Behaviors.empty, "Singleton")
//#counter
trait CounterCommand
case object Increment extends CounterCommand
final case class GetValue(replyTo: ActorRef[Int]) extends CounterCommand
case object GoodByeCounter extends CounterCommand
def counter(entityId: String, value: Int): Behavior[CounterCommand] =
Behaviors.receiveMessage[CounterCommand] {
case Increment
counter(entityId, value + 1)
case GetValue(replyTo)
replyTo ! value
Behaviors.same
case GoodByeCounter
Behaviors.stopped
}
//#counter
//#singleton
import akka.cluster.typed.ClusterSingleton
import akka.cluster.typed.ClusterSingletonSettings
val singletonManager = ClusterSingleton(system)
// Start if needed and provide a proxy to a named singleton
val proxy: ActorRef[CounterCommand] = singletonManager.spawn(
behavior = counter("TheCounter", 0),
"GlobalCounter",
Props.empty,
ClusterSingletonSettings(system),
terminationMessage = GoodByeCounter
)
proxy ! Increment
//#singleton
}