Adding 'ask' to replace 'sendRequestReplyFuture' and removing sendRequestReply
This commit is contained in:
parent
ec9c2e100e
commit
fd5afde4ff
11 changed files with 25 additions and 62 deletions
|
|
@ -17,11 +17,11 @@ Use with Actors
|
|||
|
||||
There are generally two ways of getting a reply from an ``UntypedActor``: the first is by a sent message (``actorRef.sendOneWay(msg);``), which only works if the original sender was an ``UntypedActor``) and the second is through a ``Future``.
|
||||
|
||||
Using the ``ActorRef``\'s ``sendRequestReplyFuture`` method to send a message will return a Future. To wait for and retrieve the actual result the simplest method is:
|
||||
Using the ``ActorRef``\'s ``ask`` method to send a message will return a Future. To wait for and retrieve the actual result the simplest method is:
|
||||
|
||||
.. code-block:: java
|
||||
|
||||
Future[Object] future = actorRef.sendRequestReplyFuture[Object](msg);
|
||||
Future[Object] future = actorRef.ask[Object](msg);
|
||||
Object result = future.get(); //Block until result is available, usually bad practice
|
||||
|
||||
This will cause the current thread to block and wait for the ``UntypedActor`` to 'complete' the ``Future`` with it's reply. Due to the dynamic nature of Akka's ``UntypedActor``\s this result can be anything. The safest way to deal with this is to specify the result to an ``Object`` as is shown in the above example. You can also use the expected result type instead of ``Any``, but if an unexpected type were to be returned you will get a ``ClassCastException``. For more elegant ways to deal with this and to use the result without blocking, refer to `Functional Futures`_.
|
||||
|
|
|
|||
|
|
@ -109,7 +109,7 @@ Send messages
|
|||
Messages are sent to an Actor through one of the 'send' methods.
|
||||
* 'sendOneWay' means “fire-and-forget”, e.g. send a message asynchronously and return immediately.
|
||||
* 'sendRequestReply' means “send-and-reply-eventually”, e.g. send a message asynchronously and wait for a reply through a Future. Here you can specify a timeout. Using timeouts is very important. If no timeout is specified then the actor’s default timeout (set by the 'getContext().setTimeout(..)' method in the 'ActorRef') is used. This method throws an 'ActorTimeoutException' if the call timed out.
|
||||
* 'sendRequestReplyFuture' sends a message asynchronously and returns a 'Future'.
|
||||
* 'ask' sends a message asynchronously and returns a 'Future'.
|
||||
|
||||
In all these methods you have the option of passing along your 'ActorRef' context variable. Make it a practice of doing so because it will allow the receiver actors to be able to respond to your message, since the sender reference is sent along with the message.
|
||||
|
||||
|
|
@ -158,11 +158,11 @@ Here are some examples:
|
|||
Send-And-Receive-Future
|
||||
^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Using 'sendRequestReplyFuture' will send a message to the receiving Actor asynchronously and will immediately return a 'Future'.
|
||||
Using 'ask' will send a message to the receiving Actor asynchronously and will immediately return a 'Future'.
|
||||
|
||||
.. code-block:: java
|
||||
|
||||
Future future = actorRef.sendRequestReplyFuture("Hello", getContext(), 1000);
|
||||
Future future = actorRef.ask("Hello", getContext(), 1000);
|
||||
|
||||
The 'Future' interface looks like this:
|
||||
|
||||
|
|
@ -182,7 +182,7 @@ So the normal way of working with futures is something like this:
|
|||
|
||||
.. code-block:: java
|
||||
|
||||
Future future = actorRef.sendRequestReplyFuture("Hello", getContext(), 1000);
|
||||
Future future = actorRef.ask("Hello", getContext(), 1000);
|
||||
future.await();
|
||||
if (future.isCompleted()) {
|
||||
Option resultOption = future.result();
|
||||
|
|
@ -305,7 +305,7 @@ On this 'Option' you can invoke 'boolean isDefined()' or 'boolean isEmpty()' to
|
|||
Reply using the sender future
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
If a message was sent with the 'sendRequestReply' or 'sendRequestReplyFuture' methods, which both implements request-reply semantics using Future's, then you either have the option of replying using the 'reply' method as above. This method will then resolve the Future. But you can also get a reference to the Future directly and resolve it yourself or if you would like to store it away to resolve it later, or pass it on to some other Actor to resolve it.
|
||||
If a message was sent with the 'sendRequestReply' or 'ask' methods, which both implements request-reply semantics using Future's, then you either have the option of replying using the 'reply' method as above. This method will then resolve the Future. But you can also get a reference to the Future directly and resolve it yourself or if you would like to store it away to resolve it later, or pass it on to some other Actor to resolve it.
|
||||
|
||||
The reference to the Future resides in the 'ActorRef' instance and can be retrieved using 'Option<Promise> getSenderFuture()'.
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue