Akka child actors return 一个void类型并拒绝发送消息

Akka child actors return a void type and refuse to send messages

我正在学习 Akka Typed Actor 库并尝试使用 this.getContext().getChildren() 向特定 actor 的所有子 actor 发送消息。但它 returns 是 Void 类型,而不是我最初使用 -> 创建儿童演员的类型,因此我无法向儿童发送任何类型的消息。

我所做的解决方法是在创建时将子项简单地存储在哈希图中。但这不是可扩展的。

我怎样才能做到这一点?特别是在分布式场景下?

        .onMessage(GetPosition.class, message -> {

            // whenever I send a GetPosition() message to myself,
            // I get that reminder and send an AskPosition to all workers

            while (results.keySet().size() < 10) {
                // while not finished, ask for positions from child actors

                // This is not working
                this.getContext().getChildren().forEach(child -> {
                    child.tell("this is a void class, so I cannot send any message to children");
                });

                // WORKAROUND:
                // get child  actors from hashmap (hashmap lambda has 2 things in it -> bi-consumer)
                racers.forEach((id, actor) -> actor.tell(new RacerBehavior.AskPosition(this.getContext().getSelf())));
            }

            return this;
        })

因为无法静态知道任意 actor 将产生的 children 接受的消息类型,getChildren() 只能 return ActorRef<Void>s (或 Scala 中的 ActorRef[Nothing]s,它更好地表达了您无法向他们发送消息的想法。

如果您所有的 children 恰好是同一类型(这是能够遍历它们以发送消息的先决条件),这样的事情应该可行:

child.unsafeUpcast<Object>.narrow<RacerBehavior.AskPosition>

如果您不能绝对确定所有 children 都是同一类型,请使用 parent 参与者状态中的类型明确跟踪它们(例如,有一个集合 per-type) 是要走的路。