Infinispan tutorial part 1

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
One of the hottest new additions to JBoss galaxy is Infinispan. Infinispan is an open source, Java-based data grid platform that will start a new era in scalable distributed data grid platforms.
So far you you have probably heard about JBoss Cache as a viable solution for caching and persisting your data. Infinispan has borrowed many concepts from JBoss Cache, however it is basically different because it's a datagrid platform.
Data grids are highly concurrent distributed data structures. Data grids typically allow you to address a large amount of memory and store data in a way that it is quick to access. They also tend to feature low latency retrieval, and maintain adequate copies across a network to provide resilience to server failure.

JBoss cache vs Infinispan

JBoss Cache is basically focused on Clustered applications, how to replicate efficientely data across the cluster nodes so that data is always available to your clients.

On the other hand, Infinispan  has a map-like data structure that is distributed among a grid of servers. Thanks to its lock- and synchronization-free structure, Infinispan is designed from grounds up to scale your cache-based applications on a different magnitude: here we are not speaking in terms on 1, 5 or 10 nodes - you can distribute your Cache on hundreds of servers each one adding virtually its memory to the Cache.
Why Infinispan is so scalable ? the main reason is that Infinispan uses (except for nodes discovery) a Peer to Peer communication without the need for central coordination instances, which are the real bottleneck of enterprise applications. 
Specifically, Infinispan nodes issue RPCs – remote procedure calls – to neighbouring nodes, which are invoked on a target node and a response is returned, in many ways similar to a local invocation.

Enough with introductions, let's get working with Infinispan: downlaod the latest stable release from JBoss site:
Unzip the archive. Let's see what it contains:

jboss infinispan tutorial
The core library, infinispan-core.jar, is contained in the root infinispan-4.0.0.FINAL folder. Additionally you need to include dependencies which are located in the "lib" folder.

In the "bin" folder you can find the importConfig script which can be used to import your existing JBoss cache configuration. There you can find as well a GUI demo which shows the most interesting features of Infinispan.

In the "etc" folder you can find some configuration samples which can be used as a good start for learning Infinispan configuration.

Finally in the "modules" directory you can find additional modules like the Query module, the Lucene search module, the Jopr module for monitoring Infinispan and others.  

Setting up a simple example

There are two ways in which you can interact with Infinispan:
One is in embedded mode, where you start an Infinispan instance within your JVM. The other is client/server mode,where you start a remote Infinispan instance and connect to it using a client connector.
In this first tutorial we will show to to start an Infinispan instance embedded with your JVM.
Having added the required libraries to your Project, you can start with an Helloworld example:
DefaultCacheManager m = new DefaultCacheManager(); 

Cache<String, String> cache = m.getCache();

cache.put("name", "Francesco");

String name= cache.get("name");

System.out.println("Name is "+name); 
The CacheManager is the primary mechanism for handling the Cache. It's an heavyweight object so it's strongly adviced to instantiate it and reuse it across your code.
The Cache interface is the JSR-107 Cache compatible interface which provides the cuncurrent, distributed cache structure and contains standard methods for adding (put) and retrieving (get) data from the Cache. 
You can safely reuse this object, as well, once you have instantiated it.

Using a Custom configuration

In real world projects it's likely that you want to provide a custom configuration. You have two options to configure Infinispan: by using a configuration file or programmatically.

Using a configuration file: 
String configFile = "/path/to/my/infinispan_config.xml"; 
DefaultCacheManager m = new DefaultCacheManager(configFile); 
Cache<String, String> c = m.getCache();
A Configuration file is split into the following sections:

    <global />
    <default />

    <namedCache  name="A">
    <namedCache  name="B">

<infinispan> is the root element: the minimum required for a configuration file.
<global> contains System wide local settings.
<default> refers to configuration which is used by all Infinispan caches.
<namedCache> can be used to define custom specialized caches

For example, if you want to define a custom cache for evicting data from the cache you could use the following file:
  <namedCache name="evictionCache">
     <eviction wakeUpInterval="500" maxEntries="5000" strategy="FIFO" />
     <expiration lifespan="60000" maxIdle="10000"/>
The above configuration defines the policy for evicting/expiring items from the cache.
With this configuration a maximum of 5000 entries are allowed in the cache. If you attemp to insert more than 5000 entries, the eviction thread will start removing them from memory using a FIFO strategy.
Also, entries are configured to expire aftere 60 seconds they are added to the cache or if they have been idle for over 10 seconds.

This specialized named cache configuration can be loaded using a the getCache(String s) method:
String configFile = "/path/to/my/infinispan_config.xml";
DefaultCacheManager m = new DefaultCacheManager(configFile);
Cache<String, String> c = m.getCache("evictionCache");
If you need a reference to all configuration options, you can surf here

Configuring a Cache programmatically

Sometimes it can be helpful to define the Cache attributes programmatically. No fear, it's absolutely easy. Just define an instance of the org.infinispan.config.Configuration class and set custom attributes. In this example we're going to override the default items expiration lifespan, setting it to 10000 ms.
Configuration c = new Configuration();
manager.defineConfiguration("myconfig", c);
Cache cache = manager.getCache("myconfig");
This was a first taste of Infinispan. In the Infinispan tutorial part 2 we'll explore some great featues like Cache Stores, Clustering and using Transactions with Infinispan. Stay tuned!