JMX registry implementation for Micrometer metrics facade that publishes metrics as JMX MBeans
npx @tessl/cli install tessl/maven-io-micrometer--micrometer-registry-jmx@2.0.0A JMX (Java Management Extensions) registry implementation for the Micrometer application metrics facade. This library enables applications to expose metrics via JMX MBeans, allowing monitoring tools and management consoles to access application metrics through the standard JMX interface.
Maven:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-jmx</artifactId>
<version>2.0.0-M3</version>
</dependency>Gradle:
implementation 'io.micrometer:micrometer-registry-jmx:2.0.0-M3'import io.micrometer.jmx.JmxMeterRegistry;
import io.micrometer.jmx.JmxConfig;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.util.HierarchicalNameMapper;import io.micrometer.jmx.JmxMeterRegistry;
import io.micrometer.jmx.JmxConfig;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Timer;
// Create JMX registry with default configuration
JmxMeterRegistry registry = new JmxMeterRegistry(JmxConfig.DEFAULT, Clock.SYSTEM);
// Create and use meters
Timer timer = registry.timer("request.duration");
timer.record(() -> {
// Your timed code here
});
// Metrics are automatically published to JMX MBeans
// Access via JConsole, VisualVM, or programmatically via MBeanServerThe JMX registry implementation consists of several key components:
The main registry class that collects metrics and publishes them as JMX MBeans.
public class JmxMeterRegistry extends DropwizardMeterRegistry {
/**
* Create JMX registry with default name mapper
*/
public JmxMeterRegistry(JmxConfig config, Clock clock);
/**
* Create JMX registry with custom name mapper
*/
public JmxMeterRegistry(JmxConfig config, Clock clock, HierarchicalNameMapper nameMapper);
/**
* Create JMX registry with custom metric registry
*/
public JmxMeterRegistry(JmxConfig config, Clock clock, HierarchicalNameMapper nameMapper, MetricRegistry metricRegistry);
/**
* Create JMX registry with full customization
* Note: The JMX reporter starts automatically upon construction
*/
public JmxMeterRegistry(JmxConfig config, Clock clock, HierarchicalNameMapper nameMapper, MetricRegistry metricRegistry, JmxReporter jmxReporter);
/**
* Start the JMX reporter to begin publishing metrics
*/
public void start();
/**
* Stop the JMX reporter to cease publishing metrics
*/
public void stop();
/**
* Close the registry and stop the JMX reporter
*/
@Override
public void close();
}Configuration interface for JMX-specific settings.
public interface JmxConfig extends DropwizardConfig {
/**
* Accept configuration defaults
*/
JmxConfig DEFAULT = k -> null;
@Override
default String prefix();
/**
* @return JMX domain under which to publish metrics.
*/
default String domain();
}The JMX registry inherits all standard Micrometer types through DropwizardMeterRegistry:
// From Micrometer Core
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.util.HierarchicalNameMapper;
import io.micrometer.core.instrument.dropwizard.DropwizardConfig;
// From Dropwizard Metrics
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.jmx.JmxReporter;All standard Micrometer meter types are supported:
import io.micrometer.jmx.JmxMeterRegistry;
import io.micrometer.jmx.JmxConfig;
import io.micrometer.core.instrument.Clock;
// Create registry
JmxMeterRegistry registry = new JmxMeterRegistry(JmxConfig.DEFAULT, Clock.SYSTEM);
// Create metrics
registry.counter("requests.total").increment();
registry.gauge("memory.usage", Runtime.getRuntime().totalMemory());
registry.timer("request.duration").record(Duration.ofMillis(150));import io.micrometer.jmx.JmxConfig;
// Custom configuration
JmxConfig config = new JmxConfig() {
@Override
public String domain() {
return "myapp.metrics"; // Custom JMX domain
}
@Override
public String get(String key) {
return null; // Use defaults for other settings
}
};
JmxMeterRegistry registry = new JmxMeterRegistry(config, Clock.SYSTEM);import io.micrometer.core.instrument.util.HierarchicalNameMapper;
// Custom name mapper for JMX object names
HierarchicalNameMapper nameMapper = (id, convention) -> {
return "myapp." + id.getName().replace(".", "_");
};
JmxMeterRegistry registry = new JmxMeterRegistry(
JmxConfig.DEFAULT,
Clock.SYSTEM,
nameMapper
);JmxMeterRegistry registry = new JmxMeterRegistry(JmxConfig.DEFAULT, Clock.SYSTEM);
// Registry starts automatically upon construction, but you can control it
registry.stop(); // Stop publishing metrics
registry.start(); // Resume publishing metrics
// Proper cleanup
registry.close(); // Stops reporter and closes registry