Access WSO2 Server MBeans Programmatically

Java Management Extensions (JMX) is a Java technology that supplies tools for managing and monitoring applications, system objects, devices (e.g. printers) and service-oriented networks. Those resources are represented by objects called MBeans (for Managed Bean).

A managed bean - sometimes simply referred to as an MBean - is a type of JavaBean, created with dependency injection. Managed Beans are particularly used in the Java Management Extensions technology.

The MBean represents a resource running in the Java virtual machine, such as an application or a Java EE technical service (transactional monitor, JDBC driver, etc.). They can be used for collecting statistics on concerns like performance, resources usage, or problems (pull); for getting and setting application configurations or properties (push/pull); and notifying events like faults or state changes (push).

WSO2 servers expose important server information and also management capabilities through MBeans. Let us take WSO2 ESB for an example.

Getting information on MBeans

We can do this by starting jconsole. Start WSO2 server, start jconsole (just type jconsole on command line. It comes with JDK), and connect to "Carbon Boostrap" process.





If you are connecting to a remote server, give 
hostname = host name of the remote machine or ip
port = 9999 (if there is no port offset)
username and password = a valid user who has privileges.

Navigate to MBeans tab



For an example, let's get attributes of the PassThrough http-listener. 

Sample code


 Following java code get information from the server and print the current values. 

import javax.management.remote.JMXServiceURL;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) {
        try
        {
            JMXServiceURL target = new JMXServiceURL
                    ("service:jmx:rmi://localhost:11111/jndi/rmi://localhost:9999/jmxrmi");

            //for passing credentials for password
            Map env = new HashMap();
            String[] credentials = {"admin", "admin"};
            env.put(JMXConnector.CREDENTIALS, credentials);

            JMXConnector connector = JMXConnectorFactory.connect(target, env);
            MBeanServerConnection remote = connector.getMBeanServerConnection();

            //Find this from jConsole
            ObjectName bean = new ObjectName("org.apache.synapse:Type=Transport,Name=passthru-http-receiver");

            MBeanInfo info = remote.getMBeanInfo(bean);
            MBeanAttributeInfo[] attributes = info.getAttributes();
            for (MBeanAttributeInfo attr : attributes)
            {
                System.out.println(attr.getName() + " " + remote.getAttribute(bean,attr.getName()));
            }
            connector.close();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
            System.exit(0);
        }
    }
}


Note how MBean object is found using JConsole.

Java provides some standard MBands to monitor memory/cpu consuption etc. You can get more information on server MBeans provided by WSO2 in this document.



Hasitha Hiranya

No comments:

Post a Comment

Instagram