+

Search Tips   |   Advanced Search

Examples of accessing MBean attributes and operations

We can use the Liberty profile to access the attributes, and call the operations, of Java Management Extensions (JMX) management beans (MBeans).

After you obtain an MBeanServer instance (for an application running on the Liberty profile) or an MBeanServerConnection instance (for an external client), we can access the attributes or call the operations of MBeans provided by the Liberty profile. See Work with JMX MBeans .

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". For the names of the management interfaces, see List of provided MBeans.


Example 1: 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();
}


Example 2: 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();
}


Example 3: 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");
}


Example 4: Query status of Web Service Endpoint

import javax.management.ObjectName;
import javax.management.MBeanServerConnection;
import javax.management.MBeanInfo;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanOperationInfo;

...

// Init mbs as needed
MBeanServerConnection mbs;

// Get MBeanInfo for specific ObjectName
ObjectName objName = new ObjectName("WebSphere:feature=jaxws,bus.id=testCXFJMXSupport-Server-Bus, type=Bus.Service.Endpoint,service=\"{http://jaxws.samples.ibm.com.jmx/}TestEndpointService\", port=\"TestEndpoint\",instance.id=1816106538");
MBeanInfo beanInfo = mbsc.getMBeanInfo(objName);

// Go through attributes to find the interested one for (MBeanAttributeInfo attr : beanInfo.getAttributes()) {
 if (attr.getName().equals("State")) {
     String status = String.valueOf(mbs.getAttribute(objName, attr.getName()));
  break;
 }
}


Example 5: Shut down the CXF server bus

import javax.management.ObjectName;
import javax.management.MBeanServerConnection;
import javax.management.MBeanInfo;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanOperationInfo;

...

// Init mbsc as needed
MBeanServerConnection mbs;

// Get MBeanInfo for specific ObjectName
ObjectName objName = new ObjectName("WebSphere:feature=jaxws,bus.id=testCXFJMXSupport-Server-Bus, type=Bus,instance.id=1618108530");
MBeanInfo beanInfo = mbsc.getMBeanInfo(objName);

// Go through operation to find the interested one and invoke
for (MBeanOperationInfo operation : beanInfo.getOperations()) {
 if (operation.getName().equals("shutdown")) {
  mbs.invoke(objName, operation.getName(), new Object[] { true }, new String[] { boolean.class.getName() });
  break;
 }
}


Parent topic: Work with JMX MBeans