简体   繁体   中英

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. 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. Try with round-robin-pool instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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