# Handling errors with a cluster singleton

**URL:** <https://discuss.akka.io/t/handling-errors-with-a-cluster-singleton/1371>\
**Category:** Akka Libraries\
**Created:** [June 21, 2018, 11:43pm UTC](https://discuss.akka.io/t/handling-errors-with-a-cluster-singleton/1371 "2018-06-21T23:43:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danieru](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@danieru](https://discuss.akka.io/u/danieru)\
**Post date:** [June 21, 2018, 11:43pm UTC](https://discuss.akka.io/t/handling-errors-with-a-cluster-singleton/1371/1 "2018-06-21T23:43:55Z")

</div>

Inside the akka-samples-cluster-java a cluster singleton one actor is instantiated with

```auto
    ActorSystem system = ActorSystem.create("ClusterSystem", config);

      ClusterSingletonManagerSettings settings = ClusterSingletonManagerSettings.create(system)
          .withRole("compute");
      system.actorOf(ClusterSingletonManager.props(
          Props.create(StatsService.class), PoisonPill.getInstance(), settings),
          "statsService");

```

From what I’ve read in the docs the “StatsService” actor will be a child of the User Guardian Actor.

Let’s say that in StatsService when it gets a message an IOException is thrown.

```auto
 public Receive createReceive() {
	... business logic
        ... somehow IOException is thrown (for example a file is read)
}

```

In this scenario, I’d like to shut down the entire cluster singleton when an IOException is thrown during processing.

In the fault tolerance docs I see the definition of a supervisor strategy

```auto
private static SupervisorStrategy strategy =
  new OneForOneStrategy(10, Duration.create(1, TimeUnit.MINUTES),
      DeciderBuilder
          .match(ArithmeticException.class, e -> SupervisorStrategy.resume())
          .match(NullPointerException.class, e -> SupervisorStrategy.restart())
          .match(IllegalArgumentException.class, e -> SupervisorStrategy.stop())
          .matchAny(o -> SupervisorStrategy.escalate())
          .build());

@Override
public SupervisorStrategy supervisorStrategy() {
  return strategy;
}

```

I’m a little confused on how in a cluster singleton how the supervisor strategy comes into play.

1. If using the supervisor strategy, does that mean the parent actor should be a supervisor which spawn children?

2. From reading this  
[https://stackoverflow.com/questions/36701898/how-to-supervise-cluster-singleton-in-akka](https://stackoverflow.com/questions/36701898/how-to-supervise-cluster-singleton-in-akka)

Is the only way to supervise a cluster singleton to pass it in as a parameter when creating the cluster singleton?

```auto
class SupervisorActor(childProps: Props, override val supervisorStrategy) extends Actor {

  val child = context.actorOf(childProps, "supervised-child")

  def receive: Receive = {
    case msg => child forward msg
  }
}

context.actorOf(ClusterSingletonManager.props(
        singletonProps = Props(classOf[SupervisorActor], p, supervisorStrategy),
        terminationMessage = PoisonPill,
        settings = ClusterSingletonManagerSettings(context.system)),
        name = name)

```

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![patriknw](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/patriknw/32/89_2.png) [@patriknw](https://discuss.akka.io/u/patriknw)\
**Post date:** [June 28, 2018, 6:18pm UTC](https://discuss.akka.io/t/handling-errors-with-a-cluster-singleton/1371/2 "2018-06-28T18:18:29Z")

</div>

1. Yes
2. Yes

However it doesn’t make much sense to stop a cluster singleton instance since other nodes will not know and will continue to send to the expected singleton location resulting in deadLetters.
