Class SupervisorStrategy
- java.lang.Object
-
- org.apache.pekko.actor.SupervisorStrategy
-
- Direct Known Subclasses:
AllForOneStrategy
,OneForOneStrategy
public abstract class SupervisorStrategy extends java.lang.Object
A Pekko SupervisorStrategy is the policy to apply for crashing children.IMPORTANT:
You should not normally need to create new subclasses, instead use the existing
pekko.actor.OneForOneStrategy
orpekko.actor.AllForOneStrategy
, but if you do, please read the docs of the methods below carefully, as incorrect implementations may lead to “blocked” actor systems (i.e. permanently suspended actors).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
SupervisorStrategy.Directive
static class
SupervisorStrategy.Escalate$
Escalates the failure to the supervisor of the supervisor, by rethrowing the cause of the failure, i.e.static class
SupervisorStrategy.Restart$
Discards the old Actor instance and replaces it with a new, then resumes message processing.static class
SupervisorStrategy.Resume$
Resumes message processing for the failed Actorstatic class
SupervisorStrategy.Stop$
Stops the Actor
-
Constructor Summary
Constructors Constructor Description SupervisorStrategy()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract scala.PartialFunction<java.lang.Throwable,SupervisorStrategy.Directive>
decider()
Returns the Decider that is associated with this SupervisorStrategy.static scala.PartialFunction<java.lang.Throwable,SupervisorStrategy.Directive>
defaultDecider()
When supervisorStrategy is not specified for an actor thisDecider
is used by default in the supervisor strategy.static SupervisorStrategy
defaultStrategy()
When supervisorStrategy is not specified for an actor this is used by default.static SupervisorStrategy.Escalate$
escalate()
Java API: Returning this directive escalates the failure to the supervisor of the supervisor, by rethrowing the cause of the failure, i.e.abstract void
handleChildTerminated(ActorContext context, ActorRef child, scala.collection.Iterable<ActorRef> children)
This method is called after the child has been removed from the set of children.boolean
handleFailure(ActorContext context, ActorRef child, java.lang.Throwable cause, ChildRestartStats stats, scala.collection.Iterable<ChildRestartStats> children)
This is the main entry point: in case of a child’s failure, this method must try to handle the failure by resuming, restarting or stopping the child (and returningtrue
), or it returnsfalse
to escalate the failure, which will lead to this actor re-throwing the exception which caused the failure.void
logFailure(ActorContext context, ActorRef child, java.lang.Throwable cause, SupervisorStrategy.Directive decision)
Default logging of actor failures whenpekko.actor.SupervisorStrategy#loggingEnabled
istrue
.protected boolean
loggingEnabled()
Logging of actor failures is done when this istrue
.static scala.PartialFunction<java.lang.Throwable,SupervisorStrategy.Directive>
makeDecider(java.lang.Iterable<java.lang.Class<? extends java.lang.Throwable>> trapExit)
Decider builder which just checks whether one of the given Throwables matches the cause and restarts, otherwise escalates.static scala.PartialFunction<java.lang.Throwable,SupervisorStrategy.Directive>
makeDecider(Function<java.lang.Throwable,SupervisorStrategy.Directive> func)
Converts a Java Decider into a Scala Deciderstatic scala.PartialFunction<java.lang.Throwable,SupervisorStrategy.Directive>
makeDecider(scala.collection.immutable.Seq<java.lang.Class<? extends java.lang.Throwable>> trapExit)
Decider builder which just checks whether one of the given Throwables matches the cause and restarts, otherwise escalates.static scala.PartialFunction<java.lang.Throwable,SupervisorStrategy.Directive>
makeDecider(scala.collection.Iterable<scala.Tuple2<java.lang.Class<? extends java.lang.Throwable>,SupervisorStrategy.Directive>> flat)
Decider builder for Iterables of cause-directive pairs, e.g.abstract void
processFailure(ActorContext context, boolean restart, ActorRef child, java.lang.Throwable cause, ChildRestartStats stats, scala.collection.Iterable<ChildRestartStats> children)
This method is called to act on the failure of a child: restart if the flag is true, stop otherwise.static SupervisorStrategy.Restart$
restart()
Java API: Returning this directive discards the old Actor instance and replaces it with a new, then resumes message processing.static SupervisorStrategy.Directive
restart(Logging.LogLevel logLevel)
Returning this directive discards the old Actor instance and replaces it with a new, then resumes message processing.void
restartChild(ActorRef child, java.lang.Throwable cause, boolean suspendFirst)
Restart the given child, possibly suspending it first.static SupervisorStrategy.Resume$
resume()
Java API: Returning this directive resumes message processing for the failed Actorstatic SupervisorStrategy.Directive
resume(Logging.LogLevel logLevel)
Returning this directive resumes message processing for the failed Actor.void
resumeChild(ActorRef child, java.lang.Throwable cause)
Resume the previously failed child: do never apply this to a child which is not the currently failing child.static scala.PartialFunction<java.lang.Throwable,SupervisorStrategy.Directive>
seqCauseDirective2Decider(scala.collection.Iterable<scala.Tuple2<java.lang.Class<? extends java.lang.Throwable>,SupervisorStrategy.Directive>> trapExit)
static scala.PartialFunction<java.lang.Throwable,SupervisorStrategy.Directive>
seqThrowable2Decider(scala.collection.immutable.Seq<java.lang.Class<? extends java.lang.Throwable>> trapExit)
Implicit conversion fromSeq
of Throwables to aDecider
.static SupervisorStrategy.Stop$
stop()
Java API: Returning this directive stops the Actorstatic SupervisorStrategy.Directive
stop(Logging.LogLevel logLevel)
Returning this directive stops the Actorstatic SupervisorStrategy
stoppingStrategy()
This strategy resembles Erlang in that failing children are always terminated (one-for-one).
-
-
-
Method Detail
-
resume
public static SupervisorStrategy.Resume$ resume()
Java API: Returning this directive resumes message processing for the failed Actor
-
resume
public static SupervisorStrategy.Directive resume(Logging.LogLevel logLevel)
Returning this directive resumes message processing for the failed Actor.- Parameters:
logLevel
- Log level which will be used to log the failure
-
restart
public static SupervisorStrategy.Restart$ restart()
Java API: Returning this directive discards the old Actor instance and replaces it with a new, then resumes message processing.
-
restart
public static SupervisorStrategy.Directive restart(Logging.LogLevel logLevel)
Returning this directive discards the old Actor instance and replaces it with a new, then resumes message processing.- Parameters:
logLevel
- Log level which will be used to log the failure
-
stop
public static SupervisorStrategy.Stop$ stop()
Java API: Returning this directive stops the Actor
-
stop
public static SupervisorStrategy.Directive stop(Logging.LogLevel logLevel)
Returning this directive stops the Actor- Parameters:
logLevel
- Log level which will be used to log the failure
-
escalate
public static SupervisorStrategy.Escalate$ escalate()
Java API: Returning this directive escalates the failure to the supervisor of the supervisor, by rethrowing the cause of the failure, i.e. the supervisor fails with the same exception as the child.
-
defaultDecider
public static final scala.PartialFunction<java.lang.Throwable,SupervisorStrategy.Directive> defaultDecider()
When supervisorStrategy is not specified for an actor thisDecider
is used by default in the supervisor strategy. The child will be stopped whenpekko.actor.ActorInitializationException
,pekko.actor.ActorKilledException
, orpekko.actor.DeathPactException
is thrown. It will be restarted for otherException
types. The error is escalated if it's aThrowable
, i.e.Error
.
-
defaultStrategy
public static final SupervisorStrategy defaultStrategy()
When supervisorStrategy is not specified for an actor this is used by default. OneForOneStrategy with decider defined indefaultDecider()
.
-
stoppingStrategy
public static final SupervisorStrategy stoppingStrategy()
This strategy resembles Erlang in that failing children are always terminated (one-for-one).
-
seqThrowable2Decider
public static scala.PartialFunction<java.lang.Throwable,SupervisorStrategy.Directive> seqThrowable2Decider(scala.collection.immutable.Seq<java.lang.Class<? extends java.lang.Throwable>> trapExit)
Implicit conversion fromSeq
of Throwables to aDecider
. This maps the given Throwables to restarts, otherwise escalates.
-
makeDecider
public static scala.PartialFunction<java.lang.Throwable,SupervisorStrategy.Directive> makeDecider(scala.collection.immutable.Seq<java.lang.Class<? extends java.lang.Throwable>> trapExit)
Decider builder which just checks whether one of the given Throwables matches the cause and restarts, otherwise escalates.
-
makeDecider
public static scala.PartialFunction<java.lang.Throwable,SupervisorStrategy.Directive> makeDecider(java.lang.Iterable<java.lang.Class<? extends java.lang.Throwable>> trapExit)
Decider builder which just checks whether one of the given Throwables matches the cause and restarts, otherwise escalates.
-
makeDecider
public static scala.PartialFunction<java.lang.Throwable,SupervisorStrategy.Directive> makeDecider(scala.collection.Iterable<scala.Tuple2<java.lang.Class<? extends java.lang.Throwable>,SupervisorStrategy.Directive>> flat)
Decider builder for Iterables of cause-directive pairs, e.g. a map obtained from configuration; will sort the pairs so that the most specific type is checked before all its subtypes, allowing carving out subtrees of the Throwable hierarchy.
-
makeDecider
public static scala.PartialFunction<java.lang.Throwable,SupervisorStrategy.Directive> makeDecider(Function<java.lang.Throwable,SupervisorStrategy.Directive> func)
Converts a Java Decider into a Scala Decider
-
seqCauseDirective2Decider
public static scala.PartialFunction<java.lang.Throwable,SupervisorStrategy.Directive> seqCauseDirective2Decider(scala.collection.Iterable<scala.Tuple2<java.lang.Class<? extends java.lang.Throwable>,SupervisorStrategy.Directive>> trapExit)
-
decider
public abstract scala.PartialFunction<java.lang.Throwable,SupervisorStrategy.Directive> decider()
Returns the Decider that is associated with this SupervisorStrategy. The Decider is invoked by the default implementation ofhandleFailure
to obtain the Directive to be applied.
-
handleChildTerminated
public abstract void handleChildTerminated(ActorContext context, ActorRef child, scala.collection.Iterable<ActorRef> children)
This method is called after the child has been removed from the set of children. It does not need to do anything special. Exceptions thrown from this method do NOT make the actor fail if this happens during termination.
-
processFailure
public abstract void processFailure(ActorContext context, boolean restart, ActorRef child, java.lang.Throwable cause, ChildRestartStats stats, scala.collection.Iterable<ChildRestartStats> children)
This method is called to act on the failure of a child: restart if the flag is true, stop otherwise.
-
handleFailure
public boolean handleFailure(ActorContext context, ActorRef child, java.lang.Throwable cause, ChildRestartStats stats, scala.collection.Iterable<ChildRestartStats> children)
This is the main entry point: in case of a child’s failure, this method must try to handle the failure by resuming, restarting or stopping the child (and returningtrue
), or it returnsfalse
to escalate the failure, which will lead to this actor re-throwing the exception which caused the failure. The exception will not be wrapped.This method calls
pekko.actor.SupervisorStrategy#logFailure
, which will log the failure unless it is escalated. You can customize the logging by settingpekko.actor.SupervisorStrategy#loggingEnabled
tofalse
and do the logging inside thedecider
or override thelogFailure
method.- Parameters:
children
- is a lazy collection (a view)
-
loggingEnabled
protected boolean loggingEnabled()
Logging of actor failures is done when this istrue
.
-
logFailure
public void logFailure(ActorContext context, ActorRef child, java.lang.Throwable cause, SupervisorStrategy.Directive decision)
Default logging of actor failures whenpekko.actor.SupervisorStrategy#loggingEnabled
istrue
.Escalate
failures are not logged here, since they are supposed to be handled at a level higher up in the hierarchy.Resume
failures are logged atWarning
level.Stop
andRestart
failures are logged atError
level.
-
resumeChild
public final void resumeChild(ActorRef child, java.lang.Throwable cause)
Resume the previously failed child: do never apply this to a child which is not the currently failing child. Suspend/resume needs to be done in matching pairs, otherwise actors will wake up too soon or never at all.
-
restartChild
public final void restartChild(ActorRef child, java.lang.Throwable cause, boolean suspendFirst)
Restart the given child, possibly suspending it first.IMPORTANT:
If the child is the currently failing one, it will already have been suspended, hence
suspendFirst
must be false. If the child is not the currently failing one, then it did not request this treatment and is therefore not prepared to be resumed without prior suspend.
-
-