【问题标题】:How to get a MBean binding class instance如何获取 MBean 绑定类实例
【发布时间】:2012-02-07 14:08:58
【问题描述】:

我正在尝试使用 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>

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

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

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

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

【问题讨论】:

  • 以下文章可能会帮助您实现这一目标。尽管它自 2005 年以来就描述了读取 MBean 的要求。 informit.com/guides/content.aspx?g=java&seqNum=160
  • 嘿,我已经看过了,根据那篇文章,我们需要从 mbean 服务器获取该对象名称的 ObjectInstance。但这并没有给我一个 BasicThreadPool 对象,但它给了我一个对象'ServerObjectInstance' ..请你澄清一下如何从这个对象中获取 BasicThreadPool 对象。谢谢。
  • 考虑使用 RMI。你可以阅读它here

标签: java jmx jboss5.x mbeans


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    JMX 不能那样工作。相反,它通过公开一个通用反射接口来工作,允许您调用任何给定 MBean 上的操作和属性。这是通过MBeanServerConnection 接口完成的(其中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 提供了一个非常好的抽象 re-exposes the target MBean using a Java interface that you specify。这让一切都更像普通的 Java,并且更容易使用。

    【讨论】:

    • 抱歉,课程名称是我已有的。我也知道我们可以得到属性。当我们在 JBOSS-Service.xml 中定义了一个 MBean 时,请澄清我。我相信当我们启动 JBOSS 服务器时,将创建一个使用 MBean 绑定的类的实例。是这样吗?如果是这样,那么我想要该类的实例..
    • @Muthu:我是说你无法获取实例,因为 JMX 不允许。
    猜你喜欢
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多