Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto create a observer like communication between actors with Akka

In classical programming, I use the obeserver pattern in case I want to notify observers about changes.

What is the equivalent pattern in Akka?

Use case:

  • An actor (PropertyServiceActor) is reading and caching properties form the DB
  • Different actors can register to the PropertyServiceActor
  • If a property changes, the PropertyServiceActor notifies the registered actors about the change
like image 646
Simon Avatar asked Mar 20 '15 09:03

Simon


1 Answers

Take a look at BroadcastGroup

//Create group
val paths = List("/user/workers/w1", "/user/workers/w2", "/user/workers/w3")
val observers: ActorRef =  context.actorOf(BroadcastGroup(paths).props(), "observers")

To notify all observers just send message to observers ActorRef. Also you can add and remove observers by sending akka.routing.AddRoutee and akka.routing.RemoveRoutee.

You can find more routing docs.

like image 141
Stan Kurilin Avatar answered Sep 28 '22 04:09

Stan Kurilin