Event Driven .NET and Java Data Sharing through Distributed Cache

These days, many companies are running both .NET and Java applications in their enterprise environment. And, often these applications need to share data with each other at runtime. The most common way they do that today is by storing the data in the database and having the other application poll and look for it. Some people also develop web services solely for the purpose of allowing Java applications to obtain data from .NET applications and vice versa.

The problem with the first approach is that data sharing cannot happen instantaneously because the other “consumer” application has to poll the database which happens after some predetermined interval. It also has performance and scalabilities issues like any application accessing the database for data. As know, a database cannot scale the same way that today’s applications can. This is because although you can linearly scale an application tier by adding more application servers, you cannot do the same at the database tier.

The second approach requires a lot of custom programming and essentially changing your application’s architecture just so you can share data with other applications, whether they’re .NET or Java. It would be much nicer if you could continue to develop each application for the purpose that it is being built and not worry about creating a custom framework for data sharing.

Runtime Data Sharing through a Distributed Cache

The second approach requires a lot of custom programming and essentially changing your application’s architecture just so you can share data with other applications, whether they’re .NET or Java. It would be much nicer if you could continue to develop each application for the purpose that it is being built and not worry about creating a custom framework for data sharing.

Ideally, you would want to have an event driven model where a .NET application can be notified whenever a Java application has some data for it and vice versa. But, as you know, .NET and Java are not inherently compatible for this kind of use.

This is where a distributed cache like NCache comes in really handy. NCache provides events that are platform independent and can be shared between .NET and Java. NCache also provides binary level data type compatibility between .NET and Java. This allows you to not only receive events but also corresponding data in the form of objects and all of that without having to go through any XML based transformation for data sharing purposes.

NCache event notification framework enables you to register to notified when different types of events occur within the cache cluster. This way, whenever there are any changes made to the data, either by .NET or Java applications, your application gets notified. Here is sample code using NCache item-based event for data sharing in Java:

import com.alachisoft.ncache.web.caching.*;

public void AddToCache()
{
CacheEventListener onItemRemoved = new CacheEventListner();
Cache cache = NCache.initializeCache("PORCache");
Employee emp = new Employee();
emp.setDept("Mechanical");

CacheItem cItem = new CacheItem(emp);
cItem.setItemRemoveCallback(onItemRemoved);
cache.insert("EMP-1000-ENG", cItem);
}

public class CacheEventListner implements CacheItemRemovedCallback
{
  ...
  public void itemRemoved(String key, Object value,
  CacheItemRemovedReason reason)
  {
Employee emp = (Employee) key;
System.out.println("Employee Removed " + key + "Dept" + emp.getDept());
  }
   ...
}

NCache provides you different cached item level notifications like item-added, item-removed and item-updated. Applications can register interest in various cached item keys (that may or may not exist in the cache yet), and they’re notified separately whenever that item is added, updated or removed from the distributed cache by anybody for any reason. For example, even if an item is removed due to expiration or eviction, the item-remove event notification is fired.

Both .NET and Java applications can register interest for the same cached items and be notified about them. The notification includes the affected cached item as well, which is transformed into either .NET or Java, depending on the type of application.

Here is a sample code of using NCache item-based event for data sharing in .NET:

public void AddToCache()
{
Cache cache = NCache.InitializeCache("PORCache");
Employee emp = new Employee();
emp.Name = "David Rox";
emp.Dept = "Engineering";
...

cache.Insert("EMP-1000-ENG", emp, null,
Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration,CacheItemPriority.Default);

//Register Callback to get notified of changes related to provided key
cache.RegisterKeyNotificationCallback("EMP-1000-ENG",
new CacheItemUpdatedCallback(OnItemUpdated),
newCacheItemRemovedCallback(OnItemRemoved));
}
...
void OnItemRemoved(string key, object value,CacheItemRemovedReason reason)
{
//Item is removed. Do something
Employee emp = (Employee) value;
	Console.WriteLine("Employee Removed {0}, Name {1}", key, emp.Dept);
}

In summary, with NCache you can not only share data between .NET and Java applications are runtime but can also use distributed events to notify applications of any change in data.

So, download a fully working 60-day trial of NCache Enterprise and try it out for yourself.

Download NCache Trial | NCache Details

This entry was posted in data sharing, Distributed Cache, Distributed events, Event driven data sharing and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


7 − = three

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>