Alachisoft.com

Contact Us

+1 (925) 236-3830 sales@alachisoft.com

support@alachisoft.com

Live Chat

Runtime Data Sharing with Events - TayzGrid

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. And, an in-memory data grid with strong event notifications features is the ideal way to achieve this goal. TayzGrid is an extremely fast and scalable in-memory data grid and also provides a simple and powerful message bus style messaging capability.

With TayzGrid messaging, you can do the following:

  1. Data grid level events: TayzGrid can notify clients on add, insert, and remove of any item in the data grid, if the client has registered interest in receiving such events.
  2. Item level events: TayzGrid can notify clients on a specific item update and remove if the client has registered interest in it.
  3. Continuous Query events: TayzGrid can monitor a dataset in the data grid 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 (topic based) events: Applications can fire custom events (topic based) into TayzGrid message bus so other applications that have registered interest in them can be notified.

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

Data Grid Level Events

Data grid level events are fired whenever anything is added, updated, or removed or if the data grid is cleared. By default, these events are turned off because they can get quite chatty. But, you can enable some or all of them in the data grid configuration and then have your application supply callback functions to TayzGrid so TayzGrid calls these callbacks when these events occur. Just implement CacheListener interface in your java application:


public class CacheListener implements CacheDataModificationListener
{
  @Override
  public void cacheDataModified(String key, CacheEventArg cea) {
    if (cea.getEventType() == EventType.ItemAdded)
    {
      // Your custom code for Item Add
    }
    else if (cea.getEventType() == EventType.ItemUpdated)
    {
      // Your custom code  for Item Update
    }
    else if (cea.getEventType() == EventType.ItemRemoved)
    {
      // Your custom code for Item Remove
    }
 }

  @Override
  public void cacheCleared() {
    // Your custom code for cache clear
  }
}

and use this class in your program as follows:


CacheEventListener cacheListener = new CacheEventListener();
EnumSet e = EnumSet.of( CacheNotificationType.ALL);
cache.registerCacheEventlistener(cacheListener, e);

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. To enable item level events implement CacheItemUpdatedCallback and CacheItemRemovedCallback interface in your java application as follows:


CacheItemUpdatedCallback:
import com.alachisoft.tayzgrid.web.caching.CacheItemUpdatedCallback;

public class CacheItemUpdatedCallBackImpl implements CacheItemUpdatedCallback 
{
    @Override
    public void itemUpdated(String string) {
        System.out.println(string);
    }    
}

CacheItemRemovedCallback:

import com.alachisoft.tayzgrid.web.caching.CacheItemRemovedCallback;
import com.alachisoft.tayzgrid.web.caching.CacheItemRemovedReason;

public class CacheItemRemovedCallBackImpl implements CacheItemRemovedCallback 
{
  @Override
  public void itemRemoved(String string, Object o, CacheItemRemovedReason cirr) {
    System.out.println(string);
    System.out.println("Removal reason: " + cirr.name());                
  }      
}

and apply them as follows:


cache.registerKeyNotificationCallback("Item key for which to register notifications", 
                                       new CacheItemUpdatedCallBackImpl(),
                                       new CacheItemRemovedCallBackImpl());

Continuous Query

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

TayzGrid then creates a Continuous Query in the in-memory data grid cluster and monitors all add, update, and remove operations to see if they modify stored items in the data grid matching this Continuous Query criteria. If so, then TayzGrid notifies your client application about it.

Below are the events you can register.


ContinuousQuery continuousQuery = new ContinuousQuery(query, values);
          
  //Item added notification
  continuousQuery.RegisterAddNotification(new ContinuousQueryItemAddedCallback() {
    @Override
    public void continuousQueryItemAdded(String string) {
      System.out.println(string);
      }
  });

  //Item update notification
  continuousQuery.RegisterUpdateNotification(new ContinuousQueryItemUpdatedCallback() {
    @Override
    public void continuousQueryItemUpdated(String string) {
    System.out.println(string);
    }
  });

  //Item delete notification
  continuousQuery.RegisterRemoveNotification(new ContinuousQueryItemRemovedCallback() {
  @Override
  public void continuousQueryItemRemoved(String string) {
    System.out.println(string);
    }
  });

Application Initiated Custom Events

TayzGrid 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 TayzGrid messaging and one or more applications are waiting to receive this event. Here is how your application registers to receive these custom events.

Implement CustomListener interface as follows:


import com.alachisoft.tayzgrid.event.*; 

public class CacheEventListner implements CustomListener
{
  public void customEventOccured(CustomEvent event)
    {
      System.out.println("Custom EVENT :- Custom 
      event is raised for Key[ " + event.getKey() + " ] 
      with value: " + event.getSource().toString());
    }
}

What to Do Next?