问题
While working on the Akka tutorial, I was wondering in a scenario where I have the ActorSystem reference live in the application, I can dispatch messages directly to a child actor like /user/a
in the diagram below or must all messages that need to go to that child be passed down from /user
and from /
to /user
. I ask because in the tutorial, the IotSupervisor extends AbstractBehavior<Void>
and I imagine that that would need to be changed to extending AbstractBehavior<DeviceManager.Command>
. Thanks. [![enter image description here][1]][1]
EDIT: To ground this with code, I've included the following:
package org.example;
import akka.actor.ActorRef;
import akka.actor.typed.ActorSystem;
import akka.http.javadsl.server.AllDirectives;
import akka.http.javadsl.server.HttpApp;
import akka.http.javadsl.server.Route;
import akka.util.Timeout;
import scala.concurrent.duration.FiniteDuration;
import static akka.pattern.PatternsCS.ask;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;
public class Application extends AllDirectives {
public static class MinimalHttp extends HttpApp {
ActorRef deviceManagerActorRef;
@Override
protected Route routes() {
akka.actor.ActorSystem sys = this.systemReference.get();
return concat(
path("hello", () ->
get(() -> {
final Timeout timeout = Timeout.durationToTimeout(FiniteDuration.apply(5, TimeUnit.SECONDS));
CompletionStage<org.example.DeviceManager.DeviceRegistered> regged = ask(sys.guardian(), new DeviceManager.RequestTrackDevice("group", "dev1", null), timeout)
.thenApply(org.example.DeviceManager.DeviceRegistered.class::cast)
.thenApply(r -> {
System.out.println("payload>>>>>>"+r);
return r;
});
regged = ask(sys.guardian(), new DeviceManager.RequestTrackDevice("group", "dev1", null), timeout)
.thenApply(org.example.DeviceManager.DeviceRegistered.class::cast)
.thenApply(r -> {
System.out.println("payload>>>>>>"+r);
return r;
});
return complete("foo");
}
)));
}
}
public static void main(String[] args) throws Exception {
final ActorSystem typedSystem = ActorSystem.create(IotSupervisor.create(), "iot-sup");
akka.actor.ActorSystem sys = typedSystem.classicSystem();
final MinimalHttp server = new MinimalHttp();
server.startServer("localhost", 8080, sys);
}
}
I'm currently getting this:
11:27:40.134 [iot-sup-akka.actor.default-dispatcher-3] INFO akka.actor.LocalActorRef - Message [org.example.DeviceManager$RequestTrackDevice] from Actor[akka://iot-sup/temp/$a] to Actor[akka://iot-sup/user] was unhandled. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
11:27:40.134 [iot-sup-akka.actor.default-dispatcher-3] INFO akka.actor.LocalActorRef - Message [org.example.DeviceManager$RequestTrackDevice] from Actor[akka://iot-sup/temp/$b] to Actor[akka://iot-sup/user] was unhandled. [2] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
while I'm at least expecting creating group actor for group
to be logged as well as information about the newly created device to be logged.
[1]: https://i.stack.imgur.com/MjVZD.png
来源:https://stackoverflow.com/questions/62868001/is-it-ever-ok-to-dispatch-a-child-actor-directly-or-must-all-messages-be-passed