我是Akka的新手,對Scala根本不熟悉。我正嘗試使用Akka + Java在多臺機器上運行項目。我能夠在本地成功運行Akka Java示例,但是當我嘗試在兩臺機器上運行它們中的任何一個時,代碼停止工作。我也查看了Akka-Sample-Remote源代碼。它也適用於單臺機器,但在多臺機器上使用時會中斷。我認爲問題在於我設置配置的方式。我已經將這個問題歸納爲一個簡單的HelloWorld問題,如下所示。Akka使用Java進行遠程連接
將代碼分成兩個項目,並帶有兩個單獨的配置文件:Hello Actor項目和World Actor項目。 World Actor等待接收來自Hello Actor的Hello消息,然後打印出「Hello World」。下面您可以看到這兩個項目的代碼和配置。正如你所看到的,世界演員是在1719端口啓動的,而你好演員在1720端口啓動,並嘗試使用「akka.tcp://[email protected]:1719/user/WorldActor」連接到世界演員。想知道他的代碼/配置有什麼問題嗎?
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;}
}
Application.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://[email protected]: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);
}
}
}
APPLI cation.conf:
HelloConfig {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
netty.tcp {
hostname="192.27.336.187"
port=1720
}
}
}
這已在akka郵件列表中得到解答:https://groups.google.com/d/msg/akka-user/1tLBTZdo8tA/bhCqkilFlxcJ – 2013-03-20 09:29:27