简体   繁体   中英

Is it possible to know which topic a message was received on in Akka's “Distributed Publish Subscribe in Cluster”?

I have an Akka system with Actors representing sensors. Every time a sensor makes an observation, its corresponding Actor publishes this observation on an internal pub/sub topic. This makes it easy to extend usages of the observation (saved in a DB by one Actor, published to an MQTT broker by another, etc.)

A possible solution to make the receiver aware of which topic it received the message on, is to include the topic in the message. However, this seems like bad design to me. Is it possible to see which topic the current message was received on in an Actor in Akka?

Akka's documentation on Distributed Publish Subscribe in Cluster ( http://doc.akka.io/docs/akka/current/java/distributed-pub-sub.html ) does not mention this as far as I can see.

As far as I can tell, this is not built in as a feature of Distributed Pub/Sub. I typically would solve this by making some sort of envelope:

case class PubSubEnvelope[MessageType](topic: String, payload: MessageType)

For extra safety, perhaps you'd want to make some sort of supertype for MessageType so that you could constrain it, and some enum-like structure for topic instead of String .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM