存储不同的对象并以方法形式返回

时间:2014-05-08 19:25:48

标签: java java-ee

我是Java编程的新手。我有一个要求,我需要连接到Mbean并执行某些任务,如启用,禁用,MBean计数。 enable方法用于启用Mbean,disable方法用于禁用,Count方法用于获取Mbean的计数。

在所有3种情况下,我需要连接到Mbean服务器。所以我决定编写一个通用方法来创建与服务器的连接,所以当在每个方法中调用任何方法时,我可以调用泛型方法。

我在存储不同对象时遇到的挑战,必须以泛型方法返回,以便我可以在禁用,启用和计数方法中使用对象值。

请参阅下面的代码,让我知道如何实现它。

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;
    }
}

在最后一个块下面的代码中,我无法从方法connect访问jmxconn变量,因为它是从JMXconnector派生的,但在泛型方法中我返回MBeanServerConnection类型。在我的情况下,我想要返回对象类型MBeanServerConnection和JMXconnector值。如果有人可以帮助我,那将是一个很大的帮助。

此致 TJ。

1 个答案:

答案 0 :(得分:1)

一个丑陋/简单的解决方案是让connectMethd(sic)方法返回一个包含serverConnjmxConn的数组:

更改其声明和返回语句,如下所示:

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};
}

调用connectMethd时,将结果放在Object[]变量中,并将索引0和1的元素转换为适当的类型。例子:

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

MBeanServerConnection serverConn = (MBeanServerConnection) connections[0];
JMXConnector jmxConn = (JMXConnector) connections[1];