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

时间:2014-10-24 13:48:27

标签: scala akka

假设我有一个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
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您正在使用balancing-pool,这意味着路由将使用共享邮箱并且"窃取"彼此的消息。请尝试使用round-robin-pool