java – 如何获取MBean绑定类实例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 如何获取MBean绑定类实例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2259字,纯文字阅读大概需要4分钟。
内容图文
![java – 如何获取MBean绑定类实例](/upload/InfoBanner/zyjiaocheng/697/b6613747ab064ed8981673e7ddd56416.jpg)
我试图使用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中.
内容总结
以上是互联网集市为您收集整理的java – 如何获取MBean绑定类实例全部内容,希望文章能够帮你解决java – 如何获取MBean绑定类实例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。