简体   繁体   English

将消息发送到池中的特定路由

[英]Sending a message to a specific routee in a pool

Say I have a pool of Routee actors and each of these actors has its own 'subordinate' actor it needs to communicate with. 假设我有一群Routee演员,并且每个演员都有自己的“下属”演员,需要与之进行交流。 When a the subordinate sends a message back to the 'parent' routee it seems that this message is also being passed through router so there is no guarantee that it will make it to the appropriate router. 当下属将一条消息发送回“父”路由时,似乎该消息也正在通过路由器传递,因此无法保证会将该消息传递到适当的路由器。 So take the code below: 因此,请使用以下代码:

class MyActor extends Actor {

    val router = context.actorOf(FromConfig.props(Props(new Routee)), "myrouter")

    def receive = {

        case msg: SomeMsg => router ! msg
    } 

}

class Routee extends Actor {

    val sub = context.actorOf(Props(new Subordinate(this)))
    var waiting = false

    def receive = {

        case msg: SomeMsg => 
            if (! waiting) {
                waiting = true
                sub ! msg
            }

        case ack: SomeAck =>
            waiting = false
            if (ack.routee != this) println("From a different subordinate")
    }
}

class Subordinate(routee: Routee) extends Actor {

    def receive = {

        case msg: SomeMsg =>
            sender ! SomeAck(routee)
    }

}

So if I run the code below this will cause the "From a different subordinate" message to printed: 因此,如果我运行下面的代码,这将导致打印“来自其他下属”消息:

    val actor = ActorSystem("test").actorOf(Props(new MyActor), "myactor")
    while (true) actor ! SomeMsg()

There is no guarantee that I the acknowledgement is sent back to the appropriate routee. 无法保证我将确认发送回适当的路由。 Is it the case that the acknowledgement is being passed through the router and if so is there a way around this? 确认是通过路由器传递的,是否可以解决?

Configuration: 组态:

akka {
    actor {
        provider = "akka.remote.RemoteActorRefProvider"

        deployment {
            /myactor/myrouter {
                router = balancing-pool
                nr-of-instances = 4
            }
        }
    }
    remote {
        enabled-transports = ["akka.remote.netty.tcp"]
        netty.tcp {
            hostname = "0.0.0.0"
            port = 2551
        }
    }
}

You are using a balancing-pool , which means that the routees will use a shared mailbox and "steal" each other's messages. 您正在使用balancing-pool ,这意味着路由将使用共享邮箱并“窃取”彼此的消息。 Try with round-robin-pool instead. 尝试使用round-robin-pool

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM