簡體   English   中英

將消息發送到池中的特定路由

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

假設我有一群Routee演員,並且每個演員都有自己的“下屬”演員,需要與之進行交流。 當下屬將一條消息發送回“父”路由時,似乎該消息也正在通過路由器傳遞,因此無法保證會將該消息傳遞到適當的路由器。 因此,請使用以下代碼:

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)
    }

}

因此,如果我運行下面的代碼,這將導致打印“來自其他下屬”消息:

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

無法保證我將確認發送回適當的路由。 確認是通過路由器傳遞的,是否可以解決?

組態:

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
        }
    }
}

您正在使用balancing-pool ,這意味着路由將使用共享郵箱並“竊取”彼此的消息。 嘗試使用round-robin-pool

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM