Chapter 5. The ConfigurationBuilder API
The ConfigurationBuilder API is a programmatic configuration API in Red Hat JBoss Data Grid.
The ConfigurationBuilder API is designed to assist with:
- Chain coding of configuration options in order to make the coding process more efficient
- Improve the readability of the configuration
In JBoss Data Grid, the ConfigurationBuilder API is also used to enable CacheLoaders and configure both global and cache level operations.
5.1. Using the ConfigurationBuilder API Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
5.1.1. Programmatically Create a CacheManager and Replicated Cache Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Programmatic configuration in Red Hat JBoss Data Grid almost exclusively involves the ConfigurationBuilder API and the CacheManager. The following is an example of a programmatic CacheManager configuration:
An explanation of each line of the provided configuration is as follows:
Procedure 5.1. Steps for Programmatic Configuration in JBoss Data Grid
- Create a CacheManager as a starting point in an XML file. If required, this CacheManager can be programmed in runtime to the specification that meets the requirements of the use case. The following is an example of how to create a CacheManager:
EmbeddedCacheManager manager = new DefaultCacheManager("my-config-file.xml"); Cache defaultCache = manager.getCache();
EmbeddedCacheManager manager = new DefaultCacheManager("my-config-file.xml"); Cache defaultCache = manager.getCache();
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Create a new synchronously replicated cache programmatically.
- Create a new configuration object instance using the ConfigurationBuilder helper object:
Configuration c = new ConfigurationBuilder().clustering().cacheMode(CacheMode.REPL_SYNC) .build();
Configuration c = new ConfigurationBuilder().clustering().cacheMode(CacheMode.REPL_SYNC) .build();
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the first line of the configuration, a new cache configuration object (namedc
) is created using theConfigurationBuilder
. Configurationc
is assigned the default values for all cache configuration options except the cache mode, which is overridden and set to synchronous replication (REPL_SYNC
). - Set the cache mode to synchronous replication:
String newCacheName = "repl";
String newCacheName = "repl";
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the second line of the configuration, a new variable (of typeString
) is created and assigned the valuerepl
. - Define or register the configuration with a manager:
manager.defineConfiguration(newCacheName, c);
manager.defineConfiguration(newCacheName, c);
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the third line of the configuration, the cache manager is used to define a named cache configuration for itself. This named cache configuration is calledrepl
and its configuration is based on the configuration provided for cache configurationc
in the first line. Cache<String, String> cache = manager.getCache(newCacheName);
Cache<String, String> cache = manager.getCache(newCacheName);
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the fourth line of the configuration, the cache manager is used to obtain a reference to the unique instance of therepl
that is held by the cache manager. This cache instance is now ready to be used to perform operations to store and retrieve data.
Note
Programmatic configurations can only be used with JBoss Data Grid's Library mode.
5.1.2. Create a Customized Cache Using the Default Named Cache Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The default cache configuration (or any customized configuration) can serve as a starting point to create a new cache.
As an example, if the
infinispan-config-file.xml
specifies the configuration for a replicated cache as a default and a distributed cache with a customized lifespan value is required. The required distributed cache must retain all aspects of the default cache specified in the infinispan-config-file.xml
file except the mentioned aspects.
The following is an example of a customized default cache configuration:
An explanation of the provided configuration is as follows:
Procedure 5.2. Customize the Default Cache
- Read an instance of a default Configuration object to get the default configuration:
EmbeddedCacheManager manager = new DefaultCacheManager("infinispan-config-file.xml"); Configuration dcc = cacheManager.getDefaultCacheConfiguration();
EmbeddedCacheManager manager = new DefaultCacheManager("infinispan-config-file.xml"); Configuration dcc = cacheManager.getDefaultCacheConfiguration();
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Use the ConfigurationBuilder to construct and modify the cache mode and L1 cache lifespan on a new configuration object:
Configuration c = new ConfigurationBuilder().read(dcc).clustering() .cacheMode(CacheMode.DIST_SYNC).l1().lifespan(60000L).enable() .build();
Configuration c = new ConfigurationBuilder().read(dcc).clustering() .cacheMode(CacheMode.DIST_SYNC).l1().lifespan(60000L).enable() .build();
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Register/define your cache configuration with a cache manager, where cacheName is name of cache specified in
infinispan-config-file.xml
:manager.defineConfiguration(newCacheName, c);
manager.defineConfiguration(newCacheName, c);
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Get default cache with custom configuration changes:
Cache<String, String> cache = manager.getCache(newCacheName);
Cache<String, String> cache = manager.getCache(newCacheName);
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
5.1.3. Create a Customized Cache Using a Non-Default Named Cache Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
A situation can arise where a new customized cache must be created using a named cache that is not the default. The steps to accomplish this are similar to those used when using the default named cache for this purpose.
The difference in approach is due to taking a named cache called
replicatedCache
as the base instead of the default cache. The following is an example of a customized cache using a non-default named cache:
An explanation of the provided example is as follows:
Procedure 5.3. Create a Customized Cache Using a Non-Default Named Cache
- Read the
replicatedCache
to get the default configuration:EmbeddedCacheManager manager = new DefaultCacheManager("infinispan-config-file.xml"); Configuration rc = cacheManager.getCacheConfiguration("replicatedCache");
EmbeddedCacheManager manager = new DefaultCacheManager("infinispan-config-file.xml"); Configuration rc = cacheManager.getCacheConfiguration("replicatedCache");
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Use the ConfigurationBuilder to construct and modify the desired configuration on a new configuration object:
Configuration c = new ConfigurationBuilder().read(rc).clustering() .cacheMode(CacheMode.DIST_SYNC).l1().lifespan(60000L).enable() .build();
Configuration c = new ConfigurationBuilder().read(rc).clustering() .cacheMode(CacheMode.DIST_SYNC).l1().lifespan(60000L).enable() .build();
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Register/define your cache configuration with a cache manager where newCacheName is the name of cache specified in
infinispan-config-file.xml
manager.defineConfiguration(newCacheName, c);
manager.defineConfiguration(newCacheName, c);
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Get a default cache with custom configuration changes:
Cache<String, String> cache = manager.getCache(newCacheName);
Cache<String, String> cache = manager.getCache(newCacheName);
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
5.1.4. Using the Configuration Builder to Create Caches Programmatically Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
As an alternative to using an xml file with default cache values to create a new cache, use the ConfigurationBuilder API to create a new cache without any XML files. The ConfigurationBuilder API is intended to provide ease of use when creating chained code for configuration options.
The following new configuration is valid for global and cache level configuration. GlobalConfiguration objects are constructed using GlobalConfigurationBuilder while Configuration objects are built using ConfigurationBuilder.
5.1.5. Global Configuration Examples Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
5.1.5.1. Globally Configure the Transport Layer Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
A commonly used configuration option is to configure the transport layer. This informs Red Hat JBoss Data Grid how a node will discover other nodes:
GlobalConfiguration globalConfig = new GlobalConfigurationBuilder() .globalJmxStatistics().enable() .build();
GlobalConfiguration globalConfig = new GlobalConfigurationBuilder()
.globalJmxStatistics().enable()
.build();
5.1.5.2. Globally Configure the Cache Manager Name Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The following sample configuration allows you to use options from the global JMX statistics level to configure the name for a cache manager. This name distinguishes a particular cache manager from other cache managers on the same system.
5.1.5.3. Globally Customize Thread Pool Executors Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Some Red Hat JBoss Data Grid features are powered by a group of thread pool executors. These executors can be customized at the global level as follows:
5.1.6. Cache Level Configuration Examples Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
5.1.6.1. Cache Level Configuration for the Cluster Mode Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The following configuration allows the use of options such as the cluster mode for the cache at the cache level rather than globally:
5.1.6.2. Cache Level Eviction and Expiration Configuration Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Use the following configuration to configure expiration or eviction options for a cache at the cache level:
5.1.6.3. Cache Level Configuration for JTA Transactions Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
To interact with a cache for JTA transaction configuration, configure the transaction layer and optionally customize the locking settings. For transactional caches, it is recommended to enable transaction recovery to deal with unfinished transactions. Additionally, it is recommended that JMX management and statistics gathering is also enabled.
5.1.6.4. Cache Level Configuration Using Chained Persistent Stores Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The following configuration can be used to configure one or more chained persistent stores at the cache level:
Configuration config = new ConfigurationBuilder() .persistence() .passivation(false) .addSingleFileStore().shared(false).preload(false).location("/tmp").async().enable().threadPoolSize(20).build();
Configuration config = new ConfigurationBuilder()
.persistence()
.passivation(false)
.addSingleFileStore().shared(false).preload(false).location("/tmp").async().enable().threadPoolSize(20).build();
5.1.6.5. Cache Level Configuration for Advanced Externalizers Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
An advanced option such as a cache level configuration for advanced externalizers can also be configured programmatically as follows: