ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 如何获取MBean绑定类实例

2019-10-09 00:01:14  阅读:300  来源: 互联网

标签:jboss5-x mbeans java jmx


我试图使用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中获取类名,我们也可以获得构造实例的属性.有没有更好的方法呢?

解决方法:

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

….但不是远程,只能在同一个VM中.

标签:jboss5-x,mbeans,java,jmx
来源: https://codeday.me/bug/20191008/1875409.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有