Package dk.netarkivet.common.utils
Class JMXUtils
- java.lang.Object
-
- dk.netarkivet.common.utils.JMXUtils
-
public final class JMXUtils extends 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 Object
executeCommand(MBeanServerConnection connection, String beanName, String command, String... arguments)
Execute a command on a bean.static Object
executeCommand(JMXConnector connector, String beanName, String command, String... arguments)
Execute a single command, closing the connector afterwards.static Object
getAttribute(String beanName, String attribute, MBeanServerConnection connection)
Get the value of an attribute from a bean.static Object
getAttribute(JMXConnector connector, String beanName, String attribute)
Get the value of an attribute, closing the connector afterwards.static ObjectName
getBeanName(String beanName)
Get a bean name from a string version.static JMXConnector
getJMXConnector(String hostName, int jmxPort, String login, 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 MBeanServerConnection
getMBeanServerConnection(String server, int jmxPort, int rmiPort, String userName, String password)
Returns a connection to a remote MbeanServer defined by the given arguments.static MBeanServerConnection
getMBeanServerConnection(JMXServiceURL url, Map<String,String[]> credentials)
Connects to the given (url-specified) service point, sending the given credentials as login.static CompositeData
getOneCompositeData(TabularData items)
Get a single CompositeData object out of a TabularData structure.static JMXServiceURL
getUrl(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 Map<String,String[]>
packageCredentials(String userName, String password)
Packages credentials as an environment for JMX connections.
-
-
-
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 JMXServiceURL getUrl(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 MBeanServerConnection getMBeanServerConnection(String server, int jmxPort, int rmiPort, String userName, String password)
Returns a connection to a remote MbeanServer defined by the given arguments.- Parameters:
server
- the remote servernamejmxPort
- the remote jmx-portrmiPort
- the remote rmi-portuserName
- the usernamepassword
- the password- Returns:
- a MBeanServerConnection to a remote MbeanServer defined by the given arguments.
-
getMBeanServerConnection
public static MBeanServerConnection getMBeanServerConnection(JMXServiceURL url, Map<String,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 Map<String,String[]> packageCredentials(String userName, 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 aspassword
- The password to use for that user- Returns:
- the packaged credentials
-
executeCommand
public static Object executeCommand(MBeanServerConnection connection, String beanName, String command, 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 Object getAttribute(String beanName, String attribute, 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 ObjectName getBeanName(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 JMXConnector getJMXConnector(String hostName, int jmxPort, String login, 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 CompositeData getOneCompositeData(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 Object executeCommand(JMXConnector connector, String beanName, String command, 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 Object getAttribute(JMXConnector connector, String beanName, 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.
-
-