Sunday, July 25, 2010

JBoss/JMX: Create an MBean in 2 minutes

Components being used:
  • JBoss 5.1
  • JDK 6u20
Let's start by creating a plain interface containing the methods we want our MBean to expose, e.g.:
package de.ollinux.jmx.test;

public interface Test {
public void testSomething();
}
Next we need to create the MBean itself by implementing the above interface:
package de.ollinux.jmx.test;

public class TestMBean {
@Override
public void testSomething() {
/* ... */
}
}
So far so good. But how do we tell JBoss to register this MBean at its MBean-Server? Here we got two options: XML-descriptor vs. annotations. I prefer the second option cause it's less typing and I can specify the MBean-registration directly at my Java class. There are three things I need to describe:
  1. TestMBean is a Management-Bean
  2. Methods contained in the interface Test may be executed
  3. The MBean shall belong to the namespace de.ollinux and shall be called TheTestMBean
These three things may be specified at the MBean itself by using two annotations from the package org.jboss.ejb3.annotation:
@Service(objectName = "de.ollinux:service=TheTestMBean")
@Management(Test.class)
public class TestMBean {
@Override
public void testSomething() {
/* ... */
}
}
I package both class files in a service archive called TestMBean.sar, deploy it to my JBoss and finally the MBean shows up when I connect to the jmx-console and I may execute the testSomething method:

No comments:

Post a Comment