如何从角色内部获取绝对远程角色URL? [英] How do I get the absolute remote actor url from inside the actor?
问题描述
我想做这样的事情。
演员A:
actorB ! "info"
def receive()={
case _ => {
println("Remote address is "+ _)
}
}
演员B :(远程部署)
Actor B : (this deployed remotely )
def receive()={
case "info" => {
sender tell self.path.address.toString
}
}
我希望它返回字符串akka://10.4.20.40:2555 / slave / user / slaverunner。但是我得到的只是akka:// slave。如何获得远程主机和端口? 。地址对象上的属性host,port和hostport不返回任何内容
I want it to return me the string akka://10.4.20.40:2555/slave/user/slaverunner . But what I get is just akka://slave. How do I get the remote host and port? . The properties host,port and hostport on the address object dont return anything
推荐答案
如果调用
sender.path.toString
在演员A中,您将获得发件人的地址。因此,只要您可以向其发送消息,就无需将该地址传递给另一个参与者系统。
in actor A you will get the address of the sender. So you don't need to pass the address to another actor system as long as you can send a message to it.
Akka不会为您提供本地系统中参与者的远程路径,这就是为什么 self.path.address.toString $演员B中的c $ c>无法正常工作。
Akka will not give you a remote path for an actor in the local system, which is why self.path.address.toString
in actor B won't work.
如果您确实想将主机和端口从B发送到A,则需要访问通过 ExtendedActorSystem
的 RemoteActorRefProvider
。官方的方法是通过扩展。例如:
If you really want to send the host and port from B to A then you'll need to get access to a RemoteActorRefProvider
via the ExtendedActorSystem
. The official way to do that is through an Extension. For example:
class MyExtensionImpl(system: ExtendedActorSystem) extends Extension {
def address = system.provider match {
case rarp: RemoteActorRefProvider => rarp.transport.address
case _ => system.provider.rootPath.address
}
}
object MyExtension extends ExtensionKey[MyExtensionImpl]
val address = MyExtension(system).address
这将为您提供与B进行远程通信所需的确切地址。
And that will give you the exact address that you need to communicate remotely with B.
(请注意,此代码适用于Akka2.0.x。在2.1.x中,您可以使用<$ c $来避免通过 RemoteActorRefProvider
c> system.provider.getDefaultAddress )
(note this code works with Akka 2.0.x. In 2.1.x you can avoid going through RemoteActorRefProvider
by using system.provider.getDefaultAddress
)
在Akka中,如果您正在使用构造演员地址的方式与一起使用actorFor
,那么您需要确保主机名完全匹配。
In Akka if you are using constructing actor addresses this way for use with actorFor
then you need to make sure the hostnames match exactly.
例如,如果ActorSystem认为主机为 foo.bar.com
,则它将忽略发送的消息由远程主机到 actorFor( akka:// slave @ foo:2555 / user / slaverunner)
For example if the ActorSystem thinks the host is foo.bar.com
, then it will ignore the messages sent by a remote host to actorFor("akka://slave@foo:2555/user/slaverunner")
这篇关于如何从角色内部获取绝对远程角色URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!