【发布时间】:2018-10-27 15:52:33
【问题描述】:
我正在寻找一个如何通过 Java 使用 Akka 的模式 Patterns.askWithReplyTo 的示例。
Github 上有一个示例项目:https://github.com/pcdhan/akka-patterns.git
我的挑战是我无法在有效负载中包含发件人的 ActorRef。
本地演员
ActorRef localA= system.actorOf(LocalActor.props(), "localA");
远程 Actor
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
将有效负载与 ActorRef (localA) 一起发送到远程 Actor
Payload payload = new Payload(); //How do I pass localA here
payload.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;
}
@Override
public Object apply(ActorRef param) throws Exception {
return this;
}
}
远程 Actor 日志
...Actor[akka.tcp://ClusterSystem@localhost:53324/temp/$d]
...Actor[akka.tcp://ClusterSystem@localhost:53324/temp/$e]
我期望 .../user/localA,但我得到 /temp/$d
【问题讨论】:
标签: java akka akka-cluster