Scala access variable AKKA or return value

Here is my code:

class testActor extends Actor   {
    var test = "test2"
    def receive = {
            case "test" β‡’ 
                    test="works"
                    "works"

    }
}


 def test = Action {
    var test = "test"
    val system = ActorSystem("MySystem")
    val myActor = system.actorOf(Props[testActor.testActor], name = "testActor")

    myActor ! "test"

    test = myActor.test

Ok(views.html.test(test))
}

line: test = myActor.test does not work.

I need a way to access what is returned by the actor function, in which case "works" or a way to access the variable inside the Actor.

+5
source share
1 answer

To return the result to the sender, send him a message:

def receive = {
  case "test" => sender ! "works"
}

To wait for a response, use the call Await.result ():

  implicit val timeout = Timeout(Duration(1, TimeUnit.SECONDS))
  test = Await.result(myActor ? "test", Duration(1, TimeUnit.SECONDS))
+9
source

All Articles