Search in sources :

Example 1 with JmxExporter

use of org.springside.modules.metrics.exporter.JmxExporter in project springside4 by springside.

the class MetricExamples method jmxExample.

@Test
public void jmxExample() throws InterruptedException, Exception {
    MetricRegistry metricRegistry = new MetricRegistry();
    Counter counter = metricRegistry.counter(MetricRegistry.name("UserService", "getUser.counter"));
    Timer timer = metricRegistry.timer(MetricRegistry.name("UserService", "getUser.timer"), new Double[] { 0.99d, 0.999d });
    // 无reporter,only exporter
    JmxExporter jmxExporter = new JmxExporter("metrics-example", metricRegistry);
    jmxExporter.initMBeans();
    ReportScheduler scheduler = new ReportScheduler(metricRegistry);
    scheduler.start(1, TimeUnit.SECONDS);
    counter.inc();
    TimerContext timerContext = timer.start();
    Thread.sleep(100);
    timerContext.stop();
    Thread.sleep(950);
    counter.inc(2);
    Thread.sleep(1050);
    scheduler.stop();
    // 校验MBean中的值
    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    System.out.println("TotalCount from MBean:" + mBeanServer.getAttribute(new ObjectName("metrics-example", "name", "UserService.getUser.counter"), "TotalCount").toString());
}
Also used : Counter(org.springside.modules.metrics.metric.Counter) Timer(org.springside.modules.metrics.metric.Timer) JmxExporter(org.springside.modules.metrics.exporter.JmxExporter) TimerContext(org.springside.modules.metrics.metric.Timer.TimerContext) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Aggregations

MBeanServer (javax.management.MBeanServer)1 ObjectName (javax.management.ObjectName)1 Test (org.junit.Test)1 JmxExporter (org.springside.modules.metrics.exporter.JmxExporter)1 Counter (org.springside.modules.metrics.metric.Counter)1 Timer (org.springside.modules.metrics.metric.Timer)1 TimerContext (org.springside.modules.metrics.metric.Timer.TimerContext)1