Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interact with Akka actor outside actors

I want to interact with Akka actors from my own thread. Currently, i do like so:

val res = Await.result(aref ? GroupReceive(fromRank), timeout.duration).asInstanceOf[T]

But I am unsure how this actually interacts with my thread? I wish for the receive to be asynchronous, i.e. I want to hang up the thread while receiving to allow for some other work to be done. I just recently read about Akka inbox system. inbox akka api

I think I recall that Await creates a new actor every time. What are the differences between await+ask and inbox, and can someone give me an example of how to create an inbox and use it to communicate with actors from "the outside" ?

EDIT Just to clarify, I don't want the same thread to continue working, I want it to stop hogging a cpu-core and leave other threads to work until it receives something, then wake up again.

like image 703
Felix Avatar asked May 24 '13 09:05

Felix


1 Answers

As written in the Akka's Future documentation, using Await blocks current thread until awaiting for the result.

Example

import scala.concurrent.Await
import akka.pattern.ask
import akka.util.Timeout
import scala.concurrent.duration._

implicit val timeout = Timeout(5 seconds)
val future = actor ? msg // enabled by the “ask” import
val result = Await.result(future, timeout.duration).asInstanceOf[String]

This will cause the current thread to block and wait for the Actor to 'complete' the Future with it's reply.

Use With Actors

like image 183
4lex1v Avatar answered Nov 08 '22 19:11

4lex1v