Chapter 6. The ConfigurationBuilder API
- Chain coding of configuration options in order to make the coding process more efficient
- Improve the readability of the configuration
6.1. Using the ConfigurationBuilder API
6.1.1. Programmatically Create a CacheManager and Replicated Cache
Procedure 6.1. Configure the CacheManager Programmatically
EmbeddedCacheManager manager = new DefaultCacheManager("my-config-file.xml"); Cache defaultCache = manager.getCache(); Configuration c = new ConfigurationBuilder().clustering().cacheMode(CacheMode.REPL_SYNC) .build(); String newCacheName = "repl"; manager.defineConfiguration(newCacheName, c); Cache<String, String> cache = manager.getCache(newCacheName);
- 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.
- Create a new synchronously replicated cache programmatically.
- Create a new configuration object instance using the ConfigurationBuilder helper object:In the first line of the configuration, a new cache configuration object (named
c
) 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
). - Define or register the configuration with a manager: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 called
repl
and its configuration is based on the configuration provided for cache configurationc
in the first line. - In the fourth line of the configuration, the cache manager is used to obtain a reference to the unique instance of the
repl
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
6.1.2. Create a Customized Cache Using the Default Named Cache
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.
Procedure 6.2. Customize the Default Cache
String newCacheName = "newCache"; EmbeddedCacheManager manager = new DefaultCacheManager("infinispan-config-file.xml"); Configuration dcc = manager.getDefaultCacheConfiguration(); Configuration c = new ConfigurationBuilder().read(dcc).clustering() .cacheMode(CacheMode.DIST_SYNC).l1().lifespan(60000L).enable() .build(); manager.defineConfiguration(newCacheName, c); Cache<String, String> cache = manager.getCache(newCacheName);
- Read an instance of a default
Configuration
object to get the default configuration. - Use the
ConfigurationBuilder
to construct and modify the cache mode and L1 cache lifespan on a new configuration object. - Register/define your cache configuration with a cache manager.
- Obtain a reference to
newCache
, containing the specified configuration.
6.1.3. Create a Customized Cache Using a Non-Default Named Cache
replicatedCache
as the base instead of the default cache.
Procedure 6.3. Creating a Customized Cache Using a Non-Default Named Cache
String newCacheName = "newCache"; EmbeddedCacheManager manager = new DefaultCacheManager("infinispan-config-file.xml"); Configuration rc = manager.getCacheConfiguration("replicatedCache"); Configuration c = new ConfigurationBuilder().read(rc).clustering() .cacheMode(CacheMode.DIST_SYNC).l1().lifespan(60000L).enable() .build(); manager.defineConfiguration(newCacheName, c); Cache<String, String> cache = manager.getCache(newCacheName);
- Read the
replicatedCache
to get the default configuration. - Use the
ConfigurationBuilder
to construct and modify the desired configuration on a new configuration object. - Register/define your cache configuration with a cache manager.
- Obtain a reference to
newCache
, containing the specified configuration.
6.1.4. Using the Configuration Builder to Create Caches Programmatically
6.1.5. Global Configuration Examples
6.1.5.1. Globally Configure the Transport Layer
Example 6.1. Configuring the Transport Layer
GlobalConfiguration globalConfig = new GlobalConfigurationBuilder() .transport().defaultTransport() .build();
6.1.5.2. Globally Configure the Cache Manager Name
Example 6.2. Configuring the Cache Manager Name
GlobalConfiguration globalConfig = new GlobalConfigurationBuilder() .globalJmxStatistics() .cacheManagerName("SalesCacheManager") .mBeanServerLookup(new JBossMBeanServerLookup()) .enable() .build();
6.1.5.3. Globally Configure JGroups
Example 6.3. JGroups Programmatic Configuration
GlobalConfiguration gc = new GlobalConfigurationBuilder() .transport() .defaultTransport() .addProperty("configurationFile","jgroups.xml") .build();
jgroups.xml
in the classpath; if no instances are found in the classpath it will then search for an absolute path name.
6.1.6. Cache Level Configuration Examples
6.1.6.1. Cache Level Configuration for the Cluster Mode
Example 6.4. Configure Cluster Mode at Cache Level
Configuration config = new ConfigurationBuilder() .clustering() .cacheMode(CacheMode.DIST_SYNC) .sync() .l1().lifespan(25000L).enable() .hash().numOwners(3) .build();
6.1.6.2. Cache Level Eviction and Expiration Configuration
Example 6.5. Configuring Expiration and Eviction at the Cache Level
Configuration config = new ConfigurationBuilder() .eviction() .maxEntries(20000).strategy(EvictionStrategy.LIRS).expiration() .wakeUpInterval(5000L) .maxIdle(120000L) .build();
6.1.6.3. Cache Level Configuration for JTA Transactions
Example 6.6. Configuring JTA Transactions at Cache Level
Configuration config = new ConfigurationBuilder() .locking() .concurrencyLevel(10000).isolationLevel(IsolationLevel.REPEATABLE_READ) .lockAcquisitionTimeout(12000L).useLockStriping(false).writeSkewCheck(true) .transaction() .transactionManagerLookup(new GenericTransactionManagerLookup()) .recovery().enable() .jmxStatistics().enable() .build();
6.1.6.4. Cache Level Configuration Using Chained Persistent Stores
Example 6.7. Configuring Chained Persistent Stores at Cache Level
Configuration conf = new ConfigurationBuilder() .persistence() .passivation(false) .addSingleFileStore() .location("/tmp/firstDir") .persistence() .passivation(false) .addSingleFileStore() .location("/tmp/secondDir") .build();
6.1.6.5. Cache Level Configuration for Advanced Externalizers
Example 6.8. Configuring Advanced Externalizers at Cache Level
GlobalConfiguration globalConfig = new GlobalConfigurationBuilder() .serialization() .addAdvancedExternalizer(new PersonExternalizer()) .addAdvancedExternalizer(999, new AddressExternalizer()) .build();
6.1.6.6. Cache Level Configuration for Partition Handling (Library Mode)
ConfigurationBuilder dcc = new ConfigurationBuilder(); dcc.clustering().partitionHandling().enabled(true);
Note