2016-08-16 73 views
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,但我怎麼指定?

+0

從而起到版本是你使用? – Salem

+0

我在2.5.x Java –

回答

0

我剛纔已經回答在另一篇同樣的問題,但萬一這裏是:

你可以使用拉姆達對於這一點,這裏是一個簡單的例子:

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

如果同一答案在多個帖子中有效,那麼問題可能是重複的。這是最好的,如果你舉一個這樣的...... –

相關問題