Accessing / Connecting WebLogic MBeans Server Services, Properties with Java Management Extensions (JMX)

Table Of Content

  1. Accessing WebLogic MBeans using JMX
  2. Resources
  3. Feedback

Accessing WebLogic MBeans using JMX

Following java doc describes high level of information about WebLogic MBeans Server services, properties and how MBeans object name is constructed.

Following code describes how to access WebLogic Server MBeans services, properties with Java Management Extensions (JMX).

/**
 * Copyright (c) 2008 - 2015 Bhaveshkumar Thaker [http://bhaveshthaker.com].  All rights reserved.
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;

/**
 * <p>
 * JNDI Names for WebLogic MBean Servers:
 * </p>
 * <p>
 * <table>
 * <tbody>
 * <tr>
 * <td>MBean Server</td>
 * <td>JNDI Name</td>
 * </tr>
 * <tr>
 * <td>Domain Runtime MBean Server</td>
 * <td>weblogic.management.mbeanservers.domainruntime</td>
 * </tr>
 * <tr>
 * <td>Runtime MBean Server</td>
 * <td>weblogic.management.mbeanservers.runtime</td>
 * </tr>
 * <tr>
 * <td>Edit MBean Server</td>
 * <td>weblogic.management.mbeanservers.edit</td>
 * </tr>
 * </tbody>
 * </table>
 * </p>
 * <p>
 * Choosing an MBean Server:
 * </p>
 * <p>
 * <ul>
 * <li>To modify the configuration of the domain, use the Edit MBean Server.</li>
 * <li>To monitor changes to the pending hierarchy of configuration MBeans, use the Edit MBean
 * Server.</li>
 * <li>To monitor only active configuration MBeans (and not runtime MBeans), use a Runtime MBean
 * Server.</li>
 * </ul>
 * </p>
 * <p>
 * Monitoring through a Runtime MBean Server requires less memory and network traffic than
 * monitoring through the Domain Runtime MBean Server. (WebLogic Server does not initialize the
 * Domain Runtime MBean Server until a client requests a connection to it.)
 * </p>
 * <p>
 * The run-time MBean model is organized hierarchically; however, each registered MBean in the
 * system has an MBean ObjectName and can be fetched or queried directly if necessary. The run-time
 * MBeans follow ObjectName conventions to simplify registration, lookup, and querying. An
 * ObjectName for an MBean instance is defined as follows:
 * </p>
 * <p>
 * <code>{domain}:{key}={value}[,{keyN}={valueN}]*</code>
 * </p>
 * <p>
 * You can provide any number of additional key-value pairs to uniquely identify the MBean instance.
 * </p>
 * <p>
 * Each of the run-time MBean ObjectNames contains the following keys:
 * </p>
 * <p>
 * <code>type</code> - Short type name of the resource managed by the MBean.
 * </p>
 * <p>
 * <code>id</code> - The id value of the resource managed by the MBean.
 * </p>
 * <p>
 * </p>
 * <code></code>
 *
 * @author <a href="http://bhaveshthaker.com/">Bhavesh Thaker</a>
 *
 */
public class WeblogicJMXMBeanLookup
{

    /**
     * @param args
     */
    public static void main(final String[] args)
    {
        JMXConnector jMXConnector = null;

        try
        {
            Map<String, Object> attributesDataMap = new HashMap<String, Object>();
            List<Object> domainsDataList = new ArrayList<Object>();

            final String hostname = "127.0.0.1";
            final String portString = "7001";
            final String username = "weblogic";
            final String password = "weblogic";

            final WeblogicJMXMBeanLookup weblogicJMXMBeanLookup = new WeblogicJMXMBeanLookup();
            jMXConnector = weblogicJMXMBeanLookup.getJMXConnector(hostname, portString, username,
                    password);
            final MBeanServerConnection mBeanServerConnection = weblogicJMXMBeanLookup
                    .getMBeanServerConnection(jMXConnector);

            domainsDataList = weblogicJMXMBeanLookup.getDomainsData(mBeanServerConnection);
            System.out.println("Weblogic Domains list: " + domainsDataList);

            final MBeanInfo mBeanInfo = weblogicJMXMBeanLookup.getMBeanInfoData(
                    mBeanServerConnection,
                    "com.bea:ServerRuntime=AdminServer,Name=AdminServer,Type=JRockitRuntime");
            System.out.println("MBeanInfo: " + mBeanInfo.toString());
            final List<MBeanAttributeInfo> mBeanAttributeInfoList = new ArrayList<MBeanAttributeInfo>(
                    Arrays.asList(mBeanInfo.getAttributes()));
            for (final MBeanAttributeInfo mBeanAttributeInfo : mBeanAttributeInfoList)
            {
                System.out.println("MBeanAttributeInfo: " + mBeanAttributeInfo.toString());
            }
            final List<MBeanOperationInfo> mBeanOperationInfoList = new ArrayList<MBeanOperationInfo>(
                    Arrays.asList(mBeanInfo.getOperations()));
            for (final MBeanOperationInfo mBeanOperationInfo : mBeanOperationInfoList)
            {
                System.out.println("MBeanOperationInfo: " + mBeanOperationInfo.toString());
            }

            attributesDataMap = weblogicJMXMBeanLookup.getAttributesData(mBeanServerConnection,
                    "com.bea:ServerRuntime=AdminServer,Name=AdminServer,Type=JRockitRuntime",
                    new String[] { "FreeHeap", "FreePhysicalMemory", "GCHandlesCompaction",
                            "GcAlgorithm" });
            System.out.println("Attributes Data: " + attributesDataMap);

            // Object[] params = new Object[] {"MyTest"};
            // String[] signature = new String[] {"java.lang.String"};
            weblogicJMXMBeanLookup.invokeOperation(mBeanServerConnection,
                    "com.bea:ServerRuntime=AdminServer,Name=AdminServer,Type=JRockitRuntime",
                    "runGC", null, null);
        }
        catch (final Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (jMXConnector != null)
            {
                try
                {
                    jMXConnector.close();
                }
                catch (final IOException e)
                {
                    e.printStackTrace();
                }
            }
            jMXConnector = null;
        }
    }

    public WeblogicJMXMBeanLookup()
    {
    }

    public LinkedHashMap<String, Object> getAttributesData(
            final MBeanServerConnection mBeanServerConnection, final String mBeanServiceObjectName,
            final String[] attributesArray) throws InstanceNotFoundException, ReflectionException,
            IOException, MalformedObjectNameException, NullPointerException
    {
        final LinkedHashMap<String, Object> attributesDataMap = new LinkedHashMap<String, Object>();

        final ObjectName objectName = new ObjectName(mBeanServiceObjectName);
        final AttributeList attributesList = mBeanServerConnection.getAttributes(objectName,
                attributesArray);
        final List<Attribute> attributeList = attributesList.asList();
        for (final Attribute attribute : attributeList)
        {
            attributesDataMap.put(attribute.getName(), attribute.getValue());
        }

        return attributesDataMap;
    }

    public LinkedList<Object> getDomainsData(final MBeanServerConnection mBeanServerConnection)
            throws IOException
    {
        final LinkedList<Object> domainsDataList = new LinkedList<Object>();

        final String domains[] = mBeanServerConnection.getDomains();
        Arrays.sort(domains);
        for (final String domainString : domains)
        {
            domainsDataList.add(domainString);
        }

        return domainsDataList;
    }

    public JMXConnector getJMXConnector(final String hostname, final String portString,
            final String username, final String password) throws IOException
    {
        JMXConnector jMXConnector = null;

        final String protocol = "t3";
        final String jndiroot = "/jndi/";
        final String mbeanServer = "weblogic.management.mbeanservers.runtime";

        final int port = Integer.parseInt(portString);
        final JMXServiceURL jMXServiceURL = new JMXServiceURL(protocol, hostname, port, jndiroot
                + mbeanServer);

        final Hashtable<String, Object> h = new Hashtable<String, Object>();
        h.put(Context.SECURITY_PRINCIPAL, username);
        h.put(Context.SECURITY_CREDENTIALS, password);
        h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
        h.put("jmx.remote.x.request.waiting.timeout", new Long(10000));

        jMXConnector = JMXConnectorFactory.connect(jMXServiceURL, h);

        return jMXConnector;
    }

    public MBeanInfo getMBeanInfoData(final MBeanServerConnection mBeanServerConnection,
            final String mBeanServiceObjectName) throws MalformedObjectNameException,
            NullPointerException, InstanceNotFoundException, IntrospectionException,
            ReflectionException, IOException
    {
        MBeanInfo mBeanInfo = null;

        final ObjectName objectName = new ObjectName(mBeanServiceObjectName);
        mBeanInfo = mBeanServerConnection.getMBeanInfo(objectName);

        return mBeanInfo;
    }

    public MBeanServerConnection getMBeanServerConnection(final JMXConnector jMXConnector)
            throws IOException
    {
        MBeanServerConnection mBeanServerConnection = null;

        mBeanServerConnection = jMXConnector.getMBeanServerConnection();

        return mBeanServerConnection;
    }

    public void invokeOperation(final MBeanServerConnection mBeanServerConnection,
            final String mBeanServiceObjectName, final String operationName, final Object[] params,
            final String[] signature) throws MalformedObjectNameException, NullPointerException,
            InstanceNotFoundException, MBeanException, ReflectionException, IOException
    {
        final ObjectName objectName = new ObjectName(mBeanServiceObjectName);
        mBeanServerConnection.invoke(objectName, operationName, params, signature);
    }
}

Resources

Feedback

I would like to hear from you! If you liked this tutorial, have some suggestions or even some corrections for me, please let me know. I track all user feedback in comments sections.

Leave a Reply

Your email address will not be published. Required fields are marked *