gpt4 book ai didi

java - 使用 Akka 远程系统玩 2.0.2

转载 作者:行者123 更新时间:2023-11-29 09:10:36 25 4
gpt4 key购买 nike

我正在使用 Akka 2 和 Play 2.0.2。我想将 Play Framewrok 连接到远程 Akka 系统。我已经完成了远程配置并且 Actor 的远程引用,但任何时候我尝试将 play framewrok 连接到 Akka 远程系统。我在下面显示代码片段时遇到错误。

来自 play 应用程序的代码。

public static Result index() throws InterruptedException { 

ActorSystem csystem = Akka.system();
ActorRef localNode = csystem.actorOf(new Props(LocalNode.class),"localNode");
localNode.tell("Hello");


Thread.sleep(5000);
csystem.shutdown();
return ok(index.render("I am OK"));
}
}

这是来自 Play Actor 的代码

public class LocalNode extends UntypedActor {

LoggingAdapter log = Logging.getLogger(getContext().system(), this);
Timeout timeout = new Timeout(Duration.create(5 , "seconds"));

static ActorRef masterActor;

public void preStart()
{

/* Get reference to Master Node*/
masterActor = getContext().
actorFor("akka://MasterNode@127.0.0.1:9002/user/masterNode/masterActor");
}

@Override
public void onReceive(Object message) throws Exception {
System.out.println(" Future called ");

Future<Object> future = Patterns.ask(masterActor , message, timeout);

String result = (String) Await.result(future, timeout.duration());

log.info("Message from Server", result.toString());
}
}

来自 Play 应用程序 conf 文件的代码

#confige the remote connection

localNode {
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
transport = "akka.remote.netty.NettyRemoteTransport"
netty {
hostname = "127.0.0.1"
port = 0
}
}
}
}

下面是来自远程Akka Master节点的代码

打包 Rubine_Cluster;

import com.typesafe.config.ConfigFactory;
public class MasterNode implements Bootable
{
final ActorSystem system;

public MasterNode() {
system = ActorSystem.create("MasterNode", ConfigFactory.load()
.getConfig("masterNode"));
ActorRef masterActor = system.actorOf(new Props(MasterActor.class),"masterActor");
System.out.println(" Master Node is called ");
}
public void startup() {

}
public void shutdown() {
system.shutdown();
}
}

下面是远程Akka子Actor的代码

public class MasterActor extends UntypedActor {

public MasterActor(){System.out.println(" the masteractor has been started ");}

@Override
public void onReceive(Object message) throws Exception {
System.out.print(" this is from me to you "+message.toString());

if (message instanceof String) {
// Get reference to the message sender and reply back
getSender().tell(message + " got something");
System.out.print(" this is from me to you "+message.toString());
}
}
}

下面是远程Akka系统conf文件的代码

masterNode {

akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
transport = "akka.remote.netty.NettyRemoteTransport"
netty {
hostname = "127.0.0.1"
port = 9002
}
}
}
}

下面我们有错误代码:

[ERROR] [10/01/2012 03:03:21.646] [application-akka.actor.default-dispatcher-2]
[akka://application/user/localNode] sending to terminated ref breaks promises
akka.pattern.AskTimeoutException: sending to terminated ref breaks promises
at akka.pattern.AskSupport$class.ask(AskSupport.scala:76)
at akka.pattern.package$.ask(package.scala:43)
at akka.pattern.Patterns$.ask(Patterns.scala:41)
at akka.pattern.Patterns.ask(Patterns.scala)
at controllers.LocalNode.onReceive(LocalNode.java:32)
at akka.actor.UntypedActor$$anonfun$receive$1.apply(UntypedActor.scala:1
54)
at akka.actor.UntypedActor$$anonfun$receive$1.apply(UntypedActor.scala:1
53)
at akka.actor.Actor$class.apply(Actor.scala:318)
at akka.actor.UntypedActor.apply(UntypedActor.scala:93)
at akka.actor.ActorCell.invoke(ActorCell.scala:626)
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:197)
at akka.dispatch.Mailbox.run(Mailbox.scala:179)
at akka.dispatch.ForkJoinExecutorConfigurator$MailboxExecutionTask.exec(
AbstractDispatcher.scala:516)
at akka.jsr166y.ForkJoinTask.doExec(ForkJoinTask.java:259)
at akka.jsr166y.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:975)
at akka.jsr166y.ForkJoinPool.runWorker(ForkJoinPool.java:1479)
at akka.jsr166y.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:104)

我正在做一个提交项目,有人可以帮助解决这个问题吗,欢迎提出任何建议。

最佳答案

  1. 您不应该关闭 csystem,因为它是 Play 的 ActorSystem。
  2. 你有:"actorFor("akka://MasterNode@127.0.0.1:9002/user/masterNode/masterActor");"但是您没有“masterActor”的父节点(“masterNode”)

关于java - 使用 Akka 远程系统玩 2.0.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12666903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com