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 Copy linkLink copied to clipboard!
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.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You must define the value for
${version.infinispan.bom}`in the `<properties>section of the pom.xml file.Declare the required Data Grid dependencies as provided.
pom.xml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
10.2. Configuring Data Grid caches in Red Hat JBoss EAP Copy linkLink copied to clipboard!
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.
jboss-cli.sh --connect
$ jboss-cli.sh --connectCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a cache container.
/subsystem=infinispan/cache-container=exampleCacheContainer:add(statistics-enabled=true)
/subsystem=infinispan/cache-container=exampleCacheContainer:add(statistics-enabled=true)Copy to Clipboard Copied! Toggle word wrap Toggle overflow This creates a cache container called
exampleCacheContainerwith statistics enabled.Add a cache to the cache container.
/subsystem=infinispan/cache-container=exampleCacheContainer/local-cache=exampleCache:add(statistics-enabled=true)
/subsystem=infinispan/cache-container=exampleCacheContainer/local-cache=exampleCache:add(statistics-enabled=true)Copy to Clipboard Copied! Toggle word wrap Toggle overflow This creates a local cache named
exampleCachein theexampleCacheContainercache container with statistics enabled.
10.3. Using Data Grid caches in Red Hat JBoss EAP applications Copy linkLink copied to clipboard!
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;
@Resource(lookup = "java:jboss/infinispan/cache/exampleCacheContainer/exampleCache") private Cache<String, String> ispnCache;Copy to Clipboard Copied! Toggle word wrap Toggle overflow This defines a
CachecalledispnCache.You can put, get and remove entries from the cache as follows:
Get value of a key
String value = ispnCache.get(key);
String value = ispnCache.get(key);Copy to Clipboard Copied! Toggle word wrap Toggle overflow This retrieves the value of the key in the cache. If the key is not found,
nullis returned.Put value in a key
String oldValue = ispnCache.put(key,value);
String oldValue = ispnCache.put(key,value);Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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);
String value = ispnCache.remove(key);Copy to Clipboard Copied! Toggle word wrap Toggle overflow This removes the key from the cache.