The difference between demand and story in Akka?

Reading a Scala Document I find it hard to understand the difference between ask and tell.

http://doc.akka.io/docs/akka/snapshot/scala/actors.html :

! means fire and forget, for example. send the message asynchronously and return immediately. Also known as say.

? sends a message asynchronously and returns a future representing a possible response. Also known as ask.

If the actor I'm using is launching a web request, what is the difference between the request and the tooltip? In both cases, the request will be generated asynchronously and must wait for a response, in other words, how to say immediately if the actor calls the web service and is waiting for a response?

+8
asynchronous scala akka
source share
2 answers

It seems that you already know the main difference between ask and tell , but do not understand how tell can be used to attract other participants in the processing of HTTP requests.

To make sense of using tell in HTTP request handlers, you must use an HTTP server that does not require request handlers to return their responses. Spray is such an HTTP server.

The request handler does not return a response to the spray; it is given a RequestContext object, and the response to the request involves calling some method on it. You can simply send this RequestContext to another player, who can then respond to the request:

 path("foo") { rc => rc complete "foo" // respond here } ~ path("bar") { rc => barActor ! DoBar(rc) // send it to bar; NO RESPONSE HERE } 

Then the actor referenced by barActor could say

 case DoBar(rc) => rc complete "bar" // respond here, in another actor 

The fact that Spray packs the request context into an object that can be passed and completed from any actor is great for the actor model. If, on the other hand, your web infrastructure requires the called handler to return a response, then if you want to attract another actor, your only choice is to use ask .

Typesafe has announced that Play will soon be using Spray below. Hopefully this means that Play then allow other members to send requests for processing.

+10
source share

The difference between ask and tell depends on the point of view of the sender of the message (which is not necessarily an actor). ask will send a message and return the future, which can be expected before the timeout or response expires, tell will send a message and immediately return.

In the case of ask , the actor who receives the message must reply to the sender when the operation is completed.

+12
source share

All Articles