Reputation: 24413
In my application I have to send subscription messages to actors, which may or may not be routers. If they are routers, the messages have to be send to all routees. Broadcast
is not applicable here, because the actors may not be routers and I don't want to handle Broadcast
messages inside of the actors receive block. I also don't want to create a custom router, because the router can be of any type. So what I would like to do is something like this:
system.actorOf(Props[MyActor].withRouter(FromConfig().withRoute { routeeProvider => {
case (sender, Subscribe) => routeeProvider.routees.map(Destination(sender, _))
}}))
Is there some way to do this, or any other way to add special routing behaviour like this?
Upvotes: 0
Views: 588
Reputation: 26579
Just create a normal actor, have routees/actors dial-in and/or created as children and forward inbound messages as you like.
Upvotes: 1
Reputation: 10401
You can use a BroadcastRouter for your routers. Unlike other routers which only broadcast if the message is a broadcast message this router will broadcast every message to its children.
Upvotes: 0
Reputation: 988
You can define a parent abstract class for all your actors where you can handle all special messages needed and inherit all your non-router actors from it. Something like this:
abstract class MyActor extends Actor {
protected def receiveMsg: Receive // will substitute "receive" in your actors
def receive = receiveMsg orElse {
case Broadcast(msg) => self ! msg
...
}
}
Upvotes: 2