Example Infinispan Groovy Script Using a Custom Data Container

Example One | Example Two | Example Three

The following script creates two local caches, adds some data to cacheA, prints out the size of each cache, and then terminates — we’ll add a DataContainer in a moment and see what happens.

@GrabConfig(systemClassLoader=true)

@Grapes(
    @Grab(group='org.infinispan', module='infinispan-core', version='6.0.1.Final')
)
import org.infinispan.manager.DefaultCacheManager
import org.infinispan.configuration.cache.ConfigurationBuilder
import org.infinispan.configuration.cache.CacheMode
import org.infinispan.Cache

def cacheAId = "cacheA", cacheBId = "cacheB"

def configuration = new ConfigurationBuilder()
    .jmxStatistics()
    .clustering()
        .cacheMode (CacheMode.LOCAL)
    .build();

def cacheManager = new DefaultCacheManager (configuration)

Cache cacheA = cacheManager.getCache(cacheAId)
Cache cacheB = cacheManager.getCache(cacheBId)

cacheA.put("A", "A");
cacheA.put("B", "B");
cacheA.put("C", "C");

print "cacheA.size: ${cacheA.size ()}, cacheB.size: ${cacheB.size ()}"

// If we don't stop the cacheManager then we won't be able to run
// this script multiple times.
cacheManager.stop ()

—– Results appear below —–

cacheA.size: 3, cacheB.size: 0

This is what we would expect, now let’s add a custom DataContainer and re-run the script.

In this example we need to change the configuration as follows:

def configurationBuilder = new ConfigurationBuilder()
    .jmxStatistics()
    .clustering()
        .cacheMode (CacheMode.LOCAL)

def dataContainerBuilder = configurationBuilder.dataContainer()
dataContainerBuilder.dataContainer (new DefaultDataContainer (5))

The output of the script when the CacheManager has a custom DataContainer.

cacheA.size: 3, cacheB.size: 3

This may not be ideal however as both cacheA and cacheB are sharing the same DataContainer. In order to fix this, we need to configure both cacheA and cacheB so that they have their own DataContainer — we do this as follows:

def configurationBuilder = new ConfigurationBuilder()
    .jmxStatistics()
    .clustering()
        .cacheMode (CacheMode.LOCAL)

def configuration = configurationBuilder.build();

def cacheManager = new DefaultCacheManager (configuration)

def configurationBuilderA = new ConfigurationBuilder()
def dataContainerBuilderA = configurationBuilder.dataContainer()
    dataContainerBuilderA.dataContainer (new DefaultDataContainer (5))
def configurationA = configurationBuilderA.build ()

cacheManager.defineConfiguration(cacheAId, configurationA)

def configurationBuilderB = new ConfigurationBuilder()
def dataContainerBuilderB = configurationBuilder.dataContainer()
    dataContainerBuilderB.dataContainer (new DefaultDataContainer (5))
def configurationB = configurationBuilderB.build ()

cacheManager.defineConfiguration(cacheBId, configurationB)

We can now see that the size of each cache is as we would expect it as the caches are not sharing the same DataContainer.

cacheA.size: 3, cacheB.size: 0

Leave a Reply