I am using play framework with a mix of scala and java. I can return a 
promise to the play framework and that promise can call the actor to get 
the information in a future. No Problem there. The problem is when an actor 
(A) needs something from another actor (B) and B needs something from A. A 
sends to a list of Bs and B sends a different message to A and then 
composes it. Think of generating JSON from a Customer -> Order Situation 
where Orders can have several different customers. The order JSON needs to 
have info from the customer in detail but for each of the orders we have to 
return all customer ids involved in the order. At some point I need to 
collect the results from all the calls to B and shove them into the result 
map. I can pass the map in the message to B and have it add the data but 
then if I have a pool of 5 actors and 5 customers are processing then when 
it calls back into A to get the customer ids of other customers involved in 
the order, it needs to use an instance of actor A in the pool to accomplish 
this. If they are waiting on the completion of a future then that cant 
happen. 

An example of what I am talking about is below. Please excuse that I coded 
this cold without compiling but hopefully you get the idea. 

class CustomerActor extends Actor {
    override def receive: Receive = {
      case msg: OrderHistoryJsonMsg =>
        val customer = Customer.findInCacheById(msg.customerId)
        val jsonMap = scala.collection.mutable.Map()
        // init JSON with customer data.
        val selection = Akka.system().actorSelection("/user/orderActors")
        val futures = customer.orderIds.map(orderId => {
          val future = selection ? new OrderJsonMsg(orderId)
          future.onComplete(r => {
            // add data to JSON.
          })
          future
        })
        futures.foreach(f => Await.result(f, 1 seconds))
        sender ! jsonMap
      case msg =>
        unhandled(msg)
    }
}

class OrderActor extends Actor {
  override def receive: Receive = {
    case msg: OrderJsonMsg =>
      val jsonMap = scala.collection.mutable.Map()
      // init JSON with order data.
      val order = Order.findInCacheById(msg.orderId)
      // NOTE HERE WE SEND MESSAGE BACK TO CUSTOMER ACTOR
      val selection = Akka.system().actorSelection("/user/orderActors")
      val futures =  order.customers.foreach(customerId => {
        val future = selection ? new CustomerNameMsg(customerId)
        future.onComplete(r => {
          // add data to JSON.
          sender ! jsonMap
        })
      })
    case msg =>
      unhandled(msg)
  }
}


On Wednesday, August 26, 2015 at 12:22:02 PM UTC-5, rrodseth wrote:
>
> Robert, in my case the REST endoint is using Spray. The per-request actor 
> has a reference to the RequestContext, and calls complete() on it, before 
> stopping itself.
>
> I don't have time to check, but it might be modelled on this Activator 
> Template (which I think is referenced in the net-a-porter blog post).
>
> http://www.typesafe.com/activator/template/spray-actor-per-request
>
> On Wed, Aug 26, 2015 at 9:37 AM, Viktor Klang <[email protected] 
> <javascript:>> wrote:
>
>> I still have no idea what you mean by "wait" here. Do you mean "block"?
>> Please either show some code to illustrate or be more specific, it's 
>> impossible to answer otherwise and it just ends up being a lot of replying 
>> for nothing. :(
>>
>> -- 
>> Cheers,
>> √
>> On 26 Aug 2015 08:28, "kraythe" <[email protected] <javascript:>> wrote:
>>
>>> If you need the results of the generated futures then it waits for them 
>>> all to be completed. Process A needs the results of Process B in order to 
>>> complete. This is a pretty common paradigm in our software. 
>>>
>>> -- Robert
>>>
>>> On Tuesday, August 25, 2015 at 9:43:11 PM UTC-5, √ wrote:
>>>>
>>>> how does that wait?
>>>>
>>>> -- 
>>>> Cheers,
>>>> √
>>>> On 25 Aug 2015 18:17, "kraythe" <[email protected]> wrote:
>>>>
>>>>> Call another actor using an ask pattern.
>>>>>
>>>>> On Tuesday, August 25, 2015 at 7:22:00 PM UTC-5, √ wrote:
>>>>>>
>>>>>> define "wait for the responses to send back to the caller"
>>>>>>
>>>>>> On Tue, Aug 25, 2015 at 12:04 PM, kraythe <[email protected]> wrote:
>>>>>>
>>>>>>> Lets say that we create a pool of 5 actors using round robin logic 
>>>>>>> and then we send 5 messages at the same time and those messages in the 
>>>>>>> course of being processed send another 5 messages each to the actor and 
>>>>>>> wait for the responses to send back to the caller. If this was a java 
>>>>>>> executor it would eventually deadlock waiting for the second set of 
>>>>>>> calls 
>>>>>>> to complete. Is the same deadlock possible with Akka? If not then what 
>>>>>>> would prevent it? 
>>>>>>>
>>>>>>> Thanks in advance. 
>>>>>>>
>>>>>>> -- 
>>>>>>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>> >>>>>>>>>> Check the FAQ: 
>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>> >>>>>>>>>> Search the archives: 
>>>>>>> https://groups.google.com/group/akka-user
>>>>>>> --- 
>>>>>>> You received this message because you are subscribed to the Google 
>>>>>>> Groups "Akka User List" group.
>>>>>>> To unsubscribe from this group and stop receiving emails from it, 
>>>>>>> send an email to [email protected].
>>>>>>> To post to this group, send email to [email protected].
>>>>>>> Visit this group at http://groups.google.com/group/akka-user.
>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> -- 
>>>>>> Cheers,
>>>>>> √
>>>>>>
>>>>> -- 
>>>>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>> >>>>>>>>>> Check the FAQ: 
>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>> >>>>>>>>>> Search the archives: 
>>>>> https://groups.google.com/group/akka-user
>>>>> --- 
>>>>> You received this message because you are subscribed to the Google 
>>>>> Groups "Akka User List" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send 
>>>>> an email to [email protected].
>>>>> To post to this group, send email to [email protected].
>>>>> Visit this group at http://groups.google.com/group/akka-user.
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>> -- 
>>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>>> >>>>>>>>>> Check the FAQ: 
>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>> >>>>>>>>>> Search the archives: 
>>> https://groups.google.com/group/akka-user
>>> --- 
>>> You received this message because you are subscribed to the Google 
>>> Groups "Akka User List" group.
>>> To unsubscribe from this group and stop receiving emails from it, send 
>>> an email to [email protected] <javascript:>.
>>> To post to this group, send email to [email protected] 
>>> <javascript:>.
>>> Visit this group at http://groups.google.com/group/akka-user.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> -- 
>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>> >>>>>>>>>> Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "Akka User List" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> Visit this group at http://groups.google.com/group/akka-user.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to