Class SelectiveReceiveHelper<Message>

  • All Implemented Interfaces:
    java.io.Serializable

    public class SelectiveReceiveHelper<Message>
    extends java.lang.Object
    implements java.io.Serializable
    Performs selective receive on behalf of an actor.
    See Also:
    Serialized Form
    • Constructor Detail

      • SelectiveReceiveHelper

        public SelectiveReceiveHelper​(Actor<Message,​?> actor)
        Creates a SelectiveReceiveHelper to add selective receive to an actor
        Parameters:
        actor - the actor
    • Method Detail

      • setActor

        public void setActor​(Actor<Message,​?> actor)
        used only during deserialization
      • receive

        public final <T> T receive​(MessageProcessor<? super Message,​T> proc)
                            throws SuspendExecution,
                                   java.lang.InterruptedException
        Performs a selective receive. This method blocks until a message that is selected by the given MessageProcessor is available in the mailbox, and returns the value returned by MessageProcessor.process.

        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.

        Type Parameters:
        T - The type of the returned value
        Parameters:
        proc - performs the selection.
        Returns:
        The non-null value returned by MessageProcessor.process
        Throws:
        java.lang.InterruptedException
        SuspendExecution
      • receive

        public final <T> T receive​(long timeout,
                                   java.util.concurrent.TimeUnit unit,
                                   MessageProcessor<? super Message,​T> 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.

        Type Parameters:
        T - The type of the returned value
        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 <T> T receive​(Timeout timeout,
                                   MessageProcessor<? super Message,​T> 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.

        Type Parameters:
        T - The type of the returned value
        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
      • tryReceive

        public final <T> T tryReceive​(MessageProcessor<? super Message,​T> proc)
        Tries to perform a selective receive. If a message selected by the given MessageProcessor is immediately available in the mailbox, returns the value returned by MessageProcessor.process. 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.

        Type Parameters:
        T - The type of the returned value
        Parameters:
        proc - performs the selection.
        Returns:
        The non-null value returned by MessageProcessor.process, or null if no message was slected.
      • ofType

        public static <Message,​M extends Message> MessageProcessor<Message,​M> ofType​(java.lang.Class<M> type)
        Creates a MessageProcessor that selects messages of the given class.
        Type Parameters:
        M -
        Message -
        Parameters:
        type - The class of the messages to select.
        Returns:
        a new MessageProcessor that selects messages of the given class.
      • receive

        public final <M extends Message> 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.

        Type Parameters:
        M - The type of the returned value
        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 extends Message> 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.

        Type Parameters:
        M - The type of the returned value
        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
      • receive

        public final <M extends Message> 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.

        Type Parameters:
        M - The type of the returned value
        Parameters:
        type - the type of the messages to select
        Returns:
        The next message of the wanted type.
        Throws:
        java.lang.InterruptedException
        SuspendExecution
      • tryReceive

        public final <M extends Message> 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.

        Type Parameters:
        M - The type of the returned value
        Parameters:
        type - the type of the messages to select
        Returns:
        The next message of the wanted type if immediately found; null otherwise.
      • handleLifecycleMessage

        protected void handleLifecycleMessage​(LifecycleMessage m)