Java 如何获取MBean绑定类实例

Java 如何获取MBean绑定类实例,java,jmx,jboss5.x,mbeans,Java,Jmx,Jboss5.x,Mbeans,我正在尝试使用MBean获取jboss service.xml中绑定的服务类实例 JBoss Service.xml定义了一个BasicThreadPool,我们希望在代码中使用它。 这就是JBOSS Service.xml中的内容 <mbean code="org.jboss.util.threadpool.BasicThreadPool" name="jboss.system:service=ThreadPool"> <attri

我正在尝试使用MBean获取
jboss service.xml
中绑定的服务类实例

JBoss Service.xml
定义了一个
BasicThreadPool
,我们希望在代码中使用它。 这就是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>
现在我有了MBean的信息。我希望在MBean中定义
BasicThreadPool
对象的实例。可能吗

我知道一种方法,我们可以从MBean信息中获取类名,还可以获取用于构造实例的属性。有没有更好的办法

我希望在MBean中定义BasicThreadPool对象的实例。可能吗

JMX不是那样工作的。相反,它的工作原理是公开一个通用反射接口,允许您在任何给定MBean上调用操作和属性。这是通过
MBeanServer连接
接口完成的(其中
MBeanServer
是一个子类型)

对于您的示例,您可以使用如下内容获取
jboss.system:service=ThreadPool
MBean上的
Name
属性:

MBeanServer server = MBeanServerLocator.locateJBoss();      
ObjectName objectName = new ObjectName("jboss.system:service=ThreadPool");    
String threadPoolName = (String) server.getAttribute(objectName , "Name");
这是一个丑陋的API,但它确实起到了作用


如果您感兴趣,Spring提供了一个关于JMX的非常好的抽象。这使一切看起来更像普通的Java,并且更易于使用。

正如skaffman所指出的,您无法直接获取线程池的直接实例,但使用线程池将非常接近

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实例现在实现了底层线程池服务的所有方法

请注意,如果您只需要它来提交任务以供执行,那么您只需要一件事,那就是Instance属性,它[几乎]是相同的接口,因此您也可以这样做:

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

。。。。但是不是远程的,只是在同一个VM中。

对不起,我已经知道了类的名称。我也知道我们可以得到属性。当我们在JBOSS-Service.xml中定义了MBean时,请澄清我的问题。我相信,当我们启动JBOSS服务器时,将创建一个使用MBean绑定的类的instance。是这样吗?如果是这样的话,那么我想要这个类的实例。@Muthu:我是说你不能得到这个实例,因为JMX不允许你。下面的文章可能会帮助你实现这一点。虽然自2005年以来就一直是这样,但它描述了读取MBean的需求。嘿,我已经看过了,根据那篇文章,我们需要从mbean服务器获取该对象名的ObjectInstance。但这并没有给我一个BasicThreadPool对象,而是给了我一个对象“ServerObjectInstance”……请您澄清一下如何从这个对象获取BasicThreadPool对象。谢谢。考虑使用RMI。你可以读到它
import  org.jboss.util.threadpool.ThreadPool;
import javax.management.ObjectName;
.....
ThreadPool threadPool = (ThreadPool)MBeanServerLocator.locateJBoss().getAttribute(new ObjectName("jboss.system:service=ThreadPool"), "Instance");