Accessing AKKA Actor variables or get a returned value from an Actor

1,056 views
Skip to first unread message

Ryan Rasmussen

unread,
Jul 13, 2012, 4:14:31 PM7/13/12
to scala...@googlegroups.com
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))
}

the line: test = myActor.test doesn't work.

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

Naftoli Gugenheim

unread,
Jul 13, 2012, 4:23:49 PM7/13/12
to Ryan Rasmussen, scala...@googlegroups.com
I don't know akka per se, but you need the "reply" functionality, and something besides plain !.

Alec Zorab

unread,
Jul 14, 2012, 2:55:37 PM7/14/12
to Naftoli Gugenheim, Ryan Rasmussen, scala...@googlegroups.com
system.actorOf returns an actorRef, not a testActor.

Have a look in the akka docs how it works, but you basically need to do

import akka.pattern.ask

val test: Future[String] = (myActor ? "test").mapTo[String]

and then in the actor implementation, do sender ! "response".

As a side note, you can see that the type of receive is Any => Unit, so your return of "works" is getting discarded. I strongly recommend reading the akka documentation. They're very easy to understand and explain all of this stuff in depth.

http://akka.io/docs/
Reply all
Reply to author
Forward
0 new messages