In this tutorial, I am trying to explain to you how easy you could monitor, configure and manage your Java application with JMX. A good starting point is available at Wikipedia.

Basically, a JMX domain contains MBeans, which are Java objects exposing attributes and operations. Namely, an MBean could allow you to configure your application or watch the server state and fire specific actions if necessary.

To me, JBoss provides the cleanest implementation of the JMX specification. Namely, it provides a base MBean class, ServiceMBeanSupport, which allows you to implement a JMX service very easily.

This helper class provides callback methods linked to the lifecycle of your component:

  • create the service
  • start the service
  • stop the service
  • destroy the service

Here is a very naive implementation:

public class MyFirstService extends ServiceMBeanSupport
implements MyFirstServiceMBean {

private static final Logger logger =
Logger.getLogger(MyFirstService.class.getName());

protected void createService()
throws Exception {
logger.info(”Creating service[”+getClass().getName()+”]”);
}

protected void startService()
throws Exception {
logger.info(”Starting service[”+getClass().getName()+”]”);
}

protected void stopService()
throws Exception {
logger.info(”Stopping service[”+getClass().getName()+”]”);
}

protected void destroyService()
throws Exception {
logger.info(”Destroying service[”+getClass().getName()+”]”);
}
}

Note that each and every MBean needs to implement an interface which exposes the attribute(s) and operation(s) you can invoke on it. Note also that the MyFirstServiceMBean interface is empty and extends ServiceMBean which exposes already the lifecycle methods.

Attached, you can find the full source for the project and also a built version. Drop the jar file in your JBoss installation (server/default/lib propably) and put the following my-first-service.xml file in the deploy directory (server/default/deploy) of your server:

< ?xml version="1.0" encoding="UTF-8"?>

name=”nicoll.net:service=MyFirstService”>



Note that the file must end with -service.xml for it to be recognized by the JBoss deployer.

Here you go, you have deployed your first JMX service. Now you can search on the JBoss’s jmx console or web console for the nicoll.net domain and the service MyFirstService.