drexin
drexin

Reputation: 24413

Add routes/special messages to akka router

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

Answers (3)

Viktor Klang
Viktor Klang

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

Marius Danila
Marius Danila

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

Sergiy Prydatchenko
Sergiy Prydatchenko

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

Related Questions