Over a million developers have joined DZone.

Random JCache stuff: multiple Providers and JMX beans

DZone's Guide to

Random JCache stuff: multiple Providers and JMX beans

How to deal with multiple caching providers

· Performance Zone
Free Resource

Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.

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

Image title

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 callingsetManagementEnabled(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 ?

Learn tips and best practices for optimizing your capacity management strategy with the Market Guide for Capacity Management, brought to you in partnership with BMC.

java ,cache ,jmx

Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}