JBoss MBeans POJO

JMX MBean services are the core building blocks of the JBoss Application Server. The success of the full Open Source J2EE stack lies with the use of Java Management Extension which provides a common spine that allows the user to integrate modules, containers, and plug-ins.

While MBean services are very useful, developing them is not trivial. In a regular JMX server environment, you need to extend numerous framework classes and work with reflection-like APIs. Moreover common IDE have showed a greater attention for other JEE components like EJB which can be developed using simple wizards.

 Until now! In the JBoss EJB 3.0 server, you can use simple annotations to configure POJOs and make them available as MBean services. In other words no mess with invasive frameworks, no configuration files: just a couple of annotations and you have your EJB 3.0's style MBean services.

Not only ! Developers can specify dependency between MBean services so that they are started in a particular order when the server starts, which makes them especially suitable for implementing the server's internal infrastructure services. A lot of JBoss internal services are implemented as MBeans. They communicate with each other via the internal JMX bus (i.e., the JBoss JMX Microkernel).

So how are my MBeans different from EJB services ?

The way you define them is very similar to how you define stateless or stateful session beans.

One very important difference is that there will only ever be ONE instance of the service bean. i.e. it is not pooled - the bean instance is a singleton. The singleton bean contains shared state, so data set by one client is accessible by other clients.

Example simple ServicePOJO. Let's see our first sample.

package sample.jmx;

import org.jboss.annotation.ejb.Service; 
import org.jboss.annotation.ejb.Management;

@Service (objectName="sampleJMX:service=calculateInterest") 
@Management(CalculateInterest.class) 
public class CalculateInterestMBean implements CalculateInterest {

 double interestRate;

 public void setInterestRate (double interestRate) { 
 this.interestRate = interestRate; 
 }

 public double getInterestRate () { 
 return interestRate; 
 }

 public double calculate (double saving, int days) { 
 double interest = (saving * days * this.interestRate)/36500; 
 return interest; 
 }

 // Lifecycle methods 
 public void create() throws Exception { 
 this.interestRate = 5; 
 System.out.println("CalculateInterestMBean - Creating"); 
 }

 public void destroy() { 
 System.out.println("CalculateInterestMBean - Destroying"); 
 }

}

Just a plain POJO with @Service annotation: this defines CalculatorInterestMBean as a singleton service in JBoss. It defines the objectName for this MBean as "sampleJMX:service=calculateInterest".

The second annotation is @Management. When JBoss finds this annotation will inspect this interface, and create and install an MBean implementing the attributes and operations defined in the @Management interface.

And here's the implemented interface : no annotations are needed here.

package sample.jmx;

public interface CalculateInterest {

 public void setInterestRate (double interestRate); 
 public double getInterestRate ();

 // The management method 
 public double calculate (double saving, int days);

 // Life cycle method 
 public void create () throws Exception; 
 public void destroy () throws Exception; 
}

Related articles available on mastertheboss.com

How to a dump of JNDI tree with JMX console ?

#2 JBoss Howto. This is the recipe of the day

How to configure JBoss to bind to a different IP ?

  If you want to change the jboss.bind.address property use the 

How to shut down JBoss from remote ?

JBoss recipe of the day

How to create multiple instances of an Mbean?

  Supposing you need two instances of an MBean, let's say one fo

JBoss classloader issues

By default JBoss (prior to version 3.2) uses a flat class loading

jboss.xml reference

JBoss xml reference   JBoss AS uses a set of custom xml de

Follow us on Twitter