Reputation: 1773
I'm stuck on a problem like this: I send messages to an actor, but it doesn't receive, specifically, the code:
class TestActor extends Actor {
def receive = {
case _ =>
println("null")
case Landau(args) =>
println("Got a Job" + args)
sender ! "Done"
}
}
and the sender, through "ask" method:
worker.ask(Landau(List("1", "2", "3")))
As a result, it doens't print anything, neither "null", any idea?
EDIT: this is in the main execution:
val system = ActorSystem("TestApplication")
val ref = system.actorOf(Props(new TestActor), "test")
and I don't know if relevant, but the main object and that code "worker.ask..." are in different packages
Upvotes: 4
Views: 3418
Reputation: 9635
The problem seem to be those two lines:
val worker = workerContext.actorFor("akka://TestApplication/user/test")
worker.ask(Landau(List("1", "2", "3")))
In the first line, actorFor was deprecated in Akka 2.2, you should use actorSelection now. Usage for actorSelection is a bit different, as it does not return an ActorRef as actorFor did, but an actorSelection you can use to get the ActorRef. You can do this by sending an Identify message and receiving an ActorIdentity message which contains the ActorRef you want. Or you simply use the resolveOne method of ActorSelection, the few times I used actorSelection, I always used it this way. This returns a Future holding your ActorRef. So your code here should be
val workerFuture = context actorSelection("/TestApplication/user/test") resolveOne
val worker = workerFuture = Await.result(actorFuture, 10 seconds)
The second problem might be that ask needs a slightly different syntax, as per the docs:
worker ask Landau(List("1", "2", "3"))
Give that a try.
Upvotes: 1
Reputation: 1773
Found: So, it looks like it was fault of the method actorFor, I was using this worker:
val worker = workerContext.actorFor("akka://TestApplication/user/test")
instead of this:
val worker = workerContext.actorOf(Props[TestActor])
and now it works. But I'm not sure of this choice, since I should point to that address.
Upvotes: 0