How to use Akka template without blocking

Hi I have an actor who is in charge of fetching data from the database, turning it into a list and sending it back to the sender. I'm using a template ask

to get a response from my actor because I don't want to use await.result

it because this approach is blocking the thread, which is not good.

I wrote the following code to get the response from my actor (I just skipped the db code to keep it simple, but the logic remains the same):

class MyActor extends Actor{
   var list = new MutableList[Int]()
   list+=1
   list+=2
   list+=3
   list+=4

   def receive ={
     case "returnAlist"=>
       println("sending back list "+list +"of size "+list.size)
       sender ! list
     case message => 
       unhandled(message)
   }
}

object Test extends App{
  val system = ActorSystem("testing")
  val MyActor = system.actorOf(Props[MyActor], name = "InstitutionUserSuggestion")
  implicit val timeout = Timeout(15 seconds)
  var ResultList = new MutableList[Int]()
  val future:Future[MutableList[Int]] = ask(MyActor,"returnAlist").mapTo[MutableList[Int]] 
  future.onComplete { 
    case Success(result)=>
      println(" in sucees start")
      println("value of result "+ result.size)
      println("ResultList=result")
      ResultList = result
      println("value of ResultList "+ ResultList.size)
      println("in Success end")
    case Failure(e)=>
      println(" in failure")
      e.printStackTrace()
  }

  println("returned list size is " + ResultList.size + " and its contents" + ResultList)


}

      

Here is the output of the above code:

sending back list MutableList(1, 2, 3, 4)of size 4
returned list size is 0 and its contenstsMutableList()
in sucees start
value of result 4
 ResultList=result
value of ResultList 4
 in Success end

      

My problem is the code onComplete

is executed at the end, I need to display the contents of my list ResultList

on the console, but this line

println("returned list size is "+ResultList.size +" and its contensts" +ResultList)

      

which I write after the block onComplete

, I think it is executed before the block onComplete

, so there is nothing in the list at the end ResultList

, so I cannot print its items to the console.

Please help me what should I do to get a list of items from an actor without blocking and then display it on the console.

+3


source to share


1 answer


You don't want to block while waiting for the actor to respond, so you use it correctly Future

. The code in the function onComplete

is executed when your participant replies with a list.

And since you don't want to block and process it asynchronously, your last statement println

is executed while your actor hasn't responded yet.

You can print the contents of the list in a block onComplete

:



object Test extends App{
  val system = ActorSystem("testing")
  val MyActor = system.actorOf(Props[MyActor], name = "InstitutionUserSuggestion")
  implicit val timeout = Timeout(15 seconds)

  val future: Future[List[Int]] = ask(MyActor,"returnAlist").mapTo[List[Int]] 
  future.onComplete { 
    case Success(result)=>
      println("returned list size is " + result.size +" and its contents" + result)
    case Failure(e)=>
      println("in failure")
      e.printStackTrace()
  }
}

      

As a side note, I used immutable List

, which is more idiomatic to Scala than using volatile collections for example MutableList

.

+2


source







All Articles