Add ClusterSingleton supervision documentation (#25419)

* Add ClusterSingleton supervision documentation

* Make example code compilable

* Changed snip per comment

* Back to double snip
This commit is contained in:
Justin Pihony 2018-08-08 03:24:48 -04:00 committed by Konrad `ktoso` Malawski
parent 7ca7b08694
commit b1904325ca
4 changed files with 120 additions and 1 deletions

View file

@ -0,0 +1,31 @@
/**
* Copyright (C) 2015-2018 Lightbend Inc. <https://www.lightbend.com>
*/
package jdocs.cluster.singleton;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.SupervisorStrategy;
//#singleton-supervisor-actor-usage-imports
import akka.actor.PoisonPill;
import akka.actor.Props;
import akka.cluster.singleton.ClusterSingletonManager;
import akka.cluster.singleton.ClusterSingletonManagerSettings;
//#singleton-supervisor-actor-usage-imports
abstract class ClusterSingletonSupervision extends AbstractActor {
public ActorRef createSingleton(String name, Props props, SupervisorStrategy supervisorStrategy) {
//#singleton-supervisor-actor-usage
return getContext().system().actorOf(
ClusterSingletonManager.props(
Props.create(SupervisorActor.class, () -> new SupervisorActor(props, supervisorStrategy)),
PoisonPill.getInstance(),
ClusterSingletonManagerSettings.create(getContext().system())),
name = name);
//#singleton-supervisor-actor-usage
}
}

View file

@ -0,0 +1,36 @@
/**
* Copyright (C) 2015-2018 Lightbend Inc. <https://www.lightbend.com>
*/
package jdocs.cluster.singleton;
//#singleton-supervisor-actor
import akka.actor.AbstractActor;
import akka.actor.AbstractActor.Receive;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.SupervisorStrategy;
public class SupervisorActor extends AbstractActor {
final Props childProps;
final SupervisorStrategy supervisorStrategy;
final ActorRef child;
SupervisorActor(Props childProps, SupervisorStrategy supervisorStrategy) {
this.childProps = childProps;
this.supervisorStrategy = supervisorStrategy;
this.child = getContext().actorOf(childProps, "supervised-child");
}
@Override
public SupervisorStrategy supervisorStrategy() {
return supervisorStrategy;
}
@Override
public Receive createReceive() {
return receiveBuilder()
.matchAny(msg -> child.forward(msg, getContext()))
.build();
}
}
//#singleton-supervisor-actor