-
Notifications
You must be signed in to change notification settings - Fork 19
Topic (Pub\Sub)
Nils Kilden-Pedersen edited this page May 10, 2016
·
1 revision
ITopic
is a very simple interface for Pub/Sub multicast pattern.
To listen to a topic, register a callback function by calling onMessage
:
val topic = hz.getTopic[String]("topic")
val registration = topic.onMessage() { message =>
println(s"Received: $message")
}
registration.cancel()
The onMessage
method returns a ListenerRegistration
object, which has a single method, cancel()
.
A reliable topic uses a RingBuffer
internally to ensure more reliable operation, and allows subscribers to ask for previously published messages, so long as they are not expired.
val rTopic = hz.getReliableTopic[String]("reliableTopic")
// Subscribe with replay from sequence 5:
val registration = rTopic.onSeqMessage(startFrom = 5) {
case (seq, message) => println(s"Received $seq: $message")
}
// Or simply subscribe to new only, with sequence number:
val registration = rTopic.onSeqMessage() {
case (seq, message) => println(s"Received $seq: $message")
}
// Or without sequence number, like regular topic:
val registration = rTopic.onMessage() { message =>
println(s"Received: $message")
}
Any exception thrown inside the callback function is considered terminal and will cancel the registration. Employ a try
/catch
pattern to exempt as needed.