MBeanUtils { public static void registerMBean(final ObjectInstance objectInstance, final String mBeanAttributeName, MetricName metricName, Metric2Registry metricRegistry) { metricRegistry.register(metricName, new Gauge<Object>() { @Override public Object getValue() { return getValueFromMBean(objectInstance, mBeanAttributeName); } }); } private  MBeanUtils(); static ObjectInstance queryMBean(String objectName); static Set<ObjectInstance> queryMBeans(String objectName); static Set<ObjectInstance> queryMBeans(ObjectName objectName, QueryExp queryExp); static void registerMBean(final ObjectInstance objectInstance, final String mBeanAttributeName, MetricName metricName, Metric2Registry metricRegistry); static Object getValueFromMBean(ObjectInstance objectInstance, String mBeanAttributeName);  }
@Test public void testRegisterMBean() throws Exception { MBeanUtils.registerMBean(MBeanUtils.queryMBean("java.lang:type=ClassLoading"), "LoadedClassCount", name("jvm_class_count").build(), metricRegistry); final Gauge classLoadingGauge = metricRegistry.getGauges().get(name("jvm_class_count").build()); assertNotNull(classLoadingGauge); assertTrue(((Integer) classLoadingGauge.getValue()) > 1); }