Chapter 10. Run Red Hat JBoss Data Grid in Library Mode (Single-Node Setup)

10.1. Create a Simple Class

Create a Simple Class

  1. Create a new Maven project and Java file, entitled SimpleLibraryExample.java:

    In your editor of choice create a new Maven project. Add a Java file, entitled SimpleLibraryExample.java, in the org.jdg.example package in this project.

  2. Define the references to a DefaultCacheManager:

    Define the basic imports, and create a DefaultCacheManager. This will then obtain a local reference to the default cache, as no cache name is specified:

    package org.jdg.example;
    
    import org.infinispan.Cache;
    import org.infinispan.manager.DefaultCacheManager;
    
    public class SimpleLibraryExample {
    
       public static void main(String args[]) throws Exception {
          Cache<Object, Object> c = new DefaultCacheManager().getCache();
       }
    
    }
  3. Define the Maven dependencies

    In your pom.xml define the infinispan-embedded dependency:

    <groupId>org.infinispan</groupId>
    <artifactId>infinispan-embedded</artifactId>
    <version>{FullInfinispanVersion}</version>
  4. Run the Main Method

    Use the following command to run the main method:

    mvn compile exec:java -Dexec.mainClass="org.infinispan.quickstart.embeddedcache.Quickstart
  5. Exit

    Once compiled the class should execute, continuing to run until the process is terminated. This process may be exited by entering Ctrl+C on the command line where it was launched.

10.2. Use the Default Cache

10.2.1. Add and Remove Data from the Cache

Red Hat JBoss Data Grid offers an interface that is similar to the proposed JSR-107 API to access and alter data stored in a cache.

The following procedures demonstrate adding and removing data from the cache:

Add and Remove Data from the Cache

  1. Add an entry, replacing key and value with the desired key and value:

    cache.put("key", "value");
  2. Confirm that the entry is present in the cache:

    assertEquals(1, cache.size());
    assertTrue(cache.containsKey("key"));
  3. Remove the entry from the cache:

    Object v = cache.remove("key");
  4. Confirm that the entry is no longer present in the cache:

    assertEquals("value", v);
    assertTrue(cache.isEmpty());

10.2.2. Adding and Replacing a Key Value

Red Hat JBoss Data Grid offers a thread-safe data structure.

The following procedure is an example that demonstrates conditionally adding values into the cache: file does:

Adding and Replacing a Key Value

  1. Add an entry key with value as the key’s value.

    cache.put("key", "value");

Replacing a Key Value.

  1. The following code searches for keys (named key and key2). If the two specific keys beings searched for are not found, JBoss Data Grid creates two new keys with the specified key names and values. In this example only key2 will be added to the cache, as key is already present.

    cache.putIfAbsent("key", "newValue");
    cache.putIfAbsent("key2", "newValue2");
  2. The following code confirms that the value of the stored key equals the value we wanted to store.

    assertEquals(cache.get("key"), "value");
    assertEquals(cache.get("key2"), "newValue2");

10.2.3. Removing Entries

Separate methods are used to remove entries depending on how JBoss Data Grid is used:

Library Mode

All of the following methods are found on org.infinispan.Cache and its subclasses.

  • remove(key) - remove a single key from the cache.
  • removeAsync(key) - remove a single key from the cache, asynchronously.
  • clear() - removes all of the mappings from the cache, leaving it empty once the call completes.
  • clearAsync() - asynchronously remove all of the mappings from the cache, leaving it empty once the call completes.
  • cache.evict(key) - remove the entry from the cache, moving it to the cache store if one is defined. With no cache store defined the entry is removed from the cache and is lost.

Remote Client-Server Mode

All of the following methods are found on org.infinispan.client.hotrod.RemoteCache and its subclasses.

  • remove(key) - remove a single key from the cache.
  • removeAsync(key) - remove a single key from the cache, asynchronously.
  • clear() - removes all of the mappings from the cache, leaving it empty once the call completes.
  • clearAsync() - asynchronously remove all of the mappings from the cache, leaving it empty once the call completes.
  • removeWithVersion(key, version) - remove a single key from the cache only if its current version matches the supplied version.
  • removeWithVersionAsync(key, value) - asynchronously remove a single key from the cache only if its current version matches the supplied version.

For additional information on any of the above methods refer to the API Documentation .

10.2.4. Placing and Retrieving Sets of Data

The AdvancedCache and RemoteCache interfaces include methods to either put or get a Map of existing data in bulk. These operations are typically much more efficient than an equivalent sequence of individual operations, especially when using them in server-client mode, as a single network operation occurs as opposed to multiple transactions.

When using the bulk operations the memory overhead is higher during the operation itself, as the get or put operation must accommodate for the full Map in a single execution.

The methods for each class are found below:

  • AdvancedCache:

    • Map<K,V>getAll(Set<?> keys) - returns a Map containing the values associated with the set of keys requested.
    • void putAll(Map<? extends K, ? extends V> map, Metadata metadata) - copies all of the mappings from the specified map to this cache, which takes an instance of Metadata to provide metadata information such as the lifespan, version, etc. on the entries being stored.
  • RemoteCache:

    • Map<K,V>getAll(Set<? extends K> keys) - returns a Map containing the values associated with the set of keys requested.
    • void putAll(Map<? extends K, ? extends V> map) - copies all of the mappings from the specified map to this cache.
    • void putAll(Map<? extends K, ? extends V> map, long lifespan, TimeUnit unit) - copies all of the mappings from the specified map to this cache, along with a lifespan before the entry is expired.
    • void putAll(Map<? extends K, ? extends V> map, long lifespan, TimeUnit lifespanUnit, long maxIdleTime, TimeUnit maxIdleTimeUnit) - copies all of the mappings from the specified map to this cache, along with both a timespan before the entries are expired and the maximum amount of time the entry is allowed to be idle before it is considered to be expired.

10.2.5. Adjust Data Life

Red Hat JBoss Data Grid entries are immortal by default, but these settings can be altered.

The following procedure is an example that demonstrates defining key mortality:

Adjust the Data Life

  1. Alter the key’s lifespan value:

    cache.put("key", "value", 5, TimeUnit.SECONDS);
  2. Check if the cache contains the key:

    assertTrue(cache.containsKey("key"));
  3. After the allocated lifespan time has expired, the key is no longer in the cache:

    Thread.sleep(10000);
    assertFalse(cache.containsKey("key"));

10.2.6. Default Data Mortality

As a default, newly created entries do not have a life span or maximum idle time value set. Without these two values, a data entry will never expire and is therefore known as immortal data.

10.2.7. Register the Named Cache Using XML

To configure the named cache declaratively (using XML) rather than programmatically, configure the infinispan.xml file.

An example infinispan.xml file is located in https://github.com/jboss-developer/jboss-jdg-quickstarts/ within the secure-embedded-cache/src/main/resources/ folder, and the full schema is available in the docs/schema/ directory of the Red Hat JBoss Data Grid Library distribution.