Monday, March 26, 2007

How to call an MBean method (JMX) within an EJB method in Jboss

I am going to write this post, even though you can put together all the resources you need, just because I have gone through lots of forums and posts that have quite outdated info regarding this specific task I thought to write a small post, so it can be indexed in google!Its not rocket science as you can see, though you never know! (i am sorry blogspot really sucks when it comes to writing code samples)

What I wanted to do!
I had implemented some JMX Services (Mbeans) deployed in my Application Server, Jboss 4.2.0RC1 . I needed to invoke some certain methods, on these services within EJB code, actually it was a Stateless Session Bean.

My Mbean (JMX) (skip that if you all about Mbeans)
I have used the classic way of implementing Mbeans
  • Defined a concrete class that is going to hold the actual implementation e.g
  • The class above (BatchWorker)
  • Defined an Interface Called BatchWorkerMBean that
  • Deployed as a sar on my JBoss instance.
Where to look for examples
Eventually all the info you need in order to accomplish your task is available on the extremely helpful JBoss Wiki. So head to JMX FAQ of JBoss wiki. The section below just follows the resources tips.

How to call the Mbean
As it is indicated on the resources it is a multi step process.
  1. Locate and acquire a reference of the MbeanServer running on Jboss
  2. Create a proxy object of your actuall Service
  3. Invoke the method
let's see (add code to do proper exception handling)

//get a references of the MBeanServer of Jboss

MBeanServer server = MBeanServerLocator.locateJBoss()

//create an Objectname
//watch out how you have configured your mbean its domain and its name!

ObjectName objectName =
new Objectname("BatchWorker:name=BatchWorker");

//get a proxy object for your mbean

BatchworkerMbean mbean =

// Use the proxy and invoke,I have declared a method in the interface called doJob (void)
mbean.doJob("Do this from EJB");

So..simple enough isn't it? Always remember to have a look on Jboss Wiki and on JBoss Forums! Happy JBossing! ;)


  1. Thanks a lot for sharing this example, it is very useful and I#m sure it helped a lot of us. I think it would be very helpful if you would supply an example with an OpenMBean, because such an example is nowhere to be found.

  2. Extra cool Example! Very good explanation.

    How can we take a reference to a persistent MBean (without creating a new instance) in oder to invoke a method????

    Mike Mountrakis