4

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

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>

我正在尝试在我的代码中访问它,如下所示,

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

现在我有了 MBean 信息。我想要一个BasicThreadPool在 MBean 中定义的对象的实例。可能吗 ?

我知道一种方法,我们可以从 MBean Info 中获取类名,也可以获取构造实例的属性。有没有更好的方法呢?

4

2 回答 2

4

正如 skaffman 所指出的,您不能直接获取线程池的直接实例,但使用MBeanServerInvocationHandler会让您非常接近。

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");

....但不是远程的,只在同一个虚拟机中。

于 2012-02-07T18:56:54.457 回答
3

我想要在 MBean 中定义一个 BasicThreadPool 对象的实例。可能吗 ?

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

对于您的示例,您将使用以下方式获取 MBeanName上的属性:jboss.system:service=ThreadPool

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

这是一个丑陋的 API,但可以完成工作。

如果您有兴趣,Spring 围绕 JMX 提供了一个非常好的抽象,它使用您指定的 Java 接口重新公开目标 MBean。这使得一切都更像普通的 Java,并且更容易使用。

于 2012-02-07T14:17:12.967 回答