Class FiniteStateMachineActor

  • 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 FiniteStateMachineActor
    extends BehaviorActor
    A behavior implementing a finite-state-machine. The FiniteStateMachineActor's behavior is implemented by overriding the initialState() method to return an initial *state* which then runs as the actor's body until it returns a next state, and so on until a state returns TERMINATE, in which case the actor terminates. Initialization and termination behavior can be implemented by either 1) subclassing this class and overriding some or all of: init, terminate, or 2) providing an instance of Initializer which implements these methods to the constructor.
    See Also:
    Serialized Form
    • Constructor Detail

      • FiniteStateMachineActor

        public FiniteStateMachineActor​(java.lang.String name,
                                       Initializer initializer,
                                       Strand strand,
                                       MailboxConfig mailboxConfig)
        Creates a new FSM actor
        Parameters:
        name - the actor name (may be null).
        initializer - an optional delegate object that will be run upon actor initialization and termination. May be null.
        strand - this actor's strand.
        mailboxConfig - this actor's mailbox settings.
      • FiniteStateMachineActor

        public FiniteStateMachineActor​(java.lang.String name,
                                       Initializer initializer,
                                       Strand strand,
                                       MailboxConfig mailboxConfig,
                                       SuspendableCallable<SuspendableCallable> initialState)
        Creates a new FSM actor
        Parameters:
        name - the actor name (may be null).
        initializer - an optional delegate object that will be run upon actor initialization and termination. May be null.
        strand - this actor's strand.
        mailboxConfig - this actor's mailbox settings.
        initialState - the initial state; will be used instead of calling initialState().
      • FiniteStateMachineActor

        public FiniteStateMachineActor​(java.lang.String name,
                                       Initializer initializer,
                                       MailboxConfig mailboxConfig)
        Creates a new FSM actor
        Parameters:
        name - the actor name (may be null).
        initializer - an optional delegate object that will be run upon actor initialization and termination. May be null.
        mailboxConfig - this actor's mailbox settings.
      • FiniteStateMachineActor

        public FiniteStateMachineActor​(java.lang.String name,
                                       Initializer initializer)
        Creates a new FSM actor
        Parameters:
        name - the actor name (may be null).
        initializer - an optional delegate object that will be run upon actor initialization and termination. May be null.
      • FiniteStateMachineActor

        public FiniteStateMachineActor​(Initializer initializer,
                                       MailboxConfig mailboxConfig)
        Creates a new FSM actor
        Parameters:
        initializer - an optional delegate object that will be run upon actor initialization and termination. May be null.
        mailboxConfig - this actor's mailbox settings.
      • FiniteStateMachineActor

        public FiniteStateMachineActor​(Initializer initializer)
        Creates a new FSM actor
        Parameters:
        initializer - an optional delegate object that will be run upon actor initialization and termination. May be null.
      • FiniteStateMachineActor

        public FiniteStateMachineActor​(java.lang.String name,
                                       MailboxConfig mailboxConfig)
        Creates a new FSM actor
        Parameters:
        name - the actor name (may be null).
        mailboxConfig - this actor's mailbox settings.
      • FiniteStateMachineActor

        public FiniteStateMachineActor​(java.lang.String name)
        Creates a new FSM actor
        Parameters:
        name - the actor name (may be null).
      • FiniteStateMachineActor

        public FiniteStateMachineActor​(MailboxConfig mailboxConfig)
        Creates a new FSM actor
        Parameters:
        mailboxConfig - this actor's mailbox settings.
      • FiniteStateMachineActor

        public FiniteStateMachineActor()
        Creates a new FSM actor
    • Method Detail

      • log

        public org.slf4j.Logger log()
        Description copied from class: BehaviorActor
        The Logger object associated with this actor.
        Specified by:
        log in class BehaviorActor
      • initialState

        protected SuspendableCallable<SuspendableCallable> initialState()
        Returns this finite-state-machine actor's initial state; the default implementation returns TERMINATE.
        Returns:
        this finite-state-machine actor's initial state
      • receive

        public final SuspendableCallable<SuspendableCallable> receive​(long timeout,
                                                                      java.util.concurrent.TimeUnit unit,
                                                                      MessageProcessor<java.lang.Object,​SuspendableCallable<SuspendableCallable>> proc)
                                                               throws java.util.concurrent.TimeoutException,
                                                                      SuspendExecution,
                                                                      java.lang.InterruptedException
        Performs a selective receive. This method blocks (but for no longer than the given timeout) until a message that is selected by the given MessageProcessor is available in the mailbox, and returns the value returned by MessageProcessor.process. If the given timeout expires, this method returns null.

        Messages that are not selected, are temporarily skipped. They will remain in the mailbox until another call to receive (selective or non-selective) retrieves them.

        Parameters:
        timeout - the duration to wait for a matching message to arrive.
        unit - timeout's time unit.
        proc - performs the selection.
        Returns:
        The non-null value returned by MessageProcessor.process, or null if the timeout expired.
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.TimeoutException
        SuspendExecution
      • receive

        public final SuspendableCallable<SuspendableCallable> receive​(Timeout timeout,
                                                                      MessageProcessor<java.lang.Object,​SuspendableCallable<SuspendableCallable>> proc)
                                                               throws java.util.concurrent.TimeoutException,
                                                                      SuspendExecution,
                                                                      java.lang.InterruptedException
        Performs a selective receive. This method blocks (but for no longer than the given timeout) until a message that is selected by the given MessageProcessor is available in the mailbox, and returns the value returned by MessageProcessor.process. If the given timeout expires, this method returns null.

        Messages that are not selected, are temporarily skipped. They will remain in the mailbox until another call to receive (selective or non-selective) retrieves them.

        Parameters:
        timeout - the method will not block for longer than the amount remaining in the Timeout
        proc - performs the selection.
        Returns:
        The non-null value returned by MessageProcessor.process, or null if the timeout expired.
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.TimeoutException
        SuspendExecution
      • receive

        public final <M> M receive​(java.lang.Class<M> type)
                            throws SuspendExecution,
                                   java.lang.InterruptedException
        Performs a selective receive based on type. This method blocks until a message of the given type is available in the mailbox, and returns it.

        Messages that are not selected, are temporarily skipped. They will remain in the mailbox until another call to receive (selective or non-selective) retrieves them.

        Parameters:
        type - the type of the messages to select
        Returns:
        The next message of the wanted type.
        Throws:
        java.lang.InterruptedException
        SuspendExecution
      • receive

        public final <M> M receive​(long timeout,
                                   java.util.concurrent.TimeUnit unit,
                                   java.lang.Class<M> type)
                            throws SuspendExecution,
                                   java.lang.InterruptedException,
                                   java.util.concurrent.TimeoutException
        Performs a selective receive based on type. This method blocks (but for no longer than the given timeout) until a message of the given type is available in the mailbox, and returns it. If the given timeout expires, this method returns null.

        Messages that are not selected, are temporarily skipped. They will remain in the mailbox until another call to receive (selective or non-selective) retrieves them.

        Parameters:
        timeout - the duration to wait for a matching message to arrive.
        unit - timeout's time unit.
        type - the type of the messages to select
        Returns:
        The next message of the wanted type, or null if the timeout expires.
        Throws:
        SuspendExecution
        java.lang.InterruptedException
        java.util.concurrent.TimeoutException
      • receive

        public final <M> M receive​(Timeout timeout,
                                   java.lang.Class<M> type)
                            throws SuspendExecution,
                                   java.lang.InterruptedException,
                                   java.util.concurrent.TimeoutException
        Performs a selective receive based on type. This method blocks (but for no longer than the given timeout) until a message of the given type is available in the mailbox, and returns it. If the given timeout expires, this method returns null.

        Messages that are not selected, are temporarily skipped. They will remain in the mailbox until another call to receive (selective or non-selective) retrieves them.

        Parameters:
        timeout - the method will not block for longer than the amount remaining in the Timeout
        type - the type of the messages to select
        Returns:
        The next message of the wanted type, or null if the timeout expires.
        Throws:
        SuspendExecution
        java.lang.InterruptedException
        java.util.concurrent.TimeoutException
      • tryReceive

        public final <M> M tryReceive​(java.lang.Class<M> type)
        Tries to performs a selective receive based on type. If a message of the given type is immediately found in the mailbox, it is returned. Otherwise this method returns null. This method never blocks.

        Messages that are not selected, are temporarily skipped. They will remain in the mailbox until another call to receive (selective or non-selective) retrieves them.

        Parameters:
        type - the type of the messages to select
        Returns:
        The next message of the wanted type if immediately found; null otherwise.
      • readResolve

        protected java.lang.Object readResolve()
                                        throws java.io.ObjectStreamException
        Overrides:
        readResolve in class Actor<java.lang.Object,​java.lang.Void>
        Throws:
        java.io.ObjectStreamException