Class SupervisorActor
- java.lang.Object
-
- co.paralleluniverse.actors.ActorImpl<Message>
-
- co.paralleluniverse.actors.Actor<java.lang.Object,java.lang.Void>
-
- co.paralleluniverse.actors.behaviors.BehaviorActor
-
- co.paralleluniverse.actors.behaviors.SupervisorActor
-
- All Implemented Interfaces:
ActorBuilder<java.lang.Object,java.lang.Void>
,Joinable<java.lang.Void>
,Port<java.lang.Object>
,PortAutoCloseable
,ReceivePort<java.lang.Object>
,Stranded
,SuspendableCallable<java.lang.Void>
,java.io.Serializable
,java.lang.AutoCloseable
public class SupervisorActor extends BehaviorActor
An actor that supervises, and if necessary, restarts other actors.If an actor needs to know the identity of its siblings, it should add them to the supervisor manually. For that, it needs to know the identity of its supervisor. To do that, pass
This works because the children are constructed from specs (provided they have not been constructed by the caller) during the supervisor's run, so callingself()
to that actor's constructor in theinitializer
or theinit
method. Alternatively, simply callself()
, which will return the supervisor actor, in the actor's constructor.self()
anywhere in the construction process would return the supervisor.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SupervisorActor.RestartStrategy
Specifies a supervisor's strategy in the event a child dies.-
Nested classes/interfaces inherited from class co.paralleluniverse.actors.ActorImpl
ActorImpl.ActorLifecycleListener
-
Nested classes/interfaces inherited from interface co.paralleluniverse.strands.channels.ReceivePort
ReceivePort.EOFException
-
-
Field Summary
-
Fields inherited from class co.paralleluniverse.actors.ActorImpl
flightRecorder, ref
-
-
Constructor Summary
Constructors Constructor Description SupervisorActor(SupervisorActor.RestartStrategy restartStrategy)
Constructs a new supervisor with no children.SupervisorActor(SupervisorActor.RestartStrategy restartStrategy, Supervisor.ChildSpec... childSpec)
Constructs a new supervisor with a given list of children.SupervisorActor(SupervisorActor.RestartStrategy restartStrategy, java.util.List<Supervisor.ChildSpec> childSpec)
Constructs a new supervisor with a given list of children.SupervisorActor(Strand strand, java.lang.String name, MailboxConfig mailboxConfig, SupervisorActor.RestartStrategy restartStrategy)
Constructs a new supervisor with no children.SupervisorActor(Strand strand, java.lang.String name, MailboxConfig mailboxConfig, SupervisorActor.RestartStrategy restartStrategy, Initializer initializer)
Constructs a new supervisor with no children.SupervisorActor(Strand strand, java.lang.String name, MailboxConfig mailboxConfig, SupervisorActor.RestartStrategy restartStrategy, Supervisor.ChildSpec... childSpec)
Constructs a new supervisor with a given list of children.SupervisorActor(Strand strand, java.lang.String name, MailboxConfig mailboxConfig, SupervisorActor.RestartStrategy restartStrategy, java.util.List<Supervisor.ChildSpec> childSpec)
Constructs a new supervisor with a given list of children.SupervisorActor(java.lang.String name, SupervisorActor.RestartStrategy restartStrategy)
Constructs a new supervisor with no children.SupervisorActor(java.lang.String name, SupervisorActor.RestartStrategy restartStrategy, Initializer initializer)
Constructs a new supervisor with no children.SupervisorActor(java.lang.String name, SupervisorActor.RestartStrategy restartStrategy, Supervisor.ChildSpec... childSpec)
Constructs a new supervisor with a given list of children.SupervisorActor(java.lang.String name, SupervisorActor.RestartStrategy restartStrategy, java.util.List<Supervisor.ChildSpec> childSpec)
Constructs a new supervisor with a given list of children.SupervisorActor(java.lang.String name, MailboxConfig mailboxConfig, SupervisorActor.RestartStrategy restartStrategy)
Constructs a new supervisor with no children.SupervisorActor(java.lang.String name, MailboxConfig mailboxConfig, SupervisorActor.RestartStrategy restartStrategy, Initializer initializer)
Constructs a new supervisor with no children.SupervisorActor(java.lang.String name, MailboxConfig mailboxConfig, SupervisorActor.RestartStrategy restartStrategy, Supervisor.ChildSpec... childSpec)
Constructs a new supervisor with a given list of children.SupervisorActor(java.lang.String name, MailboxConfig mailboxConfig, SupervisorActor.RestartStrategy restartStrategy, java.util.List<Supervisor.ChildSpec> childSpec)
Constructs a new supervisor with a given list of children.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <T extends ActorRef<M>,M>
TaddChild(Supervisor.ChildSpec spec)
Adds a new child actor to the supervisor.static SupervisorActor
currentSupervisor()
protected <T extends ActorRef<M>,M>
TgetChild(java.lang.Object id)
Retrieves a child actor by itsid
protected java.util.List<? extends ActorRef<?>>
getChildren()
Retrieves the children actor references as an immutable list.protected java.lang.Object
handleLifecycleMessage(LifecycleMessage m)
This method is called by this class during a call to any of thereceive
methods if aLifecycleMessage
is found in the mailbox.protected void
handleMessage(java.lang.Object m1)
Called by the defaultBehaviorActor.behavior()
method to handle each incoming message.protected void
init()
Called byonStart
to initialize the actor.org.slf4j.Logger
log()
TheLogger
object associated with this actor.protected Supervisor
makeRef(ActorRef<java.lang.Object> ref)
protected void
onStart()
This method is called by theBehaviorActor
at the beginning ofBehaviorActor.doRun()
.protected void
onTerminate(java.lang.Throwable cause)
This method is called by theBehaviorActor
at the end ofBehaviorActor.doRun()
.Supervisor
ref()
Returns the ActorRef to this actor, if it has been started.protected boolean
removeChild(ActorRef actor, boolean terminate)
Removes a child actor from the supervisor.protected boolean
removeChild(java.lang.Object id, boolean terminate)
Removes a child actor from the supervisor.protected Supervisor
self()
Returns the ActorRef to this actor, if it has been started.Supervisor
spawn()
Starts a new fiber and runs the actor in it.Supervisor
spawn(FiberFactory ff)
Starts a new fiber using the given scheduler and runs the actor in it.Supervisor
spawn(StrandFactory sf)
Starts a new fiber using the given scheduler and runs the actor in it.Supervisor
spawnThread()
Starts a new thread and runs the actor in it.-
Methods inherited from class co.paralleluniverse.actors.behaviors.BehaviorActor
behavior, checkCodeSwap, doRun, getInitializer, isRunning, shutdown, terminate
-
Methods inherited from class co.paralleluniverse.actors.Actor
addLifecycleListener, build, checkThrownIn, close, currentActor, filterMessage, get, get, getActor, getDeathCause, getMonitor, getName, getQueueLength, getStrand, hire, hire, internalSend, internalSendNonSuspendable, interrupt, isClosed, isDone, isInActor, isRegistered, isStarted, join, join, link, linked, mailbox, migrate, migrateAndRestart, monitor, monitorAddDeath, monitorAddMessage, monitorResetSkippedMessages, monitorSkippedMessage, newActor, newActor, onCodeChange, readResolve, receive, receive, receive, register, register, reinstantiate, removeLifecycleListener, removeObserverListeners, run, sendSync, setForwardWatch, setMonitor, setName, setStrand, start, stopMonitor, throwIn, toString, tryReceive, trySend, unlink, unlinked, unregister, unwatch, verifyInActor, verifyOnActorStrand, watch, writeReplace
-
Methods inherited from class co.paralleluniverse.actors.ActorImpl
close, getLifecycleListener, getMailbox, isRecordingLevel, record, record, record, record, record, record, record, sendOrInterrupt
-
-
-
-
Constructor Detail
-
SupervisorActor
public SupervisorActor(Strand strand, java.lang.String name, MailboxConfig mailboxConfig, SupervisorActor.RestartStrategy restartStrategy, Initializer initializer)
Constructs a new supervisor with no children. Children may be added later viaSupervisor.addChild
.- Parameters:
strand
- this actor's strand.name
- the actor name (may benull
).mailboxConfig
- this actor's mailbox settings.restartStrategy
- the supervisor'srestart strategy
initializer
- an optional delegate object that will be run upon actor initialization and termination. May benull
.
-
SupervisorActor
public SupervisorActor(Strand strand, java.lang.String name, MailboxConfig mailboxConfig, SupervisorActor.RestartStrategy restartStrategy, java.util.List<Supervisor.ChildSpec> childSpec)
Constructs a new supervisor with a given list of children.- Parameters:
strand
- this actor's strand.name
- the actor name (may benull
).mailboxConfig
- this actor's mailbox settings.restartStrategy
- the supervisor'srestart strategy
childSpec
- the supervisor's children
-
SupervisorActor
public SupervisorActor(Strand strand, java.lang.String name, MailboxConfig mailboxConfig, SupervisorActor.RestartStrategy restartStrategy, Supervisor.ChildSpec... childSpec)
Constructs a new supervisor with a given list of children.- Parameters:
strand
- this actor's strand.name
- the actor name (may benull
).mailboxConfig
- this actor's mailbox settings.restartStrategy
- the supervisor'srestart strategy
childSpec
- the supervisor's children
-
SupervisorActor
public SupervisorActor(Strand strand, java.lang.String name, MailboxConfig mailboxConfig, SupervisorActor.RestartStrategy restartStrategy)
Constructs a new supervisor with no children. Children may be added later viaSupervisor.addChild
.- Parameters:
strand
- this actor's strand.name
- the actor name (may benull
).mailboxConfig
- this actor's mailbox settings.restartStrategy
- the supervisor'srestart strategy
-
SupervisorActor
public SupervisorActor(java.lang.String name, MailboxConfig mailboxConfig, SupervisorActor.RestartStrategy restartStrategy)
Constructs a new supervisor with no children. Children may be added later viaSupervisor.addChild
.- Parameters:
name
- the actor name (may benull
).mailboxConfig
- this actor's mailbox settings.restartStrategy
- the supervisor'srestart strategy
-
SupervisorActor
public SupervisorActor(java.lang.String name, SupervisorActor.RestartStrategy restartStrategy)
Constructs a new supervisor with no children. Children may be added later viaSupervisor.addChild
.- Parameters:
name
- the actor name (may benull
).restartStrategy
- the supervisor'srestart strategy
-
SupervisorActor
public SupervisorActor(java.lang.String name, MailboxConfig mailboxConfig, SupervisorActor.RestartStrategy restartStrategy, Initializer initializer)
Constructs a new supervisor with no children. Children may be added later viaSupervisor.addChild
.- Parameters:
name
- the actor name (may benull
).mailboxConfig
- this actor's mailbox settings.restartStrategy
- the supervisor'srestart strategy
initializer
- an optional delegate object that will be run upon actor initialization and termination. May benull
.
-
SupervisorActor
public SupervisorActor(java.lang.String name, SupervisorActor.RestartStrategy restartStrategy, Initializer initializer)
Constructs a new supervisor with no children. Children may be added later viaSupervisor.addChild
.- Parameters:
name
- the actor name (may benull
).restartStrategy
- the supervisor'srestart strategy
initializer
- an optional delegate object that will be run upon actor initialization and termination. May benull
.
-
SupervisorActor
public SupervisorActor(SupervisorActor.RestartStrategy restartStrategy)
Constructs a new supervisor with no children. Children may be added later viaSupervisor.addChild
.- Parameters:
restartStrategy
- the supervisor'srestart strategy
-
SupervisorActor
public SupervisorActor(java.lang.String name, MailboxConfig mailboxConfig, SupervisorActor.RestartStrategy restartStrategy, java.util.List<Supervisor.ChildSpec> childSpec)
Constructs a new supervisor with a given list of children.- Parameters:
name
- the actor name (may benull
).mailboxConfig
- this actor's mailbox settings.restartStrategy
- the supervisor'srestart strategy
childSpec
- the supervisor's children
-
SupervisorActor
public SupervisorActor(java.lang.String name, MailboxConfig mailboxConfig, SupervisorActor.RestartStrategy restartStrategy, Supervisor.ChildSpec... childSpec)
Constructs a new supervisor with a given list of children.- Parameters:
name
- the actor name (may benull
).mailboxConfig
- this actor's mailbox settings.restartStrategy
- the supervisor'srestart strategy
childSpec
- the supervisor's children
-
SupervisorActor
public SupervisorActor(java.lang.String name, SupervisorActor.RestartStrategy restartStrategy, java.util.List<Supervisor.ChildSpec> childSpec)
Constructs a new supervisor with a given list of children.- Parameters:
name
- the actor name (may benull
).restartStrategy
- the supervisor'srestart strategy
childSpec
- the supervisor's children
-
SupervisorActor
public SupervisorActor(java.lang.String name, SupervisorActor.RestartStrategy restartStrategy, Supervisor.ChildSpec... childSpec)
Constructs a new supervisor with a given list of children.- Parameters:
name
- the actor name (may benull
).restartStrategy
- the supervisor'srestart strategy
childSpec
- the supervisor's children
-
SupervisorActor
public SupervisorActor(SupervisorActor.RestartStrategy restartStrategy, java.util.List<Supervisor.ChildSpec> childSpec)
Constructs a new supervisor with a given list of children.- Parameters:
restartStrategy
- the supervisor'srestart strategy
childSpec
- the supervisor's children
-
SupervisorActor
public SupervisorActor(SupervisorActor.RestartStrategy restartStrategy, Supervisor.ChildSpec... childSpec)
Constructs a new supervisor with a given list of children.- Parameters:
restartStrategy
- the supervisor'srestart strategy
childSpec
- the supervisor's children
-
-
Method Detail
-
makeRef
protected Supervisor makeRef(ActorRef<java.lang.Object> ref)
- Overrides:
makeRef
in classBehaviorActor
-
ref
public Supervisor ref()
Description copied from class:Actor
Returns the ActorRef to this actor, if it has been started.- Overrides:
ref
in classBehaviorActor
- Returns:
- the ActorRef of this actor if it has been started, or
null
otherwise.
-
self
protected Supervisor self()
Description copied from class:Actor
Returns the ActorRef to this actor, if it has been started.- Overrides:
self
in classBehaviorActor
- Returns:
- the ActorRef of this actor if it has been started, or
null
otherwise.
-
spawn
public Supervisor spawn(StrandFactory sf)
Description copied from class:Actor
Starts a new fiber using the given scheduler and runs the actor in it. The fiber's name will be set to this actor's name.- Overrides:
spawn
in classBehaviorActor
- Parameters:
sf
- thefactory
(orscheduler
) that will be used to create the actor's fiber.- Returns:
- This actors' ActorRef
-
spawn
public Supervisor spawn(FiberFactory ff)
Description copied from class:Actor
Starts a new fiber using the given scheduler and runs the actor in it. The fiber's name will be set to this actor's name.- Overrides:
spawn
in classBehaviorActor
- Parameters:
ff
- thefactory
(orscheduler
) that will be used to create the actor's fiber.- Returns:
- This actors' ActorRef
-
spawn
public Supervisor spawn()
Description copied from class:Actor
Starts a new fiber and runs the actor in it. The fiber's name will be set to this actor's name.- Overrides:
spawn
in classBehaviorActor
- Returns:
- This actors' ActorRef
-
spawnThread
public Supervisor spawnThread()
Description copied from class:Actor
Starts a new thread and runs the actor in it. The fiber's name will be set to this actor's name.- Overrides:
spawnThread
in classBehaviorActor
- Returns:
- This actors' ActorRef
-
currentSupervisor
public static SupervisorActor currentSupervisor()
-
log
public org.slf4j.Logger log()
Description copied from class:BehaviorActor
TheLogger
object associated with this actor.- Specified by:
log
in classBehaviorActor
-
init
protected void init() throws java.lang.InterruptedException, SuspendExecution
Description copied from class:BehaviorActor
Called byonStart
to initialize the actor. By default, this method callsinitializer
.init()
if the initializer in non-null; otherwise it does nothing.- Overrides:
init
in classBehaviorActor
- Throws:
java.lang.InterruptedException
SuspendExecution
-
onStart
protected void onStart() throws java.lang.InterruptedException, SuspendExecution
Description copied from class:BehaviorActor
This method is called by theBehaviorActor
at the beginning ofBehaviorActor.doRun()
. By default, this method callsBehaviorActor.init()
.- Overrides:
onStart
in classBehaviorActor
- Throws:
java.lang.InterruptedException
SuspendExecution
-
handleMessage
protected final void handleMessage(java.lang.Object m1) throws java.lang.InterruptedException, SuspendExecution
Description copied from class:BehaviorActor
Called by the defaultBehaviorActor.behavior()
method to handle each incoming message. By default, this method does nothing.- Overrides:
handleMessage
in classBehaviorActor
- Parameters:
m1
- the message received by the actor.- Throws:
java.lang.InterruptedException
SuspendExecution
-
onTerminate
protected void onTerminate(java.lang.Throwable cause) throws SuspendExecution, java.lang.InterruptedException
Description copied from class:BehaviorActor
This method is called by theBehaviorActor
at the end ofBehaviorActor.doRun()
. By default, this method callsterminate()
.- Overrides:
onTerminate
in classBehaviorActor
- Throws:
SuspendExecution
java.lang.InterruptedException
-
addChild
protected final <T extends ActorRef<M>,M> T addChild(Supervisor.ChildSpec spec) throws SuspendExecution, java.lang.InterruptedException
Adds a new child actor to the supervisor. If the child has not been started, it will be started by the supervisor.- Parameters:
spec
- thechild's spec
.- Returns:
- the actor (possibly after it has been started by the supervisor).
- Throws:
java.lang.InterruptedException
SuspendExecution
-
getChild
protected <T extends ActorRef<M>,M> T getChild(java.lang.Object id)
Retrieves a child actor by itsid
- Parameters:
id
- the child'sid
in the supervisor.- Returns:
- the child, if found;
null
if the child was not found
-
getChildren
protected java.util.List<? extends ActorRef<?>> getChildren()
Retrieves the children actor references as an immutable list.- Returns:
- the children
ActorRef
s.
-
removeChild
protected final boolean removeChild(java.lang.Object id, boolean terminate) throws SuspendExecution, java.lang.InterruptedException
Removes a child actor from the supervisor.- Parameters:
id
- the child'sid
in the supervisor.terminate
- whether or not the supervisor should terminate the actor- Returns:
true
if the actor has been successfully removed from the supervisor;false
if the child was not found.- Throws:
java.lang.InterruptedException
SuspendExecution
-
removeChild
protected final boolean removeChild(ActorRef actor, boolean terminate) throws SuspendExecution, java.lang.InterruptedException
Removes a child actor from the supervisor.- Parameters:
actor
- the actorterminate
- whether or not the supervisor should terminate the actor- Returns:
true
if the actor has been successfully removed from the supervisor;false
if the child was not found.- Throws:
java.lang.InterruptedException
SuspendExecution
-
handleLifecycleMessage
protected final java.lang.Object handleLifecycleMessage(LifecycleMessage m)
Description copied from class:BehaviorActor
This method is called by this class during a call to any of thereceive
methods if aLifecycleMessage
is found in the mailbox. By default, if the message is anExitMessage
and itswatch
isnull
, i.e. it's a result of alink
rather than awatch
, it will throw aLifecycleException
, which will, in turn, cause this exception to be thrown by the call toreceive
. This method is not allowed to block. If you want to block as a result of a lifecycle message, return the message from this method (rather than returningnull
), and have it processed by the caller toreceive
.This implementation respects
ShutdownMessage
and, upon receiving it, callsshutdown()
.- Overrides:
handleLifecycleMessage
in classBehaviorActor
- Parameters:
m
- the message- Returns:
null
if the message has been processed and should not be returned byreceive
-
-