Daniel
Daniel

Reputation: 256

Ehcache data view via JMX

Is there any way how to view data/objects which are stored in ehcache via JMX? I found just cache statistics but I need to view objects structure.

Upvotes: 4

Views: 1469

Answers (1)

AlphaGeek
AlphaGeek

Reputation: 392

If this is something you need you could build it but it is not available in the Ehcache JMX implementation. I would not expect it to be either since it is unknown if the objects stored in an ehcache are displayable through JMX. If you know that they are, you could certainly create an MBean that, given a reference to the ehcache CacheManager or Cache, could expose the content of the cache.

Keep in mind that unless you are using a memory only cache, there will be objects in the cache that are not in memory but on disk or, if you are using terracotta, they may be on the remote server. Also, it is sometimes more efficient to store java objects in their serialized form. If you are doing that, viewing the data will require a deserialization.

If you are only interested in seeing these objects when you are debugging an issue, I would consider just relying on a Debugger like the one available in good IDEs. Both NetBeans and Eclipse have capable debuggers that can be used to view the content of the cache. I have done it frequently.

Since you tagged this question with "spring" I assume you are using spring. Creating an MBean in spring is super easy. You just add the exporter bean to the context and make your MBean implement an interface named the same as your object but with MBean added to the end. Here's 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 exposed as a JMX MBean that will have an attribute named "name" and an operation named "printName". The name of the MBean will be: "FOO:name=foo". You can customize all of that behavior. See: http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/htmlsingle/#jmx-exporting

Upvotes: 1

Related Questions