あなたが特定の俳優のすべての子供の俳優にブロードキャストを送信したい場合、BalancingPoolルータはあなたが望むものではなく、RoundRobinルータでもありません。
this linkの "Pools vs Groups"セクションをお読みください。それが.NETのドキュメントであるという事実は無視してください。その内容はプラットフォームに依存しません
ルーティングプールを使用する場合、ルーティングプールによって作成されるアクターへの参照は保持されません。したがって、俳優のパス名を把握するためにいくつかの魔法を使いたい場合を除き、そのルーティングプールのルーティングロジックを使用してメッセージを送信することしかできません。
あなたが望むのは、自分でアクターを作成し、ルーティンググループを作成するときにそれらをルートとして供給することです。次に、ルータとルータの両方を介してそれらのアドレスに対処します。
あなたはそれらをすべてのメッセージを送信する場合は、することができますだけmyActors foreach (_ ! "message")
、そしてあなたは、ルータを経由して行きたい場合は、することができますrouter ! "message"
私は「BalancingPool」ルータ基当量がありません怖いです。私は、ラウンドロビンルーティングロジックを使用して、あなたに完全な例をあげる:
import akka.actor.{Actor, ActorSystem, Props}
import akka.routing.{ActorRefRoutee, RoundRobinRoutingLogic, Router}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
class ParentActor extends Actor {
val actors = (1 to 5).map(i => context.system.actorOf(Props[ChildActor], "child" + i))
val routees = actors map ActorRefRoutee
val router = Router(RoundRobinRoutingLogic(), routees)
router.route("hello", sender())
router.route("hello", sender())
router.route("hello", sender())
router.route("hello", sender())
router.route("hello", sender())
actors foreach (_ ! "broadcast")
def receive = { case _ => }
}
class ChildActor extends Actor {
def receive = {
case "hello" => println(s"${self.path.name} answers hey")
case "broadcast" => println(s"${self.path.name} received broadcast")
}
}
object Main extends App {
val system = ActorSystem("HelloSystem")
val parent = system.actorOf(Props(classOf[ParentActor]))
Future {
Thread.sleep(5000)
system.terminate()
}
}
出力するとsbt run
[info] Running Main
child2 answers hey
child5 answers hey
child1 answers hey
child4 answers hey
child1 received broadcast
child3 answers hey
child4 received broadcast
child3 received broadcast
child5 received broadcast
child2 received broadcast
[success] Total time: 8 s, completed 7/05/2016 6:28:18 PM
>
幸運アッカを学びます!
出典
2016-05-07 06:31:39
mlg
これは素晴らしいです!私は長い間これを行う方法について考えていました〜 –
私は申し訳ありませんが、私は答えを受け入れる場所を見つけようとしています........... –
見つけた〜ありがとう〜 –