简体   繁体   中英

How to find specific actor in Akka

In java

I have simple web application. In ServletContextListener I create actor

ActorSystem system = ActorSystem.create("MySystem");
actor = system.actorOf(new Props(MyServerActor.class), "MyServer");
actor.tell(new StartMessage());

this actor has path akka://MySystem/user/MyServer . Then I try to send message to this actor from business method

ActorSystem system = ActorSystem.create("MySystem");
client = system.actorSelection("/user/MyServer"); // same effect when use actorFor
client.tell("OK");

onReceive method:

@Override
public void onReceive(Object message) throws Exception {
    System.out.println(message + " :  " + message);
}

but my actor doesn't receive message. It look like i send it to /dev/null.

Where is mistake?

//edit:

I try to use fullpath too.

Ok I'm so stupid...

I create two System so the cannot access one to another, cannot find actor so message were send to /dev/null

you can try with remote actors:

String path = "akka://MySystem@some-ip:some-port/some/sub/path"
ActorRef xxx = actorSystem.actorFor(path);

Also, it would be better if you have subscribe the deadLetters() actor to record the unhandled messages.

final ActorRef actor = actorSystem.actorOf(new Props(DefaultDeadLetterHandlerActor.class));
actorSystem.eventStream().subscribe(actor, DeadLetter.class);

by maybe just simply log them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM