public final class BackoffSupervisor extends java.lang.Object implements Actor
This is useful in situations where the re-start of the child actor should be delayed e.g. in order to give an external resource time to recover before the child actor tries contacting it again (after being restarted).
Specifically this pattern is useful for for persistent actors, which are stopped in case of persistence failures. Just restarting them immediately would probably fail again (since the data store is probably unavailable). It is better to try again after a delay.
It supports exponential back-off between the given minBackoff
and
maxBackoff
durations. For example, if minBackoff
is 3 seconds and
maxBackoff
30 seconds the start attempts will be delayed with
3, 6, 12, 24, 30, 30 seconds. The exponential back-off counter is reset
if the actor is not terminated within the minBackoff
duration.
In addition to the calculated exponential back-off an additional
random delay based the given randomFactor
is added, e.g. 0.2 adds up to 20%
delay. The reason for adding a random delay is to avoid that all failing
actors hit the backend resource at the same time.
You can retrieve the current child ActorRef
by sending BackoffSupervisor.GetCurrentChild
message to this actor and it will reply with BackoffSupervisor.CurrentChild
containing the ActorRef
of the current child, if any.
The BackoffSupervisor
forwards all other messages to the child, if it is currently running.
The child can stop itself and send a PoisonPill
to the parent supervisor
if it wants to do an intentional stop.
Modifier and Type | Class and Description |
---|---|
static class |
BackoffSupervisor.CurrentChild |
static class |
BackoffSupervisor.CurrentChild$ |
static class |
BackoffSupervisor.GetCurrentChild$
Send this message to the
BackoffSupervisor and it will reply with
BackoffSupervisor.CurrentChild containing the ActorRef of the current child, if any. |
Actor.emptyBehavior$, Actor.ignoringBehavior$
Constructor and Description |
---|
BackoffSupervisor(Props childProps,
java.lang.String childName,
scala.concurrent.duration.FiniteDuration minBackoff,
scala.concurrent.duration.FiniteDuration maxBackoff,
double randomFactor) |
Modifier and Type | Method and Description |
---|---|
static BackoffSupervisor.GetCurrentChild$ |
getCurrentChild()
Java API: Send this message to the
BackoffSupervisor and it will reply with
BackoffSupervisor.CurrentChild containing the ActorRef of the current child, if any. |
void |
preStart()
User overridable callback.
|
static Props |
props(Props childProps,
java.lang.String childName,
scala.concurrent.duration.FiniteDuration minBackoff,
scala.concurrent.duration.FiniteDuration maxBackoff,
double randomFactor)
Props for creating an
BackoffSupervisor actor. |
scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit> |
receive()
This defines the initial actor behavior, it must return a partial function
with the actor logic.
|
void |
startChild() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
aroundPostRestart, aroundPostStop, aroundPreRestart, aroundPreStart, aroundReceive, context, postRestart, postStop, preRestart, self, sender, supervisorStrategy, unhandled
public BackoffSupervisor(Props childProps, java.lang.String childName, scala.concurrent.duration.FiniteDuration minBackoff, scala.concurrent.duration.FiniteDuration maxBackoff, double randomFactor)
public static Props props(Props childProps, java.lang.String childName, scala.concurrent.duration.FiniteDuration minBackoff, scala.concurrent.duration.FiniteDuration maxBackoff, double randomFactor)
BackoffSupervisor
actor.
childProps
- the Props
of the child actor that
will be started and supervisedchildName
- name of the child actorminBackoff
- minimum (initial) duration until the child actor will
started again, if it is terminatedmaxBackoff
- the exponential back-off is capped to this durationrandomFactor
- after calculation of the exponential back-off an additional
random delay based on this factor is added, e.g. 0.2
adds up to 20%
delay.
In order to skip this additional delay pass in 0
.public static BackoffSupervisor.GetCurrentChild$ getCurrentChild()
BackoffSupervisor
and it will reply with
BackoffSupervisor.CurrentChild
containing the ActorRef
of the current child, if any.public void preStart()
Actor
public void startChild()