我是 Akka 的新手,对 Scala 一点也不熟悉。我正在尝试使用 Akka+Java 在多台机器上运行项目。我能够在本地成功运行 Akka Java 示例,但是当我尝试在两台机器上运行其中任何一个时,代码停止工作。我也查看了 Akka-Sample-Remote 源代码。它也适用于单台机器,但在多台机器上使用时会中断。我认为问题在于我设置配置的方式。我将问题总结为一个简单的 HelloWorld 问题,如下所示。
代码分为两个项目,有两个独立的配置文件:一个 Hello Actor 项目和一个 World Actor 项目。World Actor 等待从 Hello Actor 接收到 Hello Message,然后打印出“Hello World”。下面你可以看到这两个项目的代码和配置。如您所见,World Actor 在端口 1719 上启动,Hello Actor 在端口 1720 上启动并尝试使用 "akka.tcp://WorldApplication@192.27.336.187:1719/user/WorldActor" 连接到 World Actor。知道他的代码/配置有什么问题吗?
JWorld.java:
public class JWorld {
public static void main(String[] args) {
JWorldApplication app = new JWorldApplication();
System.out.println("Started World Application - waiting for Hello message");
}
}
JWorldApplication.java:
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import com.typesafe.config.ConfigFactory;
public class JWorldApplication {
private ActorSystem system;
public JWorldApplication() {
system = ActorSystem.create("WorldApplication", ConfigFactory.load()
.getConfig("WorldConfig"));
ActorRef actor = system.actorOf(new Props(JWorldActor.class),
"WorldActor");
}
}
JWolrdActor.java:
import akka.actor.UntypedActor;
public class JWorldActor extends UntypedActor {
@Override
public void onReceive(Object message) {
if (message instanceof HelloMessage) {
HelloMessage recMsg = (HelloMessage) message;
System.out.println("Received Message: " + recMsg.getText());
System.out.println("***** Hello World! ******" );
} else {
System.out.println("UnHandled Message Received" );
unhandled(message);
}
}
}
HelloMessage.java:
import akka.actor.ActorRef;
public class HelloMessage{
private ActorRef receiver;
private String text;
HelloMessage() {}
HelloMessage(ActorRef receiver){ this.receiver = receiver;}
public ActorRef getReceiver(){ return receiver;}
public void setText(String text) { this.text = text;}
public String getText() {return text;}
}
应用程序.conf:
WorldConfig {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
netty.tcp {
hostname="192.27.336.187"
port=1719
}
}
}
JHello.java:
public class JHello {
public static void main(String[] args) {
JHelloApplication testApp = new JHelloApplication();
System.out.println("Started Hello Application - Sending Hello Message");
testApp.sayHello();
}
}
JHelloApplication.java:
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import com.typesafe.config.ConfigFactory;
public class JHelloApplication {
private ActorSystem system;
private ActorRef remoteActor, myActor;
public JHelloApplication() {
system = ActorSystem.create("HelloApplication", ConfigFactory.load()
.getConfig("HelloConfig"));
myActor = system.actorOf(new Props(JHelloActor.class),"HelloActor");
remoteActor = system
.actorFor("akka.tcp://WorldApplication@192.27.336.187:1719/user/WorldActor");
}
public void sayHello() {
myActor.tell(new HelloMessage(remoteActor));
}
}
JHelloActor.java:
import akka.actor.ActorRef;
import akka.actor.UntypedActor;
public class JHelloActor extends UntypedActor {
@Override
public void onReceive(Object message) {
if (message instanceof HelloMessage) {
HelloMessage msg = (HelloMessage) message;
if (msg.getReceiver() !=null){
msg.setText("Hello");
msg.getReceiver().tell(msg, getSelf());
}
} else {
System.out.println("UnHandled Message Received" );
unhandled(message);
}
}
}
应用程序.conf:
HelloConfig {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
netty.tcp {
hostname="192.27.336.187"
port=1720
}
}
}