I am starting to learn Akka by migrating an existing Java SE app to it. I am using Akka 2.0.3.
At one point I need to send a PoisonPill through the message queue to stop the actors. My actor is instantiated thus:
ActorRef myActor = actorSystem.actorOf(new Props(MyActor.class), "myActor");
to which I try to send the PoisonPill:
myActor.tell(PoisonPill.getInstance());
But I get the following compiler error:
'tell(java.lang.Object)' in 'akka.actor.ActorRef' cannot be applied to '(akka.actor.PoisonPill$)'
What am I doing wrong? I'm running Java 1.6.0_26 in Idea (which I am also learning after a lifetime in Eclipse).
Edit:
I have also tried this approach, which is in the documentation, but I get the same compiler error and Idea warns me that the Actors class is deprecated.
import static akka.actor.Actors.*;
extractionActor.tell(poisonPill());
1) Akka Actor tell() Method It works on "fire-forget" approach. You can also use ! (bang) exclamation mark to send message. This is the preferred way of sending messages.
In Akka, you can stop Actors by invoking the stop() method of either ActorContext or ActorSystem class. ActorContext is used to stop child actor and ActorSystem is used to stop top level Actor. The actual termination of the actor is performed asynchronously.
Actors are objects (class instances in Java sense) that may have mutable state and normally follow the standard rules: Everything is an actor. Actors communicate with each other exclusively by sending asynchronous messages. There is no shared state, public static variables, etc.
Another way to stop an actor is by sending a special type of message, called PoisonPill, to the actor. This message acts like any other message and will be queued up in the actor's mailbox. When the actor finds this message, it will stop itself.
As mentioned in my reply to the comment above, this does not work in Idea or when using gradle to compile. It is in fact a compilation error since the sender ActorRef is required. I know the previous answers are old, and i'm not sure if this was a change in the api, so for anyone having a similar issue you should be using :
target.tell(PoisonPill.getInstance(), ActorRef.noSender());
For reference see : http://doc.akka.io/docs/akka/snapshot/java/lambda-actors.html#PoisonPill
Please read the Akka documentation, we've spent a lot of time creating it:
PoisonPill
You can also send an actor the akka.actor.PoisonPill message, which will stop the actor when the message is processed. PoisonPill is enqueued as ordinary messages and will be handled after messages that were already queued in the mailbox.
Use it like this:
import static akka.actor.Actors.*; myActor.tell(poisonPill());
The above approach has been deprecated since 2.0.2, this is the new API:
ActorRef ref = system.actorOf(new Props(JavaAPITestActor.class));
ref.tell(PoisonPill.getInstance());
The above compiles on my machine so you might have some issue in IDEA? Try to compile it with javac and see if that works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With