View Ehcache Data Through JMX

Is there a way to view the data / objects that are stored in ehcache through JMX? I found only cache statistics, but I need to view the structure of the objects.

+4
source share
1 answer

If this is what you need, you can create it, but it is not available in the Ehcache JMX implementation. I would not expect this to be either, since it is not known whether the objects stored in ehcache will be displayed through JMX. If you know that this is so, you could create an MBean that, referring to the ehcache CacheManager or Cache, could expose the contents of the cache.

Keep in mind that if you do not use the memory cache, the cache will contain objects that are not in memory, but on disk or, if you use terracotta, they can be on a remote server. In addition, it is sometimes more efficient to store Java objects in their serialized form. If you do, viewing the data will require deserialization.

If you are only interested in seeing these objects when you are debugging a problem, I would think that I am just relying on a debugger, like the one that is available in good IDEs. NetBeans and Eclipse have debuggers that you can use to view the contents of the cache. I have done this often.

Since you marked this question with "spring", I assume you are using spring. Creating an MBean in spring is very simple. You simply add a bean exporter to the context and make your MBean implement an interface with a name just like your object, but with an MBean added to the end. Here is an example:

applicationContext.xml:

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" p:autodetect="true"/> <bean name="FOO:name=foo" class="com.foo.test.Foo"/> 

Foo.java:

 package com.foo.test; public class Foo implements FooMBean { private String name; ... @Override public String getName() { return name; } @Override public void setName(String name) { this.name = name; } @Override public void printName() { System.out.println(name); } } 

FooMBean.java:

 package com.foo.test; public interface FooMBean { public String getName(); public void setName(String name); public void printName(); } 

In this example, the foo object will be displayed as a JMX MBean, which will have an attribute named "name" and an operation named "printName". The MBean name will be: "FOO: name = foo". You can customize all this behavior. See: http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/htmlsingle/#jmx-exporting

+1
source

All Articles