首页 文章

如何在Play Framework 2中从外部向演员发送消息?

提问于
浏览
0

我是Akka的新手,并尝试在Java中使用Play Framework 2编写一些代码并使用Akka . 要创建一个actor并向其发送测试消息,我有:

public class Global extends GlobalSettings {

    @Override
    public void onStart(Application app) {
        final ActorRef testActor = Akka.system().actorOf(Props.create(TestActor.class), "testActor");
        testActor.tell("hi", ActorRef.noSender());
    }
}

这项工作非常好,我看到我的演员收到了我的消息,这是我演员的代码:

public class TestActor extends UntypedActor {

    @Override
    public void onReceive(Object message) throws Exception {
        if(message.toString().equals("hi")){
            System.out.println("I received a HI");
        }else{
            unhandled(message);
        }
    }
}

非常简单 . 但是,如果我尝试从控制器发送消息:

public static Result index() {
        final ActorRef testActor = Akka.system().actorFor("testActor");
        testActor.tell("hi", ActorRef.noSender());
        return ok(index.render("Your new application is ready."));
    }

我在终端上收到此消息:

[INFO] [09/20/2014 11:40:30.850] [application-akka.actor.default-dispatcher-4] [akka:// application / testActor]来自Actor的消息[java.lang.String] [akka: // application / deadLetters]到Actor [akka:// application / testActor]没有交付 . [1]遇到死信 . 可以使用配置设置'akka.log-dead-letters'和'akka.log-dead-letters-during-shutdown'关闭或调整此日志记录 .

有人可以帮我弄这个吗?为什么第一次使用有效,第二次使用失败?谢谢

2 回答

  • 0

    actorFor 方法需要整个路径,并且您的actor存在于用户空间中,因此您必须使用 actorFor("/user/testActor") . 目前您将其发送到 application/testActor ,这将是ActorSystem本身的顶级演员 .

    顺便说一下, actorFor 已被弃用(至少在Scala API中)并被 actorSelection 取代 .

    有关更多信息,请参阅excellent documentation .

  • 1

    actorFor 应该获得演员的路径,这可能是"akka://System/user/testActor" . 它也不会创建actor,这意味着它应该存在 .

    无论如何,有没有理由在控制器内使用 actorFor 而不是 actorOf ?它had been deprecated并且不应该使用 .

相关问题