Class JMXUtils
- java.lang.Object
-
- com.palantir.util.JMXUtils
-
public final class JMXUtils extends Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
JMXUtils.AllowConnections
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Iterable<T>
getInstanceBeanProxies(Class<T> mbeanClazz)
Returns proxy interfaces to all beans registered to the server implementing the class mbeanClazz.static <T> T
newMBeanProxy(MBeanServerConnection conn, ObjectName objectName, Class<T> interfaceClass)
This method allows you to get at a bean registered with the server.static void
registerMBeanCatchAndLogExceptions(Object mbean, String objectName)
Simple method to register a bean with the server.static <T> DynamicMBean
registerMBeanWeakRefPlusCatchAndLogExceptions(T mbean, Class<T> clazz, String objectName)
You must retain a reference to the returnedDynamicMBean
for as long as you wish this bean to be registered.static void
startLocalJMXServer(int port, JMXUtils.AllowConnections allowConnections)
Programmatically starts a JMX server.static void
unregisterMBeanCatchAndLogExceptions(String objectName)
-
-
-
Method Detail
-
newMBeanProxy
public static <T> T newMBeanProxy(MBeanServerConnection conn, ObjectName objectName, Class<T> interfaceClass)
This method allows you to get at a bean registered with the server. Just provide the class of the mbean, the objectName this bean was registered with and the server this bean was registered with.The other methods in this class to register beans use
ManagementFactory.getPlatformMBeanServer()
as the server to register with.
-
registerMBeanCatchAndLogExceptions
public static void registerMBeanCatchAndLogExceptions(Object mbean, String objectName)
Simple method to register a bean with the server. The server used is the one returned byManagementFactory.getPlatformMBeanServer()
.This bean will remain registered until manually unregistered. If you want life-cycle management, use
registerMBeanWeakRefPlusCatchAndLogExceptions(Object, Class, String)
-
registerMBeanWeakRefPlusCatchAndLogExceptions
public static <T> DynamicMBean registerMBeanWeakRefPlusCatchAndLogExceptions(T mbean, Class<T> clazz, String objectName)
You must retain a reference to the returnedDynamicMBean
for as long as you wish this bean to be registered.Some mbeans have references to some pretty big classes and we have no good way to de-register these beans because these objects don't have good life-cyle management.
This method will register your mbean with JMX but JMX will hold onto it weakly so the large object may be GC'ed as usual.
When there is no more reference to the underlying MBean from anywhere else it may be freed. If this happens, the next call to this objectName will fail with an
IllegalStateException
. When this happens, this objectName will also be unregisterd withunregisterMBeanCatchAndLogExceptions(String)
Because of the weird JMX naming conventions, this method uses
StandardMBean
to proxy the bean as aDynamicMBean
to get it through the framework- Returns:
- the DynamicMBean whose lifecycle controls how long this bean is registered. null will be returned if this bean is not regsitered correctly
-
unregisterMBeanCatchAndLogExceptions
public static void unregisterMBeanCatchAndLogExceptions(String objectName)
-
startLocalJMXServer
public static void startLocalJMXServer(int port, JMXUtils.AllowConnections allowConnections) throws IOException, MalformedURLException, RemoteException
Programmatically starts a JMX server.- Parameters:
port
- port server should listen on.allowConnections
- allows restriction of connections to those from "localhost" only.- Throws:
IOException
MalformedURLException
RemoteException
-
-