Chapter 10. Using Data Grid in Red Hat JBoss EAP applications Red Hat JBoss EAP includes Data Grid modules that you can use in Red Hat JBoss EAP applications. You can do this in two ways: Include the Data Grid libraries in a Red Hat JBoss EAP application. When you include the Data Grid libraries within an application, the caches are local to the application and cannot be used by other applications. Additionally, the cache configuration is within the application. Use the Data Grid libraries provided by Red Hat JBoss EAP. Using the Data Grid libraries provided by Red Hat JBoss EAP has the following benefits: The cache is shared between applications. The cache configuration is part of Red Hat JBoss EAP standalone or domain XML files. Applications do not include Data Grid libraries, they instead reference the required module from the MANIFEST or jboss-structure.xml configuration files. The following procedures describe using the Data Grid libraries provided by Red Hat JBoss EAP. 10.1. Configuring applications to Use Data Grid modules To use Data Grid libraries provided by Red Hat JBoss EAP in your applications, add Data Grid dependency in the application's pom.xml file. Procedure Import the Data Grid dependency management to control the versions of runtime Maven dependencies. <dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-bom</artifactId> <version>USD{version.infinispan.bom}</version> <type>pom</type> <scope>import</scope> </dependency> You must define the value for USD{version.infinispan.bom}`in the `<properties> section of the pom.xml file. Declare the required Data Grid dependencies as provided . pom.xml <dependencies> <dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-core</artifactId> <scope>provided</scope> </dependency> </dependencies> 10.2. Configuring Data Grid caches in Red Hat JBoss EAP Create Data Grid caches in Red Hat JBoss EAP. Prerequisites Red Hat JBoss EAP is running Procedure Connect to the Red Hat JBoss EAP management CLI. Create a cache container. This creates a cache container called exampleCacheContainer with statistics enabled. Add a cache to the cache container. This creates a local cache named exampleCache in the exampleCacheContainer cache container with statistics enabled. 10.3. Using Data Grid caches in Red Hat JBoss EAP applications You can access Data Grid caches in your applications through resource lookup. Prerequisites Red Hat JBoss EAP is running. You have created Data Grid cahches in Red Hat JBoss EAP. Procedure You can lookup Data Grid caches in your applications like this: @Resource(lookup = "java:jboss/infinispan/cache/exampleCacheContainer/exampleCache") private Cache<String, String> ispnCache; This defines a Cache called ispnCache . You can put, get and remove entries from the cache as follows: Get value of a key String value = ispnCache.get(key); This retrieves the value of the key in the cache. If the key is not found, null is returned. Put value in a key String oldValue = ispnCache.put(key,value); This defines a new key if it does not already exist and associates the value passed. If the key already exists, the original value is replaced. Remove a key String value = ispnCache.remove(key); This removes the key from the cache.

Commands:
<dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-bom</artifactId> <version>USD{version.infinispan.bom}</version> <type>pom</type> <scope>import</scope> </dependency>
<dependencies> <dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-core</artifactId> <scope>provided</scope> </dependency> </dependencies>
jboss-cli.sh --connect
/subsystem=infinispan/cache-container=exampleCacheContainer:add(statistics-enabled=true)
/subsystem=infinispan/cache-container=exampleCacheContainer/local-cache=exampleCache:add(statistics-enabled=true)
@Resource(lookup = "java:jboss/infinispan/cache/exampleCacheContainer/exampleCache") private Cache<String, String> ispnCache;
String value = ispnCache.get(key);
String oldValue = ispnCache.put(key,value);
String value = ispnCache.remove(key);