Enterprise Java

Random JCache stuff: multiple Providers and JMX beans

JCache (JSR 107) is the Java standard for Caching… enough said. No more introductory stuff.

This is a quick fire post which talks about

  • Multiple JCache provider configurations, and
  • Feature: JCache stats via JMX Mbeans

Managing multiple JCache providers

In case you are dealing with a single JCache provider, javax.jcache.Caching.getCachingProvider() returns an instance of the one and only CachingProvider on your classpath.

If you have multiple JCache implementations on your application class path, an attempt at using the above snippet to bootstrap your JCache provider will greet you with the following exception (which is surprisingly friendly !)

javax.cache.CacheException: Multiple CachingProviders have been configured when only a single CachingProvider is expected

Overloading to the rescue!

There are overloaded versions of the getCachingProvider method, one of which allows you to specify the fully qualified class name of a specific JCache provider implementation. The exact class name would be provided as a part of your JCache vendor documentation e.g. com.tangosol.coherence.jcache.CoherenceBasedCachingProvider and com.hazelcast.cache.HazelcastCachingProvider are the provider classes for Oracle Coherence and Hazelcast respectively.

This would work just fine:

CachingProvider coherenceJCacheProvider = Caching.getCachingProvider(“com.tangosol.coherence.jcache.CoherenceBasedCachingProvider”).getCacheManager()

You can also grab the same from the META-INF/services/javax.cache.spi.CachingProvider of the JCache provider JAR file.

JCache Provider SPI Configuration
JCache Provider SPI Configuration

JMX statistics

JCache offers configuration and run time performance statistic for free! This is driven by provider specific implementations.

  • javax.cache.management.CacheMXBean – make sure you enable this by calling setManagementEnabled(true) on the JCache MutableConfiguration object
  • javax.cache.management.CacheStatisticsMXBean – – make sure you enable this by calling setStatisticsEnabled(true) on the JCache MutableConfiguration object

Example snippet

MutableConfiguration config = new MutableConfiguration().setManagementEnabled(true).setStatisticsEnabled(true);

Introspect the Mbeans from JConsole or any equivalent client

JCache Configuration stats
JCache Configuration stats

 
JCache runtime performance stats
JCache runtime performance stats

Nice ha ?

Cheers! :-)

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button