Saturday, June 20, 2009

JMX, Groovy, JFreeChart and Swing

Recently, I worked on creating JMX based dashboard application showing Weblogic instances' information. During the exercise I came across JMX and Groovy examples

Using JMX, Groovy, Swing and JFreeChart, I came up with a dashboard application which looks like this.


It show memory usage on two instances. It automatically updates every 10 seconds and redraws chart.

Surprisingly (or not so surprisingly), code required to be written to perform this activity is minimal. Specifically thanks to Groovy's SwingBuilder and JMX libraries. Groovy makes the task of using 4 different technologies really easy and fun.

import org.jfree.chart.ChartFactory
import javax.swing.WindowConstants as WC
import javax.naming.Context;

* WeblogicJMXReporter monitors Memory value for given node
* @author kartik.shah
public class WeblogicJMXReporter

static void main(String[] args)
def env = [:]
env["java.naming.factory.initial"] = "weblogic.jndi.WLInitialContextFactory"
env[Context.SECURITY_PRINCIPAL] = "system"
env[Context.SECURITY_CREDENTIALS] = "*******" //Password is always stars
def jmxServiceURL = new JMXServiceURL("http", "", 19400,"/jndi/")
def server = JMXConnectorFactory.connect(jmxServiceURL, env).MBeanServerConnection

def chart1Map = getChart("node1_svr", server);
def piedata1 = chart1Map."pieData"
def chart1 = chart1Map."Chart"
def jvmInfo1 = chart1Map."MBean"

def chart2Map = getChart("node2_svr", server);
def piedata2 = chart2Map."pieData"
def chart2 = chart2Map."Chart"
def jvmInfo2 = chart2Map."MBean"

def swing = new groovy.swing.SwingBuilder()

def frame = swing.frame(title:'Weblogic INT Memory Usage', defaultCloseOperation:WC.EXIT_ON_CLOSE,
size:[800,600], locationRelativeTo: null) {
panel(id:'canvas') { rigidArea(width:700, height:250) }

def bounds1 = new java.awt.Rectangle(0,0, 350,250).bounds
def bounds2 = new java.awt.Rectangle(351,0, 350, 250).bounds
recalculatePieData(piedata1, jvmInfo1)
recalculatePieData(piedata2, jvmInfo2)


chart1.draw(, bounds1)
chart2.draw(, bounds2)

static getChart(nodeName, server)
def jvmInfo = new GroovyMBean(server, 'com.bea:Location='+nodeName+',Name='+nodeName+',ServerRuntime='+nodeName+',Type=JVMRuntime')
def piedata = new
piedata.setValue "Free", jvmInfo.HeapFreeCurrent
piedata.setValue "Used", jvmInfo.HeapSizeMax - jvmInfo.HeapFreeCurrent
def options = [true, true, true]
def chart = ChartFactory.createPieChart(nodeName, piedata, *options)
chart.backgroundPaint = java.awt.Color.white

["MBean":jvmInfo, "pieData":piedata, "Chart":chart]

static recalculatePieData(piedata, jvmInfo)
piedata.setValue "Free", jvmInfo.HeapFreeCurrent
piedata.setValue "Used", jvmInfo.HeapSizeMax - jvmInfo.HeapFreeCurrent
println piedata.getValue("Free")
println piedata.getValue("Used")

For purpose of this blog and keeping it simple, I kept it all in one groovy class.
Blogged with the Flock Browser


kémèt aka the baby said...

Hi i'm a beginner in both java and groovy. really like your groovy demo a lot. i've try to come out with something like this for quit a moment now but with weblogic and soniq MQ broker to show metric in queue ... thank for the exemple especially the jfreechart part .. nice . excuse my english i'm in france ..keep up the good work

Eric said...

Thanks for this example. This is exactly what I'm planning on doing. I have instrumented several java middleware applications and have an XML db server that is already instrumented using JMX MBeans and Agents.

I'm in the process of building a dashboard application that queries the JMX instrumentation and displays various charts and other information.

Your example is very helpful.