How to get a MBean binding class instance

心已入冬 提交于 2019-12-01 10:47:47

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!