java – 如何获取MBean绑定类实例
作者:互联网
我试图使用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