Este conteúdo não está disponível no idioma selecionado.
Chapter 5. The ConfigurationBuilder API
- Chain coding of configuration options in order to make the coding process more efficient
- Improve the readability of the configuration
5.1. Using the ConfigurationBuilder API Copiar o linkLink copiado para a área de transferência!
5.1.1. Programmatically Create a CacheManager and Replicated Cache Copiar o linkLink copiado para a área de transferência!
Example 5.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);
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(); - 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();In the first line of the configuration, a new cache configuration object (namedc) is created using theConfigurationBuilder. Configurationcis 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";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);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 calledrepland its configuration is based on the configuration provided for cache configurationcin the first line. Cache<String, String> cache = manager.getCache(newCacheName);In the fourth line of the configuration, the cache manager is used to obtain a reference to the unique instance of thereplthat is held by the cache manager. This cache instance is now ready to be used to perform operations to store and retrieve data.
Note
5.1.2. Create a Customized Cache Using the Default Named Cache Copiar o linkLink copiado para a área de transferência!
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.
Example 5.2. Configuring Customized Default Cache
EmbeddedCacheManager manager = new DefaultCacheManager("infinispan-config-file.xml");
Configuration dcc = cacheManager.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);
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(); - 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(); - 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); - Get default cache with custom configuration changes:
Cache<String, String> cache = manager.getCache(newCacheName);
5.1.3. Create a Customized Cache Using a Non-Default Named Cache Copiar o linkLink copiado para a área de transferência!
replicatedCache as the base instead of the default cache.
Example 5.3. Creating a Customized Cache Using a Non-Default Named Cache
EmbeddedCacheManager manager = new DefaultCacheManager("infinispan-config-file.xml");
Configuration rc = cacheManager.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);
Procedure 5.3. Create a Customized Cache Using a Non-Default Named Cache
- Read the
replicatedCacheto get the default configuration:EmbeddedCacheManager manager = new DefaultCacheManager("infinispan-config-file.xml"); Configuration rc = cacheManager.getCacheConfiguration("replicatedCache"); - 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(); - Register/define your cache configuration with a cache manager where newCacheName is the name of cache specified in
infinispan-config-file.xmlmanager.defineConfiguration(newCacheName, c); - Get a default cache with custom configuration changes:
Cache<String, String> cache = manager.getCache(newCacheName);
5.1.4. Using the Configuration Builder to Create Caches Programmatically Copiar o linkLink copiado para a área de transferência!
5.1.5. Global Configuration Examples Copiar o linkLink copiado para a área de transferência!
5.1.5.1. Globally Configure the Transport Layer Copiar o linkLink copiado para a área de transferência!
Example 5.4. Configuring the Transport Layer
GlobalConfiguration globalConfig = new GlobalConfigurationBuilder()
.globalJmxStatistics().enable()
.build();
5.1.5.2. Globally Configure the Cache Manager Name Copiar o linkLink copiado para a área de transferência!
Example 5.5. Configuring the Cache Manager Name
GlobalConfiguration globalConfig = new GlobalConfigurationBuilder()
.globalJmxStatistics()
.cacheManagerName("SalesCacheManager")
.mBeanServerLookup(new JBossMBeanServerLookup())
.enable()
.build();
5.1.5.3. Globally Customize Thread Pool Executors Copiar o linkLink copiado para a área de transferência!
Example 5.6. Customize Thread Pool Executors
GlobalConfiguration globalConfig = new GlobalConfigurationBuilder()
.replicationQueueScheduledExecutor()
.factory(new DefaultScheduledExecutorFactory())
.addProperty("threadNamePrefix", "RQThread")
.build();
5.1.6. Cache Level Configuration Examples Copiar o linkLink copiado para a área de transferência!
5.1.6.1. Cache Level Configuration for the Cluster Mode Copiar o linkLink copiado para a área de transferência!
Example 5.7. Configure Cluster Mode at Cache Level
Configuration config = new ConfigurationBuilder()
.clustering()
.cacheMode(CacheMode.DIST_SYNC)
.sync()
.l1().lifespan(25000L).enable()
.hash().numOwners(3)
.build();
5.1.6.2. Cache Level Eviction and Expiration Configuration Copiar o linkLink copiado para a área de transferência!
Example 5.8. Configuring Expiration and Eviction at the Cache Level
Configuration config = new ConfigurationBuilder()
.eviction()
.maxEntries(20000).strategy(EvictionStrategy.LIRS).expiration()
.wakeUpInterval(5000L)
.maxIdle(120000L)
.build();
5.1.6.3. Cache Level Configuration for JTA Transactions Copiar o linkLink copiado para a área de transferência!
Example 5.9. 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();
5.1.6.4. Cache Level Configuration Using Chained Persistent Stores Copiar o linkLink copiado para a área de transferência!
Example 5.10. Configuring Chained Persistent Stores at Cache Level
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 Copiar o linkLink copiado para a área de transferência!
Example 5.11. Configuring Advanced Externalizers at Cache Level
GlobalConfiguration globalConfig = new GlobalConfigurationBuilder()
.serialization()
.addAdvancedExternalizer(new PersonExternalizer())
.addAdvancedExternalizer(999, new AddressExternalizer())
.build();