I'm puzzled by the meaning of Actor.receive() and context.become(..). I was
under the impression that Actor.receive() gives you the partial function
that determines how incoming messages are handled at this moment.
context().become(...) allows you to replace that behavior by another one.
But when I try to use this, it turns out that my assumption is incorrect.
Apparently it seems that receive() returns the initial behavior of your
actor, that is the partial function that you put in when you call
receive(...). Calling context().become(...) doesn't change this receive(..)
at all, it only changes the way your actor reacts to incoming messages. I
then carefully reread the docs, and this is also what is explained there.
But then again, I find this behavior confusing. For me, an actor only has
one behavior. I provide the initial one with receive(..) and then change it
using become(...). But I would like to have a way to get the current
behavior such that I can easily extend it (using context.become(...)). Is
there something like this in the current API? I know I can implement
something myself for this, but this feels IMHO like something the framework
should give me.
Here's an example that shows all of this in action. When you run this, I
was expecting to see as output: Received Hello, Received Hello again,
Received A, Received Hello again. But you get: Received Hello, Received
Hello again, Received A, Received Hello.
public class BecomeActor extends AbstractActor {
public BecomeActor() {
receive(ReceiveBuilder
.match(String.class, s -> {
System.out.println("Received " + s);
context().become(ReceiveBuilder.matchEquals(s, m -> {
System.out.println("Received " + m + " again");
}).build().orElse(receive()));
})
.matchAny(System.out::println)
.build());
}
public static void main(String[] args) throws Exception {
ActorSystem system = ActorSystem.create();
ActorRef b = system.actorOf(Props.create(BecomeActor.class));
b.tell("Hello", ActorRef.noSender());
b.tell("Hello", ActorRef.noSender());
b.tell("A", ActorRef.noSender());
b.tell("Hello", ActorRef.noSender());
Await.ready(system.whenTerminated(), Duration.Inf());
}
}
thanks,
Bert
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.