JMX Code to print Datasource Status

You need to provide the server host name and port number in the JMX code and also set the class path before compiling it.

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;
import org.apache.log4j.Logger;


import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;



public class WLMonitMain {

    private static MBeanServerConnection connection;
    private static JMXConnector connector;
    private static ObjectName service;
    
    static Logger log = Logger.getLogger(WLMonitMain.class.getName());

    static {

        try {

            System.out.println("…");
            service = new ObjectName(
                    "com.bea:Name=RuntimeService,"
                    + "Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
        } catch (MalformedObjectNameException e) {
            throw new AssertionError(e.getMessage());
        }
    }

    public static void initConnection(String hostname, String portString,
            String username, String password) throws IOException,
            MalformedURLException {
        
        String protocol = "t3";
        Integer portInteger = Integer.valueOf(portString);
        int port = portInteger.intValue();
        String jndiroot = "/jndi/";
        String mserver = "weblogic.management.mbeanservers.runtime";
        JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port,
                jndiroot + mserver);
        Hashtable h = new Hashtable();
        h.put(Context.SECURITY_PRINCIPAL, username);
        h.put(Context.SECURITY_CREDENTIALS, password);
        h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
                "weblogic.management.remote");
        connector = JMXConnectorFactory.connect(serviceURL, h);
        connection = connector.getMBeanServerConnection();
    }

    public static void testMethod() {

        try {

           

            ObjectName serverRuntime = (ObjectName) connection.getAttribute(service, "ServerRuntime");
            String name = (String) connection.getAttribute(serverRuntime,"Name");
           
            ObjectName jvmRuntime = new ObjectName("com.bea:ServerRuntime="+name+",Name="+name+",Type=JVMRuntime");
            
            System.out.println("Heap Free Percent"+connection.getAttribute(jvmRuntime, "HeapFreePercent"));
            log.debug("Heap Free Percent"+connection.getAttribute(jvmRuntime, "HeapFreePercent"));
            
            ObjectName jdbcRuntime= new ObjectName("com.bea:ServerRuntime="+name+",Name="+name+",Type=JDBCServiceRuntime");
            
            ObjectName[] dataSourceArray = (ObjectName[]) connection.getAttribute(jdbcRuntime,"JDBCDataSourceRuntimeMBeans");
            
             for (int j = 0; j < dataSourceArray.length; j++){   
			  
			   String poolName = (String)connection.getAttribute(dataSourceArray[j], "Name");
			   System.out.println("poolName"+poolName);
                           
                           ObjectName jdbcDatasourceRuntime = new ObjectName("com.bea:ServerRuntime="+name+",Name="+poolName+",Type=JDBCDataSourceRuntime");
			   System.out.println("Datasource Status "+connection.getAttribute(jdbcDatasourceRuntime, "State"));
                           log.info("Datasource Status "+connection.getAttribute(jdbcDatasourceRuntime, "State"));
                   
	      }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public static void main(String[] args) throws Exception {

        String hostname = "localhost";
        String portString = "7001";
        String username = "weblogic";
        String password = "weblogic123";
        System.out.println("initializing connection");
        initConnection(hostname, portString, username, password);
        WLMonitMain.testMethod();
        connector.close();
    }

}

 

 

 

Leave a Reply