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 theinitializeror theinitmethod. 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 classSupervisorActor.RestartStrategySpecifies 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 SupervisorActorcurrentSupervisor()protected <T extends ActorRef<M>,M>
TgetChild(java.lang.Object id)Retrieves a child actor by itsidprotected java.util.List<? extends ActorRef<?>>getChildren()Retrieves the children actor references as an immutable list.protected java.lang.ObjecthandleLifecycleMessage(LifecycleMessage m)This method is called by this class during a call to any of thereceivemethods if aLifecycleMessageis found in the mailbox.protected voidhandleMessage(java.lang.Object m1)Called by the defaultBehaviorActor.behavior()method to handle each incoming message.protected voidinit()Called byonStartto initialize the actor.org.slf4j.Loggerlog()TheLoggerobject associated with this actor.protected SupervisormakeRef(ActorRef<java.lang.Object> ref)protected voidonStart()This method is called by theBehaviorActorat the beginning ofBehaviorActor.doRun().protected voidonTerminate(java.lang.Throwable cause)This method is called by theBehaviorActorat the end ofBehaviorActor.doRun().Supervisorref()Returns the ActorRef to this actor, if it has been started.protected booleanremoveChild(ActorRef actor, boolean terminate)Removes a child actor from the supervisor.protected booleanremoveChild(java.lang.Object id, boolean terminate)Removes a child actor from the supervisor.protected Supervisorself()Returns the ActorRef to this actor, if it has been started.Supervisorspawn()Starts a new fiber and runs the actor in it.Supervisorspawn(FiberFactory ff)Starts a new fiber using the given scheduler and runs the actor in it.Supervisorspawn(StrandFactory sf)Starts a new fiber using the given scheduler and runs the actor in it.SupervisorspawnThread()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 strategyinitializer- 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 strategychildSpec- 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 strategychildSpec- 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 strategyinitializer- 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 strategyinitializer- 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 strategychildSpec- 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 strategychildSpec- 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 strategychildSpec- 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 strategychildSpec- 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 strategychildSpec- 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 strategychildSpec- the supervisor's children
-
-
Method Detail
-
makeRef
protected Supervisor makeRef(ActorRef<java.lang.Object> ref)
- Overrides:
makeRefin classBehaviorActor
-
ref
public Supervisor ref()
Description copied from class:ActorReturns the ActorRef to this actor, if it has been started.- Overrides:
refin classBehaviorActor- Returns:
- the ActorRef of this actor if it has been started, or
nullotherwise.
-
self
protected Supervisor self()
Description copied from class:ActorReturns the ActorRef to this actor, if it has been started.- Overrides:
selfin classBehaviorActor- Returns:
- the ActorRef of this actor if it has been started, or
nullotherwise.
-
spawn
public Supervisor spawn(StrandFactory sf)
Description copied from class:ActorStarts 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:
spawnin 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:ActorStarts 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:
spawnin 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:ActorStarts a new fiber and runs the actor in it. The fiber's name will be set to this actor's name.- Overrides:
spawnin classBehaviorActor- Returns:
- This actors' ActorRef
-
spawnThread
public Supervisor spawnThread()
Description copied from class:ActorStarts a new thread and runs the actor in it. The fiber's name will be set to this actor's name.- Overrides:
spawnThreadin classBehaviorActor- Returns:
- This actors' ActorRef
-
currentSupervisor
public static SupervisorActor currentSupervisor()
-
log
public org.slf4j.Logger log()
Description copied from class:BehaviorActorTheLoggerobject associated with this actor.- Specified by:
login classBehaviorActor
-
init
protected void init() throws java.lang.InterruptedException, SuspendExecutionDescription copied from class:BehaviorActorCalled byonStartto initialize the actor. By default, this method callsinitializer.init()if the initializer in non-null; otherwise it does nothing.- Overrides:
initin classBehaviorActor- Throws:
java.lang.InterruptedExceptionSuspendExecution
-
onStart
protected void onStart() throws java.lang.InterruptedException, SuspendExecutionDescription copied from class:BehaviorActorThis method is called by theBehaviorActorat the beginning ofBehaviorActor.doRun(). By default, this method callsBehaviorActor.init().- Overrides:
onStartin classBehaviorActor- Throws:
java.lang.InterruptedExceptionSuspendExecution
-
handleMessage
protected final void handleMessage(java.lang.Object m1) throws java.lang.InterruptedException, SuspendExecutionDescription copied from class:BehaviorActorCalled by the defaultBehaviorActor.behavior()method to handle each incoming message. By default, this method does nothing.- Overrides:
handleMessagein classBehaviorActor- Parameters:
m1- the message received by the actor.- Throws:
java.lang.InterruptedExceptionSuspendExecution
-
onTerminate
protected void onTerminate(java.lang.Throwable cause) throws SuspendExecution, java.lang.InterruptedExceptionDescription copied from class:BehaviorActorThis method is called by theBehaviorActorat the end ofBehaviorActor.doRun(). By default, this method callsterminate().- Overrides:
onTerminatein classBehaviorActor- Throws:
SuspendExecutionjava.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.InterruptedExceptionSuspendExecution
-
getChild
protected <T extends ActorRef<M>,M> T getChild(java.lang.Object id)
Retrieves a child actor by itsid- Parameters:
id- the child'sidin the supervisor.- Returns:
- the child, if found;
nullif 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
ActorRefs.
-
removeChild
protected final boolean removeChild(java.lang.Object id, boolean terminate) throws SuspendExecution, java.lang.InterruptedExceptionRemoves a child actor from the supervisor.- Parameters:
id- the child'sidin the supervisor.terminate- whether or not the supervisor should terminate the actor- Returns:
trueif the actor has been successfully removed from the supervisor;falseif the child was not found.- Throws:
java.lang.InterruptedExceptionSuspendExecution
-
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:
trueif the actor has been successfully removed from the supervisor;falseif the child was not found.- Throws:
java.lang.InterruptedExceptionSuspendExecution
-
handleLifecycleMessage
protected final java.lang.Object handleLifecycleMessage(LifecycleMessage m)
Description copied from class:BehaviorActorThis method is called by this class during a call to any of thereceivemethods if aLifecycleMessageis found in the mailbox. By default, if the message is anExitMessageand itswatchisnull, i.e. it's a result of alinkrather 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
ShutdownMessageand, upon receiving it, callsshutdown().- Overrides:
handleLifecycleMessagein classBehaviorActor- Parameters:
m- the message- Returns:
nullif the message has been processed and should not be returned byreceive
-
-