Configuring Thread Pools with Code
Following are examples of describing how to configure the thread pools the different services will use.
Disk store
CacheManager cacheManager
= CacheManagerBuilder.newCacheManagerBuilder()
.using(PooledExecutionServiceConfigurationBuilder
.newPooledExecutionServiceConfigurationBuilder() // 1
.defaultPool("dflt", 0, 10)
.pool("defaultDiskPool", 1, 3)
.pool("cache2Pool", 2, 2)
.build())
.with(new CacheManagerPersistenceConfiguration(new File(getStoragePath(),
"myData")))
.withDefaultDiskStoreThreadPool("defaultDiskPool") // 2
.withCache("cache1",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class,
String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(10, EntryUnit.ENTRIES)
.disk(10L, MemoryUnit.MB)))
.withCache("cache2",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class,
String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(10, EntryUnit.ENTRIES)
.disk(10L, MemoryUnit.MB))
.withDiskStoreThreadPool("cache2Pool", 2)) // 3
.build(true);
Cache<Long, String> cache1 =
cacheManager.getCache("cache1", Long.class, String.class);
Cache<Long, String> cache2 =
cacheManager.getCache("cache2", Long.class, String.class);
cacheManager.close();
1 | Configure the thread pools. Note that the default one (dflt) is required for the events even when no event listener is configured. |
2 | Tell the CacheManagerBuilder to use a default thread pool for all disk stores that don't explicitly specify one. |
3 | Tell the cache to use a specific thread pool for its disk store. |
Write Behind
CacheManager cacheManager
= CacheManagerBuilder.newCacheManagerBuilder()
.using(PooledExecutionServiceConfigurationBuilder.
newPooledExecutionServiceConfigurationBuilder() // 1
.defaultPool("dflt", 0, 10)
.pool("defaultWriteBehindPool", 1, 3)
.pool("cache2Pool", 2, 2)
.build())
.withDefaultWriteBehindThreadPool("defaultWriteBehindPool") // 2
.withCache("cache1",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class,
String.class, ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10,
EntryUnit.ENTRIES))
.withLoaderWriter(new SampleLoaderWriter<Long, String>(
singletonMap(41L, "zero")))
.add(WriteBehindConfigurationBuilder
.newBatchedWriteBehindConfiguration(1, TimeUnit.SECONDS, 3)
.queueSize(3)
.concurrencyLevel(1)))
.withCache("cache2",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class,
String.class, ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10,
EntryUnit.ENTRIES))
.withLoaderWriter(new SampleLoaderWriter<Long, String>(
singletonMap(41L, "zero")))
.add(WriteBehindConfigurationBuilder
.newBatchedWriteBehindConfiguration(1, TimeUnit.SECONDS, 3)
.useThreadPool("cache2Pool") // 3
.queueSize(3)
.concurrencyLevel(2)))
.build(true);
Cache<Long, String> cache1 =
cacheManager.getCache("cache1", Long.class, String.class);
Cache<Long, String> cache2 =
cacheManager.getCache("cache2", Long.class, String.class);
cacheManager.close();
1 | Configure the thread pools. Note that the default one (dflt) is required for the events even when no event listener is configured. |
2 | Tell the CacheManagerBuilder to use a default thread pool for all write-behind caches that don't explicitly specify one. |
3 | Tell the WriteBehindConfigurationBuilder to use a specific thread pool for its write-behind work. |
Events
CacheManager cacheManager
= CacheManagerBuilder.newCacheManagerBuilder()
.using(PooledExecutionServiceConfigurationBuilder
.newPooledExecutionServiceConfigurationBuilder() // 1
.pool("defaultEventPool", 1, 3)
.pool("cache2Pool", 2, 2)
.build())
.withDefaultEventListenersThreadPool("defaultEventPool") // 2
.withCache("cache1",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class,
String.class, ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10,
EntryUnit.ENTRIES))
.add(CacheEventListenerConfigurationBuilder
.newEventListenerConfiguration(new ListenerObject(),
EventType.CREATED, EventType.UPDATED)))
.withCache("cache2",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class,
String.class, ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10,
EntryUnit.ENTRIES))
.add(CacheEventListenerConfigurationBuilder
.newEventListenerConfiguration(new ListenerObject(),
EventType.CREATED, EventType.UPDATED))
.withEventListenersThreadPool("cache2Pool")) // 3
.build(true);
Cache<Long, String> cache1 =
cacheManager.getCache("cache1", Long.class, String.class);
Cache<Long, String> cache2 =
cacheManager.getCache("cache2", Long.class, String.class);
cacheManager.close();
1 | Configure the thread pools. Note that there is no default one so all thread-using services must be configured with explicit defaults. |
2 | Tell the CacheManagerBuilder to use a default thread pool to manage events of all caches that don't explicitly specify one. |
3 | Tell the CacheEventListenerConfigurationBuilder to use a specific thread pool for sending its events. |