Java 如何指定参与者来处理来自特定端口的消息

Java 如何指定参与者来处理来自特定端口的消息,java,akka,Java,Akka,现在,我试着在一个非常简单的用例中使用Akka。我想要一个特定的Actor来处理通过TCP获得的消息,但我无法实现这一点,而且文档似乎不够清晰。以下是我现在拥有的: Application.conf: akka { actor { provider = "akka.remote.RemoteActorRefProvider" } remote { enabled-transports = ["akka.remote.netty.tcp"] netty.tcp

现在,我试着在一个非常简单的用例中使用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
    }
 }
}
Main

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完全陌生,所以我想我错过了一些非常简单的东西。非常感谢您的帮助。

对于远程处理,您需要运行两个独立的actor系统,服务器使用上面指定的配置,客户端使用下面的配置

这将告诉Akka示例参与者是在远程系统上实例化的,并将相应地路由发送的消息

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

谢谢我对Akka的整个远程处理机制的理解是非常错误的,但现在我成功地按照我真正想要的方式工作了(您的评论帮助我理解了我想要的)。您可能还对Akka IO()感兴趣,它对于使用TCP或UDP的通用客户机服务器设置非常有用。Akka Remoting设计用于ActorSystems之间的P2P通信,但不用于与外部系统的集成。