Question

I am very new to Java programming. I have a requirement where i need to connect to Mbean and perform certain tasks like Enable ,disable, count of MBeans. enable method is used to enable the Mbean and disable method is to disable and Count method is used to get the count of Mbean.

In all the 3 case i need to connect to the Mbean server. So i decided to write a generic method to create a connection to the server, so when ever any method called then inside each method i can call the generic method.

The challenge that i am facing in storing different objects and have to return in generic method so that i can use the objects values in disable,enable and count methods.

Please see below code and let me how i can achieve it.

package mbeanappscontrollerws;

import java.util.Hashtable;

import java.util.Iterator;
import java.util.Set;

import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXServiceURL;

import oracle.oc4j.admin.jmx.remote.api.JMXConnectorConstant;

import java.net.MalformedURLException;

import java.io.IOException;

import java.util.regex.Pattern;

import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.ReflectionException;

public class MBeanAppsController {

    public static void main(String[] args) {
        try {
            /*        enable("******", "****", "****",
                     "*****", "****",
                     "***");

            disable("******", "****", "****",
                     "*****", "****",
                     "***");
*/
        } catch (Exception e) {
            // TODO
        }

    }

    public static  MBeanServerConnection  connectMethd(String host, String port, 
                                                     String container, 
                                                     String beanFQDN, 
                                                     String usr, String pwd) {


        /* MBean server URL */

        String mbeanServerURL = 
            new String("service:jmx:rmi:" + "///opmn://" + host + ":" + port + 
                       "/" + container);
        /* credentials for authentication */
        Hashtable cred = new Hashtable();

        cred.put(JMXConnectorConstant.CREDENTIALS_LOGIN_KEY, (Object)usr);
        cred.put(JMXConnectorConstant.CREDENTIALS_PASSWORD_KEY, (Object)pwd);

        /* connection parameters */
        Hashtable env = new Hashtable();
        env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, 
                (Object)"oracle.oc4j.admin.jmx.remote");
        env.put(JMXConnector.CREDENTIALS, (Object)cred);
        /*System.out.println("env " + env);*/
        JMXServiceURL jmxServerURL = null;
        JMXConnector jmxConn = null;

        MBeanServerConnection serverConn = null;

        try {

            /* connect to the MBean Server */
            System.out.println("mbeanServerURL " + mbeanServerURL);

            jmxServerURL = new JMXServiceURL(mbeanServerURL);
            System.out.println("jmxServerURL " + jmxServerURL);
            try {
                jmxConn = JMXConnectorFactory.connect(jmxServerURL, env);


                serverConn = jmxConn.getMBeanServerConnection();
            } catch (IOException e) {
                System.out.println(e);
            }

        }

        catch (MalformedURLException e) {
        }
        return serverConn;
    }



    public static String[] enable(String host, String port, String container, 
                                  String beanFQDN, String usr, String pwd) {


        String Output[] = new String[3];
        try {
            MBeanServerConnection serverConn = 
                connectMethd(host, port, container, beanFQDN, usr, pwd);

            /* retrieve mbean app using the FQDN */

            ObjectName mbeanName = new ObjectName(beanFQDN);
            /* enable the MBean App */
            serverConn.invoke(mbeanName, "startUp", null, null);

            /* retrieve the running status of the MBean App after enabling */
            Boolean mbeanAppRunning = 
                Boolean.valueOf(serverConn.getAttribute(mbeanName, 
                                                        "Running").toString());
            String beanAppStatus = "DISABLED";
            if (mbeanAppRunning.booleanValue())
                beanAppStatus = "ENABLED";

            /* pattern to extract mbean application names */
            Pattern mbeanAppPat = 
                Pattern.compile("name=", Pattern.CASE_INSENSITIVE);
            String beanName = mbeanAppPat.split(beanFQDN)[1];
            Output[0] = beanName;
            Output[1] = beanFQDN;
            Output[2] = beanAppStatus;

        } catch (MalformedObjectNameException e) {

            e.getStackTrace();
        } catch (InstanceNotFoundException e) {

            e.getStackTrace();
        } catch (AttributeNotFoundException e) {

            e.getStackTrace();
        } catch (ReflectionException e) {

            e.getStackTrace();
        } catch (MBeanException e) {
            e.getStackTrace();
        } catch (IOException ioe) {
            System.out.println(ioe);
        }

        System.out.println("Enable Method OutPut " + Output);
        return Output;


    }

    public static String[] disable(String host, String port, String container, 
                                   String beanFQDN, String usr, String pwd) {

        String Output[] = new String[3];
        try {
            MBeanServerConnection serverConn = 
                connectMethd(host, port, container, beanFQDN, usr, pwd);

            /* retrieve mbean app using the FQDN */

            ObjectName mbeanName = new ObjectName(beanFQDN);
            /* enable the MBean App */
            serverConn.invoke(mbeanName, "shutDown", null, null);

            /* retrieve the running status of the MBean App after enabling */
            Boolean mbeanAppRunning = 
                Boolean.valueOf(serverConn.getAttribute(mbeanName, 
                                                        "Running").toString());
            String beanAppStatus = "DISABLED";
            if (mbeanAppRunning.booleanValue())
                beanAppStatus = "ENABLED";

            Pattern mbeanAppPat = 
                Pattern.compile("name=", Pattern.CASE_INSENSITIVE);
            String beanName = mbeanAppPat.split(beanFQDN)[1];
            /* debug */
            System.out.println("Finished executing 'disable' operation.");
            Output[0] = beanName;
            Output[1] = beanFQDN;
            Output[2] = beanAppStatus;

        } catch (MalformedObjectNameException e) {

            e.getStackTrace();
        } catch (InstanceNotFoundException e) {

            e.getStackTrace();
        } catch (AttributeNotFoundException e) {

            e.getStackTrace();
        } catch (ReflectionException e) {

            e.getStackTrace();
        } catch (MBeanException e) {
            e.getStackTrace();
        } catch (IOException ioe) {
            System.out.println(ioe);
        }

        System.out.println("Disable Method OutPut " + Output);
        return Output;

    }

    public static int MBeancount(String host, String port, String container, 
                                 String filter, String usr, String pwd) {
        int mbeanAppsCount = 0;

        try {
            MBeanServerConnection serverConn = 
                connectMethd(host, port, container, filter, usr, pwd);

            ObjectName mbeanDomainName = new ObjectName(filter);
            Set mbeanAppNames = serverConn.queryNames(mbeanDomainName, null);
            /* retrieve mbean apps count */
            Iterator i;
            for (i = mbeanAppNames.iterator(); i.hasNext(); 
                 i.next(), mbeanAppsCount++)
                ;

        } catch (MalformedObjectNameException e) {
            /* debug */
            System.out.println(e);
        } catch (IOException ioe) {
            System.out.println(ioe);
        } finally {
            /* debug */
            System.out.println("End: MBeanAppsController-Java-getMBeanAppsCount");

            try {
                /* close server connection */
                if (jmxConn != null) {
                    jmxConn.close();
                } else {
                }
            } catch (IOException e) {
                /* debug */
                System.out.println(e);
            }

        }
        System.out.println("mbeanAppsCount" + mbeanAppsCount);
        return mbeanAppsCount;
    }
}

In above code under final block i am not able to access jmxconn variable from the method connect because it is derived from JMXconnector but in generic method i am returning MBeanServerConnection type. In my case i want both the object type MBeanServerConnection and JMXconnector values to be returned. If some one can help me then it would be a great help.

Regards, TJ.

Was it helpful?

Solution

An ugly/simple solution is to make the connectMethd(sic) method return an array containing serverConn and jmxConn:

Change its declaration and return statements, as bellow:

public static  Object[]  connectMethd(String host, String port, 
                                      String container, 
                                      String beanFQDN, 
                                      String usr, String pwd){
    // same code as before here...

    return new Object[]{serverConn,jmxConn};
}

When calling connectMethd, put the result in an Object[] variable, and cast its elements at indexes 0 and 1 to the appropriate types. Examples:

Object[] connections = connectMethd(host, port, container, beanFQDN, usr, pwd);

MBeanServerConnection serverConn = (MBeanServerConnection) connections[0];
JMXConnector jmxConn = (JMXConnector) connections[1];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top