嘗試將Akka用於並行計算,但在通信actor時面臨問題。我有兩個不同的角色(他們的名字是發送者和接收者),它們工作在兩個不同的系統上,在同一個IP地址(名字是SenderSystem和Receiver System)的不同端口上工作。我想做的是從發件人演員發送消息到接收者演員。但是,在控制檯上我看到這樣Akka Remote - 郵件未傳遞(Java)
[INFO] [08/15/2015 12:36:51.645] [SenderSystem-akka.actor.default-dispatcher-4] [akka://SenderSystem/sender] Message [com.aliyesilkanat.akkadeneme.Messages$1] from Actor[akka://SenderSystem/deadLetters] to Actor[akka://SenderSystem/sender] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
這裏的消息是application.conf
akka {
loglevel = "INFO"
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
untrusted-mode = off
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
hostname = "127.0.0.1"
}
}
}
sender.conf
include "application"
akka {
actor {
deployment {
/receiver {
remote = "akka://[email protected]:8091"
}
}
}
remote.netty.tcp.port = 8090
}
receiver.conf
include "application"
akka {
remote.netty.tcp.port = 8091
}
Receiver.java
ReceiverApplication.java
package com.aliyesilkanat.akkadeneme.receiver;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import com.typesafe.config.ConfigFactory;
public class ReceiverApplication {
public static void main(String[] args) {
startRecieverSystem();
}
private static void startRecieverSystem() {
final ActorSystem system = ActorSystem.create("ReceiverSystem",
ConfigFactory.load("receiver"));
ActorRef actorOf = system.actorOf(Props.create(Receiver.class),
"receiverActor");
System.out.println("created receiver actor: " + actorOf.toString());
}
}
Sender.java
package com.aliyesilkanat.akkadeneme.sender;
import akka.actor.ActorSelection;
import akka.actor.UntypedActor;
import com.aliyesilkanat.akkadeneme.Messages;
public class Sender extends UntypedActor {
public Sender() {
}
@Override
public void onReceive(Object msg) throws Exception {
if (msg.equals(Messages.SEND)) {
System.out.println(getSender() + " sends");
ActorSelection receiverActor = getContext().actorSelection(
"akka.tcp://[email protected]:8091/user/receiver"); // I am not sure about this one
receiverActor.tell(Messages.RECEIVE, getSelf());
}
}
}
SenderApplication.java
package com.aliyesilkanat.akkadeneme.sender;
import com.typesafe.config.ConfigFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
public class SenderApplication {
private static ActorSystem system;
public static void main(String[] args) {
startSenderApp();
}
private static void startSenderApp() {
setSystem(ActorSystem.create("SenderSystem",
ConfigFactory.load("sender")));
ActorRef actorOf = getSystem().actorOf(Props.create(Sender.class),
"senderActor");
System.out.println("created sender actor: " + actorOf.toString());
}
public static ActorSystem getSystem() {
return system;
}
public static void setSystem(ActorSystem system) {
SenderApplication.system = system;
}
}
最後,主方法
public static void main(String[] args) {
ReceiverApplication.main(null);
SenderApplication.main(null);
ActorSystem system = SenderApplication.getSystem();
ActorSelection ref = system.actorSelection("sender");
ref.tell(Messages.SEND, ActorRef.noSender());
}
整個控制檯輸出
[INFO] [08/15/2015 12:48:12.220] [main] [Remoting] Starting remoting
[INFO] [08/15/2015 12:48:12.451] [main] [Remoting] Remoting started; listening on addresses :[akka.tcp://[email protected]:8091]
[INFO] [08/15/2015 12:48:12.451] [main] [Remoting] Remoting now listens on addresses: [akka.tcp://[email protected]:8091]
created receiver actor: Actor[akka://ReceiverSystem/user/receiverActor#2084584126]
[INFO] [08/15/2015 12:48:12.481] [main] [Remoting] Starting remoting
[INFO] [08/15/2015 12:48:12.491] [main] [Remoting] Remoting started; listening on addresses :[akka.tcp://[email protected]:8090]
[INFO] [08/15/2015 12:48:12.491] [main] [Remoting] Remoting now listens on addresses: [akka.tcp://[email protected]:8090]
created sender actor: Actor[akka://SenderSystem/user/senderActor#-2]
[INFO] [08/15/2015 12:48:12.491] [SenderSystem-akka.actor.default-dispatcher-3] [akka://SenderSystem/sender] Message [com.aliyesilkanat.akkadeneme.Messages$1] from Actor[akka://SenderSystem/deadLetters] to Actor[akka://SenderSystem/sender] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
THX我remotly創建接收演員很多!但我也誤認了演員的地址。它應該是這樣的。 akka.tcp://[email protected]:8091/user/receiverActor –
'receiverActor'是遠程的。 –