0
我正在嘗試關注JavaWebSocket Tutorial on the official docs。如何在Play控制器中注入具有構造函數參數的Actor?
有這樣的演員:
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());
}
}
}
這是WebSocket的:
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 ?
}
後來init進程後,我喜歡從控制器方法發送消息給演員。
this.websocketactor = actorSystem.actorOf(MyWebSocketActor.props()); // this line is giving me errors because I don't know what goes in there.
這也許是ActorRef出,這是我的WebSocket,但我怎麼指定?
從而起到版本是你使用? – Salem
我在2.5.x Java –