Class 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 self() to that actor's constructor in the initializer or the init method. Alternatively, simply call self(), which will return the supervisor actor, in the actor's constructor.

    This works because the children are constructed from specs (provided they have not been constructed by the caller) during the supervisor's run, so calling self() anywhere in the construction process would return the supervisor.
    See Also:
    Serialized Form
    • 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 via Supervisor.addChild.
        Parameters:
        strand - this actor's strand.
        name - the actor name (may be null).
        mailboxConfig - this actor's mailbox settings.
        restartStrategy - the supervisor's restart strategy
        initializer - an optional delegate object that will be run upon actor initialization and termination. May be null.
      • 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 be null).
        mailboxConfig - this actor's mailbox settings.
        restartStrategy - the supervisor's restart 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 be null).
        mailboxConfig - this actor's mailbox settings.
        restartStrategy - the supervisor's restart 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 via Supervisor.addChild.
        Parameters:
        strand - this actor's strand.
        name - the actor name (may be null).
        mailboxConfig - this actor's mailbox settings.
        restartStrategy - the supervisor's restart 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 via Supervisor.addChild.
        Parameters:
        name - the actor name (may be null).
        mailboxConfig - this actor's mailbox settings.
        restartStrategy - the supervisor's restart 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 via Supervisor.addChild.
        Parameters:
        name - the actor name (may be null).
        mailboxConfig - this actor's mailbox settings.
        restartStrategy - the supervisor's restart strategy
        initializer - an optional delegate object that will be run upon actor initialization and termination. May be null.
      • SupervisorActor

        public SupervisorActor​(java.lang.String name,
                               SupervisorActor.RestartStrategy restartStrategy,
                               Initializer initializer)
        Constructs a new supervisor with no children. Children may be added later via Supervisor.addChild.
        Parameters:
        name - the actor name (may be null).
        restartStrategy - the supervisor's restart strategy
        initializer - an optional delegate object that will be run upon actor initialization and termination. May be null.
      • 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 be null).
        mailboxConfig - this actor's mailbox settings.
        restartStrategy - the supervisor's restart 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 be null).
        mailboxConfig - this actor's mailbox settings.
        restartStrategy - the supervisor's restart 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 be null).
        restartStrategy - the supervisor's restart 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 be null).
        restartStrategy - the supervisor's restart strategy
        childSpec - the supervisor's children
    • Method Detail

      • ref

        public Supervisor ref()
        Description copied from class: Actor
        Returns the ActorRef to this actor, if it has been started.
        Overrides:
        ref in class BehaviorActor
        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 class BehaviorActor
        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 class BehaviorActor
        Parameters:
        sf - the factory (or scheduler) 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 class BehaviorActor
        Parameters:
        ff - the factory (or scheduler) 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 class BehaviorActor
        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 class BehaviorActor
        Returns:
        This actors' ActorRef
      • log

        public org.slf4j.Logger log()
        Description copied from class: BehaviorActor
        The Logger object associated with this actor.
        Specified by:
        log in class BehaviorActor
      • 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 - the child'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 its id
        Parameters:
        id - the child's id 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 ActorRefs.
      • 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's id 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 actor
        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
      • 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 the receive methods if a LifecycleMessage is found in the mailbox. By default, if the message is an ExitMessage and its watch is null, i.e. it's a result of a link rather than a watch, it will throw a LifecycleException, which will, in turn, cause this exception to be thrown by the call to receive. 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 returning null), and have it processed by the caller to receive.

        This implementation respects ShutdownMessage and, upon receiving it, calls shutdown().

        Overrides:
        handleLifecycleMessage in class BehaviorActor
        Parameters:
        m - the message
        Returns:
        null if the message has been processed and should not be returned by receive