问题描述
我正在尝试使用MBean获取绑定在jboss-service.xml
中的服务类的实例.
I am trying to get the instance of a service class bound in jboss-service.xml
using MBean.
JBoss-Service.xml
定义了一个BasicThreadPool
,我们希望在我们的代码中使用它.这就是JBOSS-Service.xml
中的内容.
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"));
现在我有了MBean信息.我想在MBean中定义BasicThreadPool
对象的实例.有可能吗?
Now I have the MBean Info. I want to have an instance of the BasicThreadPool
object defined in the MBean. Is it possible ?
我知道一种方法,我们可以从MBean Info中获取类名,也可以获取用于构造实例的属性.有更好的方法吗?
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所指出的,您不能直接获取线程池的直接实例,而只能使用 MBeanServerInvocationHandler 将带您接近.
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);
该示例中的
threadPool 实例现在实现了基础线程池服务的所有方法.
The threadPool instance in that example now implements all the methods of the underlying thread pool service.
提醒您,如果只需要它来提交执行任务,则只需要一件事情,那就是 Instance 属性,该属性[几乎]是相同的界面,因此您也可以执行这个:
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");
....,但不是远程的,仅在同一VM中.
.... but not remotely though, only in the same VM.
这篇关于如何获取MBean绑定类实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!