How to create an observer, like communication between actors with Akka

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

What is the equivalent model in Akka?

Use Case:

  • Actor (PropertyServiceActor) - these are properties for reading and caching from the database
  • Different members can register with PropertyServiceActor
  • If the property changes, the PropertyServiceActor notifies registered members of the change
+5
source share
1 answer

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, simply send a message to observers ActorRef. You can also add and remove observers by sending akka.routing.AddRoutee and akka.routing.RemoveRoutee .

You can find more routing docs.

+5
source

Source: https://habr.com/ru/post/1215735/


All Articles