Sunday, August 16, 2015

JMX simple example

1. Intro

The JMX technology provides a simple, standard way of managing resources such as applications, devices, and services. Because the JMX technology is dynamic, you can use it to monitor and manage resources as they are created, installed and implemented. You can also use the JMX technology to monitor and manage the Java Virtual Machine (Java VM).

How it works : when our application is running, we can connect to it using jconsole application (located in JAVA_HOME/bin directory) : 

After that we have to switch to tab "MBeans", select our application, and we can view and change(if we want) IN RUNTIME some application variables: 



We have just to change variable (or execute method) using jconsole - and it will have the same effect like it was changed(or method was executed) inside our application. 


2. Project structure

Everything is pretty simple :)  



3. pom.xml file

Jmx is part of Java SDK, so we don't need any dependencies at all.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.demien.jmxtest</groupId>
    <artifactId>JMXTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    
</project>

4. JMX Bean 

First we need to create interface for our class  which we want to make visible/usable for jconsole. And , ofcource, we also need implementation.  Important thing here - naming. Interface have to be ending with MBean. Implementation - it has to be the same name as for interface, but without MBean.



package com.demien.jmxtest;

public interface TestJmxConfigMBean {
    String getValue();
    void setValue(String value);

    Long getRunningTime();
    void resetRunnigTime();
} 
 
 
 

package com.demien.jmxtest;

import java.util.Date;

public class TestJmxConfig implements TestJmxConfigMBean {

    private String value="Start value";
    private Date startTime=new Date();

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value=value;
    }

    public Long getRunningTime() {
        Date currectTime=new Date();
        return currectTime.getTime()-startTime.getTime();
    }

    public void resetRunnigTime() {
        startTime=new Date();
    }
}




5. Application runner

We need to register our MBen at MBeanServer. After that it will be visible by jconsole. 
Other logic is very simple : just printing values from our MBean every 10 seconds. 


package com.demien.jmxtest;

import javax.management.*;
import java.lang.management.ManagementFactory;

public class App {

    static TestJmxConfig mBean = new TestJmxConfig();

    public static void registerMbean() throws Exception {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName name = new ObjectName("com.demien.jmxtest:type=TestJmxConfig");
        mbs.registerMBean(mBean, name);

    }

    public static void main(String[] args) throws Exception {
        registerMbean();
        while (true) {
            System.out.println("mbeanData: RunningTime="+mBean.getRunningTime()+"  Value="+mBean.getValue());
            Thread.sleep(10000);
        }
    }
}



6. Results of execution

At the beginning, application is printing "Start value". At running time, between 20 and 30 seconds of execution I changed value, So for rinning time 30028 - we have value which I entered in jconsole. 
Little later(after 40 seconds of program running) I executed operation "ResetRunnigTime". So time began to count again from 0. 

mbeanData: RunningTime=28  Value=Start value
mbeanData: RunningTime=10028  Value=Start value
mbeanData: RunningTime=20028  Value=Start value
mbeanData: RunningTime=30028  Value=Value was changed ! 
mbeanData: RunningTime=40028  Value=Value was changed ! 
mbeanData: RunningTime=3418  Value=Value was changed ! 

mbeanData: RunningTime=13418  Value=Value was changed ! 

7. The end 

 Source code can be downloaded from here.

No comments:

Post a Comment