Class RequestReplyHelper
- java.lang.Object
-
- co.paralleluniverse.actors.behaviors.RequestReplyHelper
-
public final class RequestReplyHelper extends java.lang.ObjectThis class contains static methods that implement a request-reply pattern with actors. These methods can be used to communicate with actors by other actors, or even by non-actor strands.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <V,M extends RequestMessage<V>>
Vcall(ActorRef<? super M> actor, M m)Sends a request message to an actor, awaits a response value and returns it.static <V> Vcall(ActorRef actor, RequestMessage<V> m, long timeout, java.util.concurrent.TimeUnit unit)Sends a request message to an actor, awaits a response value (but no longer than the given timeout) and returns it.static <V> Vcall(ActorRef actor, RequestMessage<V> m, Timeout timeout)Sends a request message to an actor, awaits a response value (but no longer than the given timeout) and returns it.static <Message> ActorRef<Message>from()Returns anActorRefthat should be used as the from property of aRequestMessage.static java.lang.ObjectmakeId()Generates a random, probably unique, message identifier.static <V> voidreply(RequestMessage<V> req, V result)Replies with a result to aRequestMessage.static voidreplyError(RequestMessage<?> req, java.lang.Throwable e)Replies with an exception to aRequestMessage.static voidsetDefaultTimeout(long timeout, java.util.concurrent.TimeUnit unit)Sets a default timeout for non-timedcalls on this strand.
-
-
-
Method Detail
-
makeId
public static java.lang.Object makeId()
Generates a random, probably unique, message identifier. This method simply callsActorUtil.randtag().- Returns:
- a newly allocated, probably unique, message identifier.
-
setDefaultTimeout
public static void setDefaultTimeout(long timeout, java.util.concurrent.TimeUnit unit)Sets a default timeout for non-timedcalls on this strand. Non-timed calls that take longer than the default timeout, will throw aTimeoutExceptionwrapped in aRuntimeException. Timed calls (those that take a timeout parameter) will not be affected.This method only affects the current strand.
- Parameters:
timeout- the timeout durationunit- the time unit of the timeout, ornullto unset.
-
from
public static <Message> ActorRef<Message> from()
Returns anActorRefthat should be used as the from property of aRequestMessage. If called from an actor strand, this method returns the current actor. If not, it creates a temporary faux-actor that will be used internally to receive the response, even if the current strand is not running an actor.- Type Parameters:
Message-- Returns:
- an
ActorRefthat should be used as the from property of a request, even if not called from within an actor.
-
call
public static <V,M extends RequestMessage<V>> V call(ActorRef<? super M> actor, M m) throws java.lang.InterruptedException, SuspendExecution
Sends a request message to an actor, awaits a response value and returns it. This method can be called by any code, even non-actor code. If the actor responds with an error message, aRuntimeExceptionwill be thrown by this method.
The message'sidandfromproperties may be left unset.This method should be used as in the following example (assuming a
Stringreturn value:
In the example,String res = call(actor, new MyRequest());MyRequestextendsRequestMessage. Note how the result of thefrommethod is passed to the request's constructor, but the message ID isn't.- Type Parameters:
V- the return value's type- Parameters:
actor- the actor to which the request is sentm- theRequestMessage, whoseidandfromproperties may be left unset.- Returns:
- the value sent by the actor as a response
- Throws:
java.lang.RuntimeException- if the actor responds with an error message, its contained exception will be thrown, possibly wrapped by aRuntimeException, or if adefault timeouthas been set and has expired.java.lang.InterruptedExceptionSuspendExecution
-
call
public static <V> V call(ActorRef actor, RequestMessage<V> m, long timeout, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.TimeoutException, java.lang.InterruptedException, SuspendExecution
Sends a request message to an actor, awaits a response value (but no longer than the given timeout) and returns it. This method can be called by any code, even non-actor code. If the actor responds with an error message, aRuntimeExceptionwill be thrown by this method.
The message'sidandfromproperties may be left unset.This method should be used as in the following example (assuming a
Stringreturn value:
In the example,String res = call(actor, new MyRequest());MyRequestextendsRequestMessage. Note how the result of thefrommethod is passed to the request's constructor, but the message ID isn't.- Type Parameters:
V- the return value's type- Parameters:
actor- the actor to which the request is senttimeout- the maximum duration to wait for a responseunit- the time unit of the timeout- Returns:
- the value sent by the actor as a response
- Throws:
java.lang.RuntimeException- if the actor responds with an error message, its contained exception will be thrown, possibly wrapped by aRuntimeException.java.util.concurrent.TimeoutException- if the timeout expires before a response is received from the actor.java.lang.InterruptedExceptionSuspendExecution
-
call
public static <V> V call(ActorRef actor, RequestMessage<V> m, Timeout timeout) throws java.util.concurrent.TimeoutException, java.lang.InterruptedException, SuspendExecution
Sends a request message to an actor, awaits a response value (but no longer than the given timeout) and returns it. This method can be called by any code, even non-actor code. If the actor responds with an error message, aRuntimeExceptionwill be thrown by this method.
The message'sidandfromproperties may be left unset.This method should be used as in the following example (assuming a
Stringreturn value:
In the example,String res = call(actor, new MyRequest());MyRequestextendsRequestMessage. Note how the result of thefrommethod is passed to the request's constructor, but the message ID isn't.- Type Parameters:
V- the return value's type- Parameters:
actor- the actor to which the request is senttimeout- the method will not block for longer than the amount remaining in theTimeout- Returns:
- the value sent by the actor as a response
- Throws:
java.lang.RuntimeException- if the actor responds with an error message, its contained exception will be thrown, possibly wrapped by aRuntimeException.java.util.concurrent.TimeoutException- if the timeout expires before a response is received from the actor.java.lang.InterruptedExceptionSuspendExecution
-
reply
public static <V> void reply(RequestMessage<V> req, V result) throws SuspendExecution
Replies with a result to aRequestMessage. If the request has been sent by a call tocall, theresultargument will be the value returned bycall. This method should only be called by an actor.Internally this method uses a
ValueResponseMessageto send the reply.- Parameters:
req- the request we're responding toresult- the result of the request- Throws:
SuspendExecution
-
replyError
public static void replyError(RequestMessage<?> req, java.lang.Throwable e) throws SuspendExecution
Replies with an exception to aRequestMessage. If the request has been sent by a call tocall, theeargument will be the exception thrown bycall(possibly wrapped by aRuntimeException). This method should only be called by an actor.Internally this method uses an
ErrorResponseMessageto send the reply.- Parameters:
req- the request we're responding toe- the error the request has caused- Throws:
SuspendExecution
-
-