WAS v8.5 > Reference > Developer examples

Liberty profile: Examples of accessing MBean attributes and operations

This topic demonstrates some examples of accessing the attributes operations of JvmStats MXBeans on the Liberty profile.

Once you obtain either...

...we can access the attributes or call the operations of JMX MBeans provided by the Liberty Profile.

The following code examples assume the variable mbs is an MBeanServer or MBeanServerConnection instance. We can use the provided methods to access the attributes and operations in a similar way to Java reflection. Alternatively, each MBean has a management interface with getter methods for the attributes and methods for the operations. We can use these interfaces via one of the javax.managementJMX.newMBeanProxy methods or one of the javax.management.JMX.newMXBeanProxy methods for MXBeans to obtain a proxy object. The name of a management interface ends with MXBean.


Check the state of application "myApp"

import javax.management.ObjectName;
import javax.management.JMX;
import com.ibm.websphere.application.ApplicationMBean;
...

ObjectName myAppMBean = new ObjectName(
"WebSphere:service=com.ibm.websphere.application.ApplicationMBean,name=myApp");
if (mbs.isRegistered(myAppMBean)) 
{
 String state = (String) mbs.getAttribute(myAppMBean, "State"); 
 // alternatively, obtain a proxy object  ApplicationMBean app = JMX.newMBeanProxy(mbs, myAppMBean, ApplicationMBean.class);
 state = app.getState();
}


Get response time statistics for servlet “Example Servlet” from application “myApp”

import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import javax.management.JMX;
import com.ibm.websphere.webcontainer.ServletStatsMXBean;

...

ObjectName servletMBean = new ObjectName("WebSphere:type=ServletStats,name=myApp.Example Servlet");
if (mbs.isRegistered(servletMBean)) 
{
 CompositeData responseTimeDetails = (CompositeData) mbs.getAttribute(servletMBean, "ResponseTimeDetails");
 CompositeData responseTimeReading = (CompositeData) responseTimeDetails.get("reading");
 Double mean = (Double) responseTimeReading.get("mean");
 Double standardDeviation = (Double) responseTimeReading.get("standardDeviation");
 // alternatively, obtain a proxy object  ServletStatsMXBean servletStats = JMX.newMXBeanProxy(mbs, servletMBean, ServletStatsMXBean.class);
 StatisticsMeter meter = servletStats.getResponseTimeDetails();
 StatisticsReading reading = meter.getReading();
 mean = reading.getMean();
 standardDeviation = reading.getStandardDeviation();
}


Create a web server plug-in configuration file

import com.ibm.websphere.webcontainer.GeneratePluginConfigMBean;

...

ObjectName pluginMBean = new ObjectName("WebSphere:name=com.ibm.ws.jmx.mbeans.generatePluginConfig");
if (mbs.isRegistered(pluginMBean)) 
{
    mbs.invoke(pluginMBean, "generatePluginConfig", new Object[] 
    {
        "installRoot", "serverName"
    }, new String[] 
    {
    String.class.getName(), String.class.getName() 
    });

 // alternatively, use a proxy object  GeneratePluginConfigMBean plugin = JMX.newMBeanProxy(mbs, name, GeneratePluginConfigMBean.class);
 plugin.generatePluginConfig("installRoot", "serverName");
}


Parent topic:

Work with JMX MBeans on the Liberty profile