Alachisoft.com

Use NCache Messaging for Runtime Data Sharing

Many applications today need to share data with each other at runtime asynchronously.  And, relational databases do not provide an efficient event notification mechanism and their performance and scalability is a major concern.

Message queues provide sequenced messages and guarantee message delivery but they also have performance and scalability issues.

The best way is to use a message bus style messaging platform that is fast and scalable and also provides producer/consumer style data sharing coordination. NCache is an extremely fast and scalable in-memory distributed cache and also provides a simple and powerful message bus style messaging capability.

With NCache messaging, you can do the following:

  1. Cache level events: NCache can notify clients on add, insert, and remove of any item in the cache or if the cache is cleared, if the client has registered interest in receiving such events.
  2. Item level events: NCache can notify clients on a specific item update and remove if the client has registered interest in it.
  3. Continuous Query events: NCache can monitor a dataset in the cache based on SQL-like criteria and then notify clients for add, update, and remove of items from this data set to clients who have registered interest in it.
  4. Application initiated custom events: Applications can fire custom events into NCache message bus so other applications who have registered interest in them can be notified.


All the applications (whether .NET or Java) connected to NCache can use it as a messaging platform and propagate events through it to other applications. Or, they can ask the cache to notify them when certain things happen in the cache.

Cache Level Events

Cache level events are fired whenever anything is added, updated, or removed or if the cache is cleared from the cache. By default, these events are turned off because they can get quite chatty. But, you can enable some or all of them in the cache configuration and then have your application supply callback functions to NCache so NCache calls these callbacks when these events occur. Below are the type of callbacks you can register.


CacheEventListener listener = new CacheEventListener();
EnumSet _eventType = EnumSet.noneOf(EventType.class);

_eventType.add(EventType.ItemAdded);
_eventType.add(EventType.ItemUpdated);
_eventType.add(EventType.ItemRemoved);

CacheEventDescriptor descripter = _cache.addCacheDataModificationListener(listener,
                                  _eventType, EventDataFilter.None);

Item Level Events

These events are fired on specific item updates or removal but only if one or more clients have registered callbacks for them. Below are the events and callbacks you can register.


CacheEventListener listener = new CacheEventListener();
EnumSet _eventType = EnumSet.noneOf(EventType.class);

_eventType.add(EventType.ItemUpdated);
_eventType.add(EventType.ItemRemoved);

_cache.addCacheDataModificationListener ("Key:1", listener,
                                         _eventType, EventDataFilter.None);

Continuous Query

Continuous Query is a powerful feature of NCache that allows you to register an SQL-like query with the cache and also specify for which events would you like your callbacks to be called by NCache.

NCache then creates a Continuous Query in the cache cluster and monitors all add, update, and remove operations to see if they modify cached items matching this Continuous Query criteria. If so, then NCache notifies your client application about it.

Below are the events you can register.


ContinuousQuery cQuery1 = new ContinuousQuery(qry, indexTable);
ContinuousQueryListener cqListener = new ContinuousQueryListener();
EnumSet _eventType = EnumSet.noneOf(EventType.class);

_eventType.add(EventType.ItemAdded);
_eventType.add(EventType.ItemUpdated);
_eventType.add(EventType.ItemRemoved);

cQuery1.addDataModificationListener(cqListener, _eventType,
                                    EventDataFilter.DataWithMetaData);

Application Initiated Custom Events

NCache allows your applications to coordinate with other in a producer/consumer or publish/subscribe scenario. One application produces data and then fires a custom event into NCache messaging and one or more applications are waiting to receive this event.

Here is how your application registers to receive these custom events.


public void OnApplicationEvent(object notifId, object data)
{
  ...
} 
_cache.CustomEvent += new CustomEventCallback(this.OnApplicationEvent);

And, here is how your application fires this custom event into NCache messaging.


_cache.RaiseCustomEvent("NotificationID", DateTime.Now);

What to Do Next?