Making a cluster with Infinispan and JGroups 5

Recently i did a POC on infinispan and JGroups and submitted this POC in my company. I think it is worth to share on blog.

First a little about infinispan.

Infinispan is an open source data grid platform.
It exposes a JSR-107 compatible Cache interface (which in turn extends java.util.Map) in which you can store objects.

Now we will be making a cluster with the help of infinispan and jgroups. JBoss AS also makes use of infinispan and jgroups for clustering.

Approach

What is JGroups
JGroups is a toolkit for reliable messaging. It can be used to create clusters whose nodes can send messages to each other. The main features include
* Cluster creation and deletion. Cluster nodes can be spread across LANs or WANs
* Joining and leaving of clusters
* Membership detection and notification about joined/left/crashed cluster nodes
* Detection and removal of crashed nodes
* Sending and receiving of node-to-cluster messages (point-to-multipoint)
* Sending and receiving of node-to-node messages (point-to-point)

JGroups comes with a large number of protocols (but anyone can write their own), for example
* Transport protocols: UDP (IP Multicast) or TCP
* Fragmentation of large messages
* Reliable unicast and multicast message transmission. Lost messages are retransmitted
* Failure detection: crashed nodes are excluded from the membership
* Flow control to prevent slow receivers to get overrun by fast senders
* Ordering protocols: FIFO, Total Order
* Membership
* Encryption
* Compression
For more detail please visit http://www.jgroups.org/

First make jgroups.xml file which will contain information about multicast address, multicastport and other things. If customization is required then only we require to make it, otherwise the default will be provided by jgroups jar and that will get loaded at run time.

Now we will make infinispan.xml that will contain the information about global transport property i.e. which is responsible for clustering. In our case it will be the jgroups.xml file

<!--?xml version="1.0" encoding="UTF-8"?-->
<?xml version="1.0" encoding="UTF-8"?>
<infinispan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="urn:infinispan:config:5.1 http://www.infinispan.org/schemas/infinispan-config-5.1.xsd"
	xmlns="urn:infinispan:config:5.1">

	<global>
		<transport>
			<properties>
				<property name="configurationFile" value="jgroups.xml" />
			</properties>
		</transport>
	</global>

	<default>
		<!-- Configure a synchronous replication cache -->
		<clustering mode="replication">
			<sync />
		</clustering>
	</default>

</infinispan>

So above we gave the reference of jgroups.xml file and we made the cache replicated. as above in infinispan.xml we did:

<default>
     <!-- Configure a synchronous replication cache -->
       <clustering mode="replication">
         <sync />
      </clustering>
 </default>
    For testing it:-

First we need to take the cache from default cache manager like :

DefaultCacheManager cacheManager = new DefaultCacheManager("infinispan-replication.xml");
employeeCache = cacheManager.getCache("employeeCache");

Below is the code Snippet:

package com.mgr.clustering;

import java.io.IOException;

import org.infinispan.Cache;
import org.infinispan.manager.DefaultCacheManager;

public class NodeClustering {

	private DefaultCacheManager cacheManager; 
	private Cache<String, String> userInputCache;
	
	public NodeClustering(){
		
	}
	
	public void initialize() throws IOException{
		cacheManager = new DefaultCacheManager("infinispan-replication.xml");
		userInputCache = cacheManager.getCache("userInput");
	}
	
	public void putInotCache(String key, String value){
		userInputCache.put(key, value);
	}
	
	public String getFromCache(String key){
		return userInputCache.get(key);
	}
}

Now we can put key value pair inside our cache:

employeeCache.put(key, value);

Now we are complete with the configuration and with cache manager part.
Now its time to test our cluster.

Below is the main program:

package com.ngr.client;

import java.io.IOException;

import com.mgr.clustering.NodeClustering;

public class Node1 {

	public static void main(String[] args) throws IOException {
		System.setProperty("java.net.preferIPv4Stack", "true");
		NodeClustering clustering = new NodeClustering(); 
		clustering.initialize();// initialize the cache
		clustering.putInotCache("name", "XYZ");// put some item
	}
}

First I will explain the above thing. Here we initialize the cache i.e. get the instance of cache (“Replicated cache”). Now we are putting an item “XYZ” inside that.

Now we can get this item if we run the below program on an another instance of JVM or in an another machine in the same network.

package com.ngr.client;

import java.io.IOException;

import com.mgr.clustering.NodeClustering;

public class Node1 {

	public static void main(String[] args) throws IOException {
		System.setProperty("java.net.preferIPv4Stack", "true");
		NodeClustering clustering = new NodeClustering();
		clustering.initialize();
		System.out.println(clustering.getFromCache("name"));
	}
}

The output of above program will come as : XYZ

So this is done through replication of the cache i.e. we made a cluster which can replicate cache between them.

5 thoughts on “Making a cluster with Infinispan and JGroups

  1. Reply Daniel Beaulieu Aug 16,2013 2:10 am

    “Thanks for sharing, this is a fantastic blog. Fantastic.”

  2. Reply fas Aug 20,2013 3:41 pm

    Hi,
    In a replicated cache, is UDP multicast used for replicating the data? Or just for node discovery?

  3. Reply oppanblogs Aug 21,2013 6:43 am

    Hi Fas,

    UDP is used for sending the data to a group of destination i.e. who so ever is listening on UDP multicast address will get that data.

    So it is up to us that By the use of UDP we can do node discovery as well replicate the data like we did by the help of Infinispan.

  4. Reply Isabel Marant Bobby Suede Sneakers Khaki Store Sep 3,2013 11:19 pm

    I’m having a small issue I cannot make my reader pick up your rss feed, I’m using google reader by the way.

  5. Reply snort this Jan 21,2014 3:02 am

    Very interesting details you have mentioned, appreciate it for posting.

Leave a Reply