我正在尋找如何Patterns.askWithReplyTo使用 Java使用 Akka 模式的示例。Github 上提供了一個示例項目:https : //github.com/pcdhan/akka-patterns.git我的挑戰(zhàn)是我無法在有效負(fù)載中包含發(fā)件人的 ActorRef。本地演員ActorRef localA= system.actorOf(LocalActor.props(), "localA");遠(yuǎn)程演員Timeout timeout = new Timeout(10000, TimeUnit.MILLISECONDS);ActorSelection actorSelection=system.actorSelection("akka.tcp://ClusterSystem@localhost:2551/user/ActorA");Future<Object> future = Patterns.ask(actorSelection, new Identify(""), timeout);ActorIdentity reply = (ActorIdentity) Await.result(future, timeout.duration());ActorRef actorRef = reply.ref().get(); //my remote actor ref將有效負(fù)載與 ActorRef (localA) 一起發(fā)送到遠(yuǎn)程 ActorPayload payload = new Payload(); //How do I pass localA herepayload.setMsg("0");Future<Object> askWithSenderRef = Patterns.askWithReplyTo(actorRef,payload,20000L);Payload responsePayload = (Payload) Await.result(askWithSenderRef, timeout.duration());System.out.println("Response from Remote Actor Payload: "+responsePayload.getMsg());有效載荷public class Payload implements Function<ActorRef, Object>, Serializable {private static final long serialVersionUID = 1L;String msg;public String getMsg() { return msg;}public void setMsg(String msg) { this.msg = msg;}@Overridepublic Object apply(ActorRef param) throws Exception { return this;}}遠(yuǎn)程參與者日志...Actor[akka.tcp://ClusterSystem@localhost:53324/temp/$d]...Actor[akka.tcp://ClusterSystem@localhost:53324/temp/$e]我期望 .../user/localA,但我得到 /temp/$d
1 回答

一只萌萌小番薯
TA貢獻(xiàn)1795條經(jīng)驗 獲得超7個贊
askWithReplyTo并不意味著將發(fā)送參與者傳遞self到消息中。
askWithReplyTo 期望你給它一個工廠函數(shù),它被喂給臨時響應(yīng)actor,所以如果你有一個可以像這樣構(gòu)造的消息:
new MyMessage(ActorRef replyTo)
你可以askWithReplyTo像這樣使用它:
final Future<Object> f = Patterns.askWithReplyTo(
otherActor,
replyTo -> new MyMessage(replyTo),
timeout);
第二個參數(shù)是一個 lambda,它將與臨時 ask-actor 一起調(diào)用(它總是在您要求處理響應(yīng)超時時創(chuàng)建),以便您可以將它包含在消息中。
該模式僅在接收方使用該replyTo字段進(jìn)行響應(yīng)時才有用,而 not sender(),這是您通常用來響應(yīng)的方式。
添加回答
舉報
0/150
提交
取消