Exploring under hood of Java applications using JMX – Part 1
Introduction.
A business company was looking for a proper method to monitor it’s business applications. They were almost implemented by Java. Firstly I was going to implement another application for this purpose. Fortunately a friend of mine shows the way. We planned to use JMX. Here is a tutorial that clearly shows you how to let a Java application to be exposed using JMX.
At first have a look on JMX technical introductions. I used to send SNMP messages for making a communication between applications during run time JMX has many advantages over SNMP. JMX simply enables you to start a listener in the application that could be watched. This makes the monitor side enable to touch and trace public methods of every classes that implements MBean interface. Dont worry if you have a legacy application that is not armored with MBean interfaces. You need just develop an API that expose the critical variables for the out side.
MBean Interface
I developed an interface that its name finished by MBean. This is a contract that should be respected to continue.
package com.datispars.monitoring;
public interface JmxSampleMBean {
public void getMessage();
public int add(int x, int y);
public String getTitle();
public int getTimeOut();
public void setTimeOut(int size);
}
MBean Implementation
Now I just implemented an implementation of the MBean. The sample is as much easy as talk without a comment.
package com.datispars.monitoring;
public class JmxSample implements JmxSampleMBean {
private final String title = "JMX Sample";
private int timeOut = 20;
public void getMessage() {
System.out.println("Hi," + getTitle());
}
public int add(int x, int y) {
int result = x + y;
System.out.println(x + "+" + y + "= " + result);
return result;
}
public String getTitle() {
System.out.println("the " + title + " has been called.");
return this.title;
}
public int getTimeOut() {
return this.timeOut;
}
public synchronized void setTimeOut(int value) {
this.timeOut = value;
System.out.println("time out has been changed to " + this.timeOut + " seconds.");
}
}
MBean Factory
The main method consists a MBean server and a MBean registration and nothing else:
package com.datispars.monitoring;
import java.lang.management.*;
import javax.management.*;
public class Main {
public static void main(String[] args) throws Exception {
// instantiating MBean server
MBeanServer mbServer = ManagementFactory.getPlatformMBeanServer();
// declaring name and type of MBean
ObjectName mBeanName = new ObjectName("com.datispars.monitoring:type=JmxSample");
// instantiating MBean
JmxSample jmxSample = new JmxSample();
// regisring onject as MBean
mbServer.registerMBean(jmxSample, mBeanName);
// waiting for jmx requests.
System.out.println("Listening...");
Thread.sleep(Integer.MAX_VALUE);
}
}
Big Brother Is Watching You
Now it is the time to watch what is happening within the application. Run the application. Open a terminal and just run “jconsole“. Click on the local application name. The jconsole looks fantastic!. Alive diagrams that draw by the information which are provided by JVM let you know how is the situation. Go to the MBean tab and find the MBean declarations of your application. Call “add” with integer numbers. Have a look on the console output of the application. It is great! That is why I love Java. You can find always engineering solutions for the problem.
In next part we will set up an Apache Tomcat monitoring with MBean objects.
Posted in Java, Linux, Software Engineering
No Comments