【问题标题】:How do I inject an Actor with constructor parameters in a Play controller?如何在 Play 控制器中注入带有构造函数参数的 Actor?
【发布时间】:2016-08-16 06:35:06
【问题描述】:

我正在尝试关注JavaWebSocket Tutorial on the official docs

有这个演员:

import akka.actor.*;

public class MyWebSocketActor extends UntypedActor {

    public static Props props(ActorRef out) {
        return Props.create(MyWebSocketActor.class, out);
    }

    private final ActorRef out;

    public MyWebSocketActor(ActorRef out) {
        this.out = out;
    }

    public void onReceive(Object message) throws Exception {
        if (message instanceof String) {
            out.tell("I received your message: " + message, self());
        }
    }
}

这是网络套接字:

public static LegacyWebSocket<String> socket() {
    return WebSocket.withActor(MyWebSocketActor::props);
}

这是我的控制器:

@Singleton
public class MessagesController extends BaseController implements CurrentUser {

    private UserProvider userProvider;
    private ActorSystem actorSystem;
    private Materializer materializer;
    private Configuration configuration;
    ActorRef websocketactor;


    @Inject
    public MessagesController(final UserProvider userProvider,
                              ActorSystem actorSystem,
                              Materializer materializer,
                              Configuration configuration

    ) {
        this.userProvider = userProvider;
        this.actorSystem = actorSystem;
        this.materializer = materializer;
        this.configuration = configuration;
        this.websocketactor = actorSystem.actorOf(); // What goes in here ? 

    }

在初始化过程之后,我想从控制器方法向actor发送消息。

 this.websocketactor = actorSystem.actorOf(MyWebSocketActor.props()); // this line is giving me errors because I don't know what goes in there. 

可能是 ActorRef out,这是我的 websocket,但我该如何指定呢?

【问题讨论】:

  • 你用的是哪个播放版本?

标签: java playframework akka


【解决方案1】:

我刚刚在另一篇文章中回答了同样的问题,但以防万一:

您可以为此使用 lambda,这是一个简单的示例:

  public LegacyWebSocket<String> socket(String token) {
        return WebSocket.withActor(actorRef -> WSActor.props(actorRef,token));
  }

【讨论】:

  • 如果相同的答案在多个帖子中有效,那么问题可能是重复的。最好将其中一个标记为这样...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-31
  • 1970-01-01
  • 1970-01-01
  • 2012-07-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多