Class JMXUtils


  • public final class JMXUtils
    extends java.lang.Object
    Various JMX-related utility functions. FIXME: Use generic RegistryContextFactory instead of Oracle specific.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static org.slf4j.Logger log
      The logger.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object executeCommand​(javax.management.MBeanServerConnection connection, java.lang.String beanName, java.lang.String command, java.lang.String... arguments)
      Execute a command on a bean.
      static java.lang.Object executeCommand​(javax.management.remote.JMXConnector connector, java.lang.String beanName, java.lang.String command, java.lang.String... arguments)
      Execute a single command, closing the connector afterwards.
      static java.lang.Object getAttribute​(java.lang.String beanName, java.lang.String attribute, javax.management.MBeanServerConnection connection)
      Get the value of an attribute from a bean.
      static java.lang.Object getAttribute​(javax.management.remote.JMXConnector connector, java.lang.String beanName, java.lang.String attribute)
      Get the value of an attribute, closing the connector afterwards.
      static javax.management.ObjectName getBeanName​(java.lang.String beanName)
      Get a bean name from a string version.
      static javax.management.remote.JMXConnector getJMXConnector​(java.lang.String hostName, int jmxPort, java.lang.String login, java.lang.String password)
      Get a JMXConnector to a given host and port, using login and password.
      static long getJmxTimeout()  
      static int getMaxTries()
      The maximum number of times we back off on getting an mbean or a job.
      static javax.management.MBeanServerConnection getMBeanServerConnection​(java.lang.String server, int jmxPort, int rmiPort, java.lang.String userName, java.lang.String password)
      Returns a connection to a remote MbeanServer defined by the given arguments.
      static javax.management.MBeanServerConnection getMBeanServerConnection​(javax.management.remote.JMXServiceURL url, java.util.Map<java.lang.String,​java.lang.String[]> credentials)
      Connects to the given (url-specified) service point, sending the given credentials as login.
      static javax.management.openmbean.CompositeData getOneCompositeData​(javax.management.openmbean.TabularData items)
      Get a single CompositeData object out of a TabularData structure.
      static javax.management.remote.JMXServiceURL getUrl​(java.lang.String server, int jmxPort, int rmiPort)
      Constructs the same service URL that JConsole does on the basis of a server name, a JMX port number, and a RMI port number.
      static java.util.Map<java.lang.String,​java.lang.String[]> packageCredentials​(java.lang.String userName, java.lang.String password)
      Packages credentials as an environment for JMX connections.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • log

        public static final org.slf4j.Logger log
        The logger.
    • Method Detail

      • getMaxTries

        public static int getMaxTries()
        The maximum number of times we back off on getting an mbean or a job. The cumulative time trying is 2^(MAX_TRIES) milliseconds, thus the constant is defined as log_2(TIMEOUT), as set in settings.
        Returns:
        The number of tries
      • getJmxTimeout

        public static long getJmxTimeout()
        Returns:
        the JMX timeout in milliseconds.
      • getUrl

        public static javax.management.remote.JMXServiceURL getUrl​(java.lang.String server,
                                                                   int jmxPort,
                                                                   int rmiPort)
        Constructs the same service URL that JConsole does on the basis of a server name, a JMX port number, and a RMI port number.

        Example URL: service:jmx:rmi://0.0.0.0:9999/jndi/rmi://0.0.0.0:1099/JMXConnector where RMI port number = 9999, JMX port number = 1099 server = 0.0.0.0 a.k.a localhost(?).

        Parameters:
        server - The server that should be connected to using the constructed URL.
        jmxPort - The number of the JMX port that should be connected to using the constructed URL (may not be a negative number)
        rmiPort - The number of the RMI port that should be connected to using the constructed URL, or -1 if the default RMI port should be used.
        Returns:
        the constructed URL.
      • getMBeanServerConnection

        public static javax.management.MBeanServerConnection getMBeanServerConnection​(java.lang.String server,
                                                                                      int jmxPort,
                                                                                      int rmiPort,
                                                                                      java.lang.String userName,
                                                                                      java.lang.String password)
        Returns a connection to a remote MbeanServer defined by the given arguments.
        Parameters:
        server - the remote servername
        jmxPort - the remote jmx-port
        rmiPort - the remote rmi-port
        userName - the username
        password - the password
        Returns:
        a MBeanServerConnection to a remote MbeanServer defined by the given arguments.
      • getMBeanServerConnection

        public static javax.management.MBeanServerConnection getMBeanServerConnection​(javax.management.remote.JMXServiceURL url,
                                                                                      java.util.Map<java.lang.String,​java.lang.String[]> credentials)
        Connects to the given (url-specified) service point, sending the given credentials as login.
        Parameters:
        url - The JMX service url of some JVM on some machine.
        credentials - a map with (at least) one entry, mapping "jmx.remote.credentials" to a String array of length 2. Its first item should be the user name. Its second item should be the password.
        Returns:
        An MBeanServerConnection representing the connected session.
      • packageCredentials

        public static java.util.Map<java.lang.String,​java.lang.String[]> packageCredentials​(java.lang.String userName,
                                                                                                  java.lang.String password)
        Packages credentials as an environment for JMX connections. This packaging has the same form that JConsole uses: a one-entry Map, the mapping of "jmx.remote.credentials" being an array containing the user name and the password.
        Parameters:
        userName - The user to login as
        password - The password to use for that user
        Returns:
        the packaged credentials
      • executeCommand

        public static java.lang.Object executeCommand​(javax.management.MBeanServerConnection connection,
                                                      java.lang.String beanName,
                                                      java.lang.String command,
                                                      java.lang.String... arguments)
        Execute a command on a bean.
        Parameters:
        connection - Connection to the server holding the bean.
        beanName - Name of the bean.
        command - Command to execute.
        arguments - Arguments to the command. Only string arguments are possible at the moment.
        Returns:
        The return value of the executed command.
      • getAttribute

        public static java.lang.Object getAttribute​(java.lang.String beanName,
                                                    java.lang.String attribute,
                                                    javax.management.MBeanServerConnection connection)
        Get the value of an attribute from a bean.
        Parameters:
        beanName - Name of the bean to get an attribute for.
        attribute - Name of the attribute to get.
        connection - A connection to the JMX server for the bean.
        Returns:
        Value of the attribute.
      • getBeanName

        public static javax.management.ObjectName getBeanName​(java.lang.String beanName)
        Get a bean name from a string version.
        Parameters:
        beanName - String representation of bean name
        Returns:
        Object representing that bean name.
      • getJMXConnector

        public static javax.management.remote.JMXConnector getJMXConnector​(java.lang.String hostName,
                                                                           int jmxPort,
                                                                           java.lang.String login,
                                                                           java.lang.String password)
        Get a JMXConnector to a given host and port, using login and password.
        Parameters:
        hostName - The host to attempt to connect to.
        jmxPort - The port on the host to connect to (a non-negative number).
        login - The login name to authenticate as (typically "controlRole" or "monitorRole".
        password - The password for JMX access.
        Returns:
        A JMX connector to the given host and port, using default RMI.
        Throws:
        IOFailure - if connecting to JMX fails.
      • getOneCompositeData

        public static javax.management.openmbean.CompositeData getOneCompositeData​(javax.management.openmbean.TabularData items)
        Get a single CompositeData object out of a TabularData structure.
        Parameters:
        items - TabularData structure as returned from JMX calls.
        Returns:
        The one item in the items structure.
        Throws:
        ArgumentNotValid - if there is not exactly one item in items, or items is null.
      • executeCommand

        public static java.lang.Object executeCommand​(javax.management.remote.JMXConnector connector,
                                                      java.lang.String beanName,
                                                      java.lang.String command,
                                                      java.lang.String... arguments)
        Execute a single command, closing the connector afterwards. If you wish to hold on to the connector, call JMXUtils#executeCommand(MBeanServerConnection, String, String, String[])
        Parameters:
        connector - A one-shot connector object.
        beanName - The name of the bean to execute a command on.
        command - The command to execute.
        arguments - The arguments to the command (all strings)
        Returns:
        Whatever the command returned.
      • getAttribute

        public static java.lang.Object getAttribute​(javax.management.remote.JMXConnector connector,
                                                    java.lang.String beanName,
                                                    java.lang.String attribute)
        Get the value of an attribute, closing the connector afterwards. If you wish to hold on to the connector, call JMXUtils#executeCommand(MBeanServerConnection, String, String, String[])
        Parameters:
        connector - A one-shot connector object.
        beanName - The name of the bean to get an attribute from.
        attribute - The attribute to get.
        Returns:
        Whatever the command returned.