Skip to content

Viewing and Modifying Configuration Defaults Using a JMX MBean Client

Bill Davidson edited this page Aug 22, 2016 · 15 revisions

With a JMX MBean client, you can view and modify the values of most of JSONUtil's defaults while your application is running. There are a few additional operations available such as flushing the reflection cache and listing the classes that are currently selected for reflection.

There are a few MBean clients available. I prefer jvisualvm which has been packaged with the standard JDK since Java 6 update 7. Some people prefer the older jconsole which is also packaged with the standard JDK. There is also Java Mission Control which has been distributed with the JDK since Java 7 update 40. There are others from other sources. Some clients (like jvisualvm) require you to load a plugin in order to support MBeans. You'll need to do that before you can access the MBeans with it.

You should probably set an appName in your environment to avoid MBean collisions when multiple webapps use this library in the same web tier container at the same time. See the JNDI Wiki page or the JSONConfigDefaults API page for more information how to do that.

In the examples below, jvisualvm is looking at a JSONConfigDefaultsMBean running locally in my JUnit tests from Eclipse. My JUnit tests set appName=TestJSONUtil in JNDI, which is why that's showing up in jvisualvm.

Attribute Defaults (mostly booleans) in Java VisualVM

flags

Operations in in Java VisualVM

operations

Since MBeans can expose serious security risks, web tier containers provide authentication schemes which you have to get past before you can access the MBean server on remote systems. See your server software's documentation for how to set that up and use it.

Once you're connected and open the tab/page for MBeans, JSONUtil's defaults can be viewed and modified by navigating to org.kopitubruk.util.json.JSONConfigDefaults. From there you can select properties to view or modify or operations to run.

You should set up a ServletContextListener for your webapp in order to remove the MBean when your application is removed or reloaded within the web server. Otherwise the MBean sticks around which not only wastes memory but prevents the new one from being loaded when you reload the webapp. Here's an example called "AppCleanUp"

package org.myDomain.web.app;

import javax.servlet.ServletContextListener;
import org.kopitubruk.util.json.JSONConfigDefaults;

public class AppCleanUp implements ServletContextListener
{
    public void contextDestroyed( ServletContextEvent sce )
    {
        JSONConfigDefaults.clearMBean();
    }
}

Then in the web.xml for your webapp, you should add the following:

<listener>
    <listener-class>org.myDomain.web.app.AppCleanUp</listener-class>
</listener>

When your web tier container unloads your webapp, it will call the contextDestroyed method and that will remove the JSONConfigDefaults MBean from your server.