4

I am trying to get the instance of a service class bound in jboss-service.xml using MBean.

JBoss-Service.xml has defined a BasicThreadPool which we want to use it in our code. This is what it is in JBOSS-Service.xml.

  <mbean 
        code="org.jboss.util.threadpool.BasicThreadPool"
        name="jboss.system:service=ThreadPool">

  <attribute name="Name">JBoss System Threads</attribute>
  <attribute name="ThreadGroupName">System Threads</attribute>
  <attribute name="KeepAliveTime">60000</attribute>
  <attribute name="MaximumPoolSize">10</attribute>

  <attribute name="MaximumQueueSize">1000</attribute>
  <!-- The behavior of the pool when a task is added and the queue is full.
  abort - a RuntimeException is thrown
  run - the calling thread executes the task
  wait - the calling thread blocks until the queue has room
  discard - the task is silently discarded without being run
  discardOldest - check to see if a task is about to complete and enque
     the new task if possible, else run the task in the calling thread
  -->
  <attribute name="BlockingMode">run</attribute>
   </mbean>

I am trying to access this in my code as below,

MBeanServer server = MBeanServerLocator.locateJBoss();          
MBeanInfo mbeaninfo = server.getMBeanInfo(new ObjectName("jboss.system:service=ThreadPool"));

Now I have the MBean Info. I want to have an instance of the BasicThreadPool object defined in the MBean. Is it possible ?

I know a way, we can get the class name from the MBean Info and also we can get the attributes by which we can construct an instance. Is there any better way of doing it ?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Muthu
  • 67
  • 3
  • 9
  • The following article may help you to achieve this. Although its since 2005 it describes the requirement to read MBean. http://www.informit.com/guides/content.aspx?g=java&seqNum=160 – bugske Feb 07 '12 at 14:16
  • Hey I have already looked at that, as per that article, we need to get the ObjectInstance for that Object Name from the mbean server. But that doesn't gave me a BasicThreadPool Object but it gives me an object 'ServerObjectInstance' ..will you please clarify me how to get the BasicThreadPool Object from this Object. Thanks. – Muthu Feb 07 '12 at 14:26
  • Consider using RMI. You can read about it [here](http://docs.oracle.com/javase/7/docs/technotes/guides/rmi/index.html) – Mads Nielsen Feb 08 '12 at 16:14

2 Answers2

4

As skaffman indicated, you cannot directly acquire a direct instance of the thread pool, but using a MBeanServerInvocationHandler will get you pretty close.

import org.jboss.util.threadpool.BasicThreadPoolMBean;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
.....
BasicThreadPoolMBean threadPool = (BasicThreadPoolMBean)MBeanServerInvocationHandler.newProxyInstance(MBeanServerLocator.locateJBoss(); new ObjectName("jboss.system:service=ThreadPool"), BasicThreadPoolMBean.class, false);

The threadPool instance in that example now implements all the methods of the underlying thread pool service.

Mind you, if you only need it to submit tasks for execution, there's only one thing you need and that's the Instance attribute which is [pretty much] the same interface, so you could also do this:

import  org.jboss.util.threadpool.ThreadPool;
import javax.management.ObjectName;
.....
ThreadPool threadPool = (ThreadPool)MBeanServerLocator.locateJBoss().getAttribute(new ObjectName("jboss.system:service=ThreadPool"), "Instance");

.... but not remotely though, only in the same VM.

Nicholas
  • 15,916
  • 4
  • 42
  • 66
3

I want to have an instance of the BasicThreadPool object defined in the MBean. Is it possible ?

JMX doesn't work that way. Instead, it works by exposing a general-purpose reflective interface allowing you to invoke operations and attributes on any given MBean. This is done via the MBeanServerConnection interface (of which MBeanServer is a sub-type).

For your example, you would fetch the Name attribute on the jboss.system:service=ThreadPool MBean using something like this:

MBeanServer server = MBeanServerLocator.locateJBoss();      
ObjectName objectName = new ObjectName("jboss.system:service=ThreadPool");    
String threadPoolName = (String) server.getAttribute(objectName , "Name");

It's an ugly API, but does the job.

If you're interested, Spring provides a very nice abstraction around JMX that re-exposes the target MBean using a Java interface that you specify. This makes everything feel more like normal Java, and is much easier to work with.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • Sorry, the name of the class is something I am already having. Also I understand we can get the attributes. Clarify me when we have defined a MBean in the JBOSS-Service.xml. I believe when we start the JBOSS server an instnace of the class bound using MBean will be created..is that so ? if thats the way then I want that instance of the class.. – Muthu Feb 07 '12 at 14:35
  • @Muthu: And I'm saying you can't get the instance, because JMX doesn't let you. – skaffman Feb 07 '12 at 14:39