Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Transform incident management with machine learning and analytics to help you maintain optimal performance and availability while keeping pace with the growing demands of digital business with this eBook, 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 ?

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.

Topics:
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.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}