1

I was looking at this SO question, but here I only want Caffeine to start reporting to JMX.

I have added an application.conf file as such and referenced it via -Dconfig.file:

caffeine.jcache {
  # A named cache is configured by nesting a new definition under the caffeine.jcache namespace. The
  # per-cache configuration is overlaid on top of the default configuration.
  default {
    # The monitoring configuration
    monitoring {
      # If JCache statistics should be recorded and externalized via JMX
      statistics = true

      # If the configuration should be externalized via JMX
      management = true
    }
}

It is not working, but I suspect it might be related to jcache, but not sure what is the expected way to implement this basic monitoring.

Alain P
  • 1,293
  • 8
  • 16

1 Answers1

1

The cache instance is registered with the MBean server when it is instantiated by the CacheManager. The following test uses the programmatic api for test simplicity.

public final class JmxTest {

  @Test
  public void jmx() throws MalformedObjectNameException {
    var config = new CaffeineConfiguration<>();
    config.setManagementEnabled(true);
    config.setStatisticsEnabled(true);

    var manager = Caching.getCachingProvider().getCacheManager();
    var cache = manager.createCache("jmx", config);
    cache.put(1, 2);

    var server = ManagementFactory.getPlatformMBeanServer();
    String name = String.format("javax.cache:type=%s,CacheManager=%s,Cache=%s",
        "CacheStatistics", manager.getURI().toString(), cache.getName());
    var stats = JMX.newMBeanProxy(server, new ObjectName(name), CacheStatisticsMXBean.class);
    assertThat(stats.getCachePuts()).isEqualTo(1);
  }
}

If you do not need JCache for an integration then you will likely prefer to use the native APIs and metrics library. It is supported by Micrometer, Dropwizard Metrics, and the Prometheus client. While JCache is great for framework integrations, its api is rigid and cause surprising performance issues.

Ben Manes
  • 9,178
  • 3
  • 35
  • 39
  • Thanks and sorry for the long delay, was aiming for JMX and started work on OpenTelemetry and now wondering if best approach is with Prometheus – Alain P Dec 05 '21 at 11:16
  • @AlainP fwiw, at work I use Micrometer and the Caffeine binder, which is exported to Prometheus. There is a JCache binder as well, but again I do not recommend that framework for normal application usage as I consider it too problematic. – Ben Manes Dec 05 '21 at 17:50
  • Not using JCache now, and having been warned I will surely not go that way. – Alain P Dec 08 '21 at 09:40