Class SelectiveReceiveHelper<Message>
- java.lang.Object
-
- co.paralleluniverse.actors.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 Summary
Constructors Constructor Description SelectiveReceiveHelper(Actor<Message,?> actor)
Creates aSelectiveReceiveHelper
to add selective receive to an actor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
handleLifecycleMessage(LifecycleMessage m)
static <Message,M extends Message>
MessageProcessor<Message,M>ofType(java.lang.Class<M> type)
Creates aMessageProcessor
that selects messages of the given class.<T> T
receive(long timeout, java.util.concurrent.TimeUnit unit, MessageProcessor<? super Message,T> proc)
Performs a selective receive.<M extends Message>
Mreceive(long timeout, java.util.concurrent.TimeUnit unit, java.lang.Class<M> type)
Performs a selective receive based on type.<T> T
receive(MessageProcessor<? super Message,T> proc)
Performs a selective receive.<T> T
receive(Timeout timeout, MessageProcessor<? super Message,T> proc)
Performs a selective receive.<M extends Message>
Mreceive(Timeout timeout, java.lang.Class<M> type)
Performs a selective receive based on type.<M extends Message>
Mreceive(java.lang.Class<M> type)
Performs a selective receive based on type.void
setActor(Actor<Message,?> actor)
used only during deserialization<T> T
tryReceive(MessageProcessor<? super Message,T> proc)
Tries to perform a selective receive.<M extends Message>
MtryReceive(java.lang.Class<M> type)
Tries to performs a selective receive based on type.
-
-
-
Method Detail
-
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 isselected
by the givenMessageProcessor
is available in the mailbox, and returns the value returned byMessageProcessor.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 isselected
by the givenMessageProcessor
is available in the mailbox, and returns the value returned byMessageProcessor.process
. If the given timeout expires, this method returnsnull
.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
, ornull
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 isselected
by the givenMessageProcessor
is available in the mailbox, and returns the value returned byMessageProcessor.process
. If the given timeout expires, this method returnsnull
.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 theTimeout
proc
- performs the selection.- Returns:
- The non-null value returned by
MessageProcessor.process
, ornull
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 messageselected
by the givenMessageProcessor
is immediately available in the mailbox, returns the value returned byMessageProcessor.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
, ornull
if no message was slected.
-
ofType
public static <Message,M extends Message> MessageProcessor<Message,M> ofType(java.lang.Class<M> type)
Creates aMessageProcessor
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 returnsnull
.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 returnsnull
.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 theTimeout
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 returnsnull
. 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)
-
-