如何在 Akka Java 中使用 Patterns.askWithReplyTo
How to use Patterns.askWithReplyTo in Akka Java
我正在寻找如何使用 Akka 模式 Patterns.askWithReplyTo
和 Java 的示例。
示例项目可在 Github 获得:https://github.com/pcdhan/akka-patterns.git
我的挑战是我无法在有效负载中包含发件人的 ActorRef。
本地演员
ActorRef localA= system.actorOf(LocalActor.props(), "localA");
远程演员
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[akka.tcp://ClusterSystem@localhost:53324/temp/$d]
...Actor[akka.tcp://ClusterSystem@localhost:53324/temp/$e]
我希望.../user/localA,但我得到 /temp/$d
askWithReplyTo
并不是要将发送者 self
传递到消息中。
askWithReplyTo
期望你给它一个工厂函数,它被提供给临时响应参与者,所以如果你有一条消息,你可以像这样构造:
new MyMessage(ActorRef replyTo)
您可以像这样将它与 askWithReplyTo
一起使用:
final Future<Object> f = Patterns.askWithReplyTo(
otherActor,
replyTo -> new MyMessage(replyTo),
timeout);
第二个参数是一个 lambda,它将与临时的 ask-actor 一起调用(它总是在您确实 ask 处理响应超时时创建),以便您可以将其包含在消息中。
该模式仅在接收方使用 replyTo
字段进行响应而不是 sender()
时才有用,后者通常是您响应的方式。
我正在寻找如何使用 Akka 模式 Patterns.askWithReplyTo
和 Java 的示例。
示例项目可在 Github 获得:https://github.com/pcdhan/akka-patterns.git
我的挑战是我无法在有效负载中包含发件人的 ActorRef。
本地演员
ActorRef localA= system.actorOf(LocalActor.props(), "localA");
远程演员
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[akka.tcp://ClusterSystem@localhost:53324/temp/$d]
...Actor[akka.tcp://ClusterSystem@localhost:53324/temp/$e]
我希望.../user/localA,但我得到 /temp/$d
askWithReplyTo
并不是要将发送者 self
传递到消息中。
askWithReplyTo
期望你给它一个工厂函数,它被提供给临时响应参与者,所以如果你有一条消息,你可以像这样构造:
new MyMessage(ActorRef replyTo)
您可以像这样将它与 askWithReplyTo
一起使用:
final Future<Object> f = Patterns.askWithReplyTo(
otherActor,
replyTo -> new MyMessage(replyTo),
timeout);
第二个参数是一个 lambda,它将与临时的 ask-actor 一起调用(它总是在您确实 ask 处理响应超时时创建),以便您可以将其包含在消息中。
该模式仅在接收方使用 replyTo
字段进行响应而不是 sender()
时才有用,后者通常是您响应的方式。