如何指定Actor来处理来自特定端口的消息

时间:2014-07-22 11:45:45

标签: java akka

现在我尝试使用Akka非常简单,就像看起来一样,用例。我想要一个特定的Actor来处理我通过TCP获得的消息,但我无法实现这一点,而且文档似乎不够清晰。这就是我现在所拥有的:

Application.conf:

akka {
  actor {
    provider = "akka.remote.RemoteActorRefProvider"
  }
  remote {
    enabled-transports = ["akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "127.0.0.1"
      port = 2552
    }
 }
}

主要

public class Main {
    public static void main(String[] args) {
        ActorSystem system = ActorSystem.create("Server");
        ActorRef a = system.actorOf(Props.create(SampleActor.class), "Messages");
        a.tell("testmessage", a);
    }
}

SampleActor

public class SampleActor extends UntypedActor {

    private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);

    @Override
    public void onReceive(Object msg) {
        log.info("Sample actor: " + msg.toString());
    }
}

但我不知道如何让SampleActor处理我在127.0.0.1:2552收到的消息。 这是我运行此应用程序时得到的输出,并在浏览器中访问127.0.0.1:2552一次:

[INFO] [07/22/2014 15:39:50.953] [main] [Remoting] Starting remoting
[INFO] [07/22/2014 15:39:51.086] [main] [Remoting] Remoting started; listening on addresses :[akka.tcp://Server@127.0.0.1:2552]
[INFO] [07/22/2014 15:39:51.087] [main] [Remoting] Remoting now listens on addresses: [akka.tcp://Server@127.0.0.1:2552]
[INFO] [07/22/2014 15:39:51.092] [Server-akka.actor.default-dispatcher-3] [akka://Server/user/Messages] Sample actor: testmessage
[INFO] [07/22/2014 15:39:59.796] [Server-akka.actor.default-dispatcher-3] [akka://Server/system/transports/akkaprotocolmanager.tcp0/akkaProtocol-tcp%3A%2F%2FServer%40127.0.0.1%3A6993-2] Message [akka.remote.transport.AssociationHandle$Disassociated] from Actor[akka://Server/deadLetters] to Actor[akka://Server/system/transports/akkaprotocolmanager.tcp0/akkaProtocol-tcp%3A%2F%2FServer%40127.0.0.1%3A6993-2#455049767] 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'.
[INFO] [07/22/2014 15:39:59.797] [Server-akka.actor.default-dispatcher-3] [akka://Server/system/transports/akkaprotocolmanager.tcp0/akkaProtocol-tcp%3A%2F%2FServer%40127.0.0.1%3A6994-1] Message [akka.remote.transport.AssociationHandle$Disassociated] from Actor[akka://Server/deadLetters] to Actor[akka://Server/system/transports/akkaprotocolmanager.tcp0/akkaProtocol-tcp%3A%2F%2FServer%40127.0.0.1%3A6994-1#548527175] was not delivered. [2] 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'.
[INFO] [07/22/2014 15:39:59.800] [Server-akka.actor.default-dispatcher-4] [akka://Server/system/transports/akkaprotocolmanager.tcp0/akkaProtocol-tcp%3A%2F%2FServer%40127.0.0.1%3A6995-3] Message [akka.remote.transport.AssociationHandle$Disassociated] from Actor[akka://Server/deadLetters] to Actor[akka://Server/system/transports/akkaprotocolmanager.tcp0/akkaProtocol-tcp%3A%2F%2FServer%40127.0.0.1%3A6995-3#-105273249] was not delivered. [3] 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'.
[INFO] [07/22/2014 15:39:59.933] [Server-akka.actor.default-dispatcher-4] [akka://Server/system/transports/akkaprotocolmanager.tcp0/akkaProtocol-tcp%3A%2F%2FServer%40127.0.0.1%3A6996-4] Message [akka.remote.transport.AssociationHandle$Disassociated] from Actor[akka://Server/deadLetters] to Actor[akka://Server/system/transports/akkaprotocolmanager.tcp0/akkaProtocol-tcp%3A%2F%2FServer%40127.0.0.1%3A6996-4#2016773005] was not delivered. [4] 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'.
[INFO] [07/22/2014 15:39:59.934] [Server-akka.actor.default-dispatcher-4] [akka://Server/system/transports/akkaprotocolmanager.tcp0/akkaProtocol-tcp%3A%2F%2FServer%40127.0.0.1%3A6997-5] Message [akka.remote.transport.AssociationHandle$Disassociated] from Actor[akka://Server/deadLetters] to Actor[akka://Server/system/transports/akkaprotocolmanager.tcp0/akkaProtocol-tcp%3A%2F%2FServer%40127.0.0.1%3A6997-5#-1053639148] was not delivered. [5] 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'.
[INFO] [07/22/2014 15:39:59.936] [Server-akka.actor.default-dispatcher-3] [akka://Server/system/transports/akkaprotocolmanager.tcp0/akkaProtocol-tcp%3A%2F%2FServer%40127.0.0.1%3A6998-6] Message [akka.remote.transport.AssociationHandle$Disassociated] from Actor[akka://Server/deadLetters] to Actor[akka://Server/system/transports/akkaprotocolmanager.tcp0/akkaProtocol-tcp%3A%2F%2FServer%40127.0.0.1%3A6998-6#-1709546418] was not delivered. [6] 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'.

我对Akka来说绝对是新手,所以我想我错过了一些非常简单的事情。我将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:2)

对于远程处理,您需要运行2个独立的actor系统,具有您在上面指定的配置的服务器和具有以下配置的客户端

这将告诉Akka示例actor在远程系统实例化并将相应的路由发送

akka {
  log-dead-letters-during-shutdown = off
  actor {
    provider = "akka.remote.RemoteActorRefProvider"
    deployment {
      /Messages {
        remote = "akka.tcp://Server@127.0.0.1:2553"
      }
    }
  } 
}