In event-driven distributed architectures, real-time events require notification sharing among different applications as soon as they take place. For such systems, NCache’s Pub/Sub messaging patterns enable Publishers to share events with Subscribers as soon as an event of interest occurs, so that any further processing required takes place without any delay.
With Pub/Sub messaging, we can share messages between applications to boost communication and also offload time-consuming operations to background processors.
This blog covers how the NCache Pub/Sub Messaging model works in Scala applications to run smoothly without any outdated information.
Introducing Pub/Sub messaging
Pub/Sub is a messaging paradigm where senders (or Publishers) share messages with multiple receivers (or Subscribers) through a channel, avoiding coupling between senders and receivers.

Figure: Working of Pub/Sub Messaging in NCache
A Publisher application communicates with a subscriber application, using Topics. Since any Pub/Sub model needs a communication channel, NCache serves as a conduit for Topics, allowing the Publisher to publish their message to the Topic. And the Subscribers receive the message via the Topic as a notification.
Pub/Sub Messaging Components
The basic components of Pub/Sub messaging in NCache are a Topic, Publisher, and Subscriber.
- A Topic is a place where messages are published. The Publisher publishes messages on the Topic. Subscribers subscribe to the Topic to receive the message.
- A Publisher can publish messages to a Topic by specifying a Topic name.
- A subscriber application can subscribe to Topic messages by registering itself to a Topic of interest through a subscription.
How to Use NCache Pub/Sub Messaging in Scala
To enable this real-time information sharing in Scala applications, NCache acts as a messaging bus, helping Scala applications communicate better with its distributed architecture and extensive feature set.
Let’s discuss the Pub/Sub messaging in detail, with the help of a few code examples.
1. Create a Topic
The createTopic method creates a Topic in the cache with a specified name. If the Topic already exists, an instance of the Topic is returned as Topic. Whenever a message is published on a Topic, it is delivered based on message preference to Subscribers that are registered on that Topic. The following example creates a Topic called orderTopic
.
1 2 3 4 5 6 |
// Mention the name of the Topic val TopicName = "orderTopic" // Create the Topic val Topic = cache.getMessagingService.createTopic(TopicName) |
2. Publish messages to a Topic
The Topic interface facilitates publishing messages against the Topic. This also provides event registrations for message delivery failure, receiving messages, and deleting Topics.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// Get Topic val orderTopic = cache.getMessagingService.getTopic(TopicName) if (TopicName != null) { // Create an object to be sent in the message val order = fetchOrderFromDB(1100) // Create a new message val orderMessage = Message(order) val expiryTime = new TimeSpan(12, 12, 12) // Set the expiration time of the message orderMessage.setExpirationTime(expiryTime) // Register message delivery failure val TopicListener = TopicEventListener() orderTopic.addMessageDeliveryFailureListener(TopicListener) orderTopic.addTopicDeletedListener(TopicListener) // Publish the order with the delivery option set as All // and register message delivery failure orderTopic.publish(orderMessage, DeliveryOption.All, true) } else { // No Topic exists } |
3. Subscribe to a Topic
A subscriber registers itself to a topic of interest through a subscription. In NCache, we have multiple methods of creating a subscription such as non-durable, durable, multiple, and asynchronous subscriptions. For non-durable subscriptions, the Topic interface facilitates publishing messages against a Topic. Moreover, it allows the subscriber to register a callback to receive a notification as soon as a message is published.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// “orderTopic” is the name of the topic created beforehand string topicName = "orderTopic"; // Get the topic ITopic topic = cache.MessagingService.GetTopic(topicName); // If topic exists, Create subscription if (topic != null) { // Create and register subscribers for orderTopic // MessageReceived callback is specified ITopicSubscription subscription= topic.CreateSubscription(MessageReceived); } |
Similarly, the IDurableTopicSubscription
interface facilitates creating durable subscription and publishing of messages against the topic. In durable subscription, subscriber can create shared and exclusive durable subscriptions.
Besides this, NCache also provides patterns to subscribe to multiple topics in a single call existing on the server with its Multiple Subscriptions method. To understand more about the subscription policies in NCache, refer to the NCache Documentation.
Conclusion
If you are looking for a reliable and highly available message delivery mechanism, NCache Pub/Sub Messaging is just the thing for you. Not to mention, the various publishing methods and subscriptions it offers. So, take your application to the next level and download NCache today!