以编程方式访问Tomcat中的内置MBean [英] Accessing built-in MBeans in Tomcat programmatically

查看:212
本文介绍了以编程方式访问Tomcat中的内置MBean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在尝试从此处的本教程中修改代码: http://docs.oracle.com/javase/tutorial/jmx/remote/custom.html 这样我就可以从以下描述的tomcat访问MBean: http://wiki.apache. org/tomcat/FAQ/Monitoring

Basically I'm trying to modify the code from this tutorial here: http://docs.oracle.com/javase/tutorial/jmx/remote/custom.html so that I can access the MBeans from tomcat that are described here: http://wiki.apache.org/tomcat/FAQ/Monitoring

从代码访问JMX Bean java.lang:type = Memory毫无问题,因为它的接口是在java.lang中定义的.这是该代码的示例:

there is no problem accessing the JMX Bean java.lang:type=Memory from code since it's interface is defined in java.lang. Here's the code example of that:

    ObjectName mbeanName = new ObjectName("java.lang:type=Memory");
    MemoryMXBean mxbeanProxy2 = JMX.newMXBeanProxy(mbsc, mbeanName, MemoryMXBean.class, true);
    MemoryUsage memUsage = mxbeanProxy2.getHeapMemoryUsage();
    echo("\nMemory Utilization: " + (memUsage.getUsed()/(double)memUsage.getMax()) * 100 +  "%");

此处mbsc是MBeanServerConnection的实例. 问题是,当我尝试以类似方式访问tomcat中的内置MBean时,遇到了找不到为任何tomcat MBean定义的任何接口的问题.我可以从JConsole监视MBean,但是为此,我需要能够从代码执行此操作.我在某处发现它也可以通过以下方式完成:

Here the mbsc is an instance of MBeanServerConnection. The problem is that when I'm trying to access the built-in MBeans in tomcat in a similar way I run into the problem that I can't find any interface defined for any of the tomcat MBeans. I can monitor the MBeans from JConsole but for this I need to be able to do this from code. I found it somewhere that this could also done with something like this:

ObjectName mbeanName2 = new ObjectName("Catalina:type=ThreadPool,name=\"http-apr-8080\"");
Object value = mbsc.getAttribute(mbeanName, "name");

但是这给了我这个例外: 线程主"中的异常javax.management.AttributeNotFoundException:无此类属性:com.sun.jmx.mbeanserver上的名称……

But this gives me this exception: Exception in thread "main" javax.management.AttributeNotFoundException: No such attribute: name at com.sun.jmx.mbeanserver......

我觉得我缺少一些基本的东西.但是,关于此的信息似乎非常有限,谷歌并没有太大帮助.

I feel like I'm missing something fairly basic. But the information on this specifically seem to be very limited and google did not help much.

推荐答案

我认为您的第二段代码中有错别字.您为Catalina ThreadPool创建了一个名为 mbeanName2 的新ObjectName,但是当您尝试检索属性名称"时,您仍在使用 mbeanName .

I think there's a typo in your second snippet of code. You created a new ObjectName for the Catalina ThreadPool called mbeanName2, but when you try to retrieve the attribute "name", you're still using mbeanName.

应该是:

ObjectName mbeanName2 = new ObjectName("Catalina:type=ThreadPool,name=\"http-apr-8080\"");
Object value = mbsc.getAttribute(mbeanName2, "name");

除此之外,您的代码应该可以正常工作.

Other than that, you code should work fine.

这篇关于以编程方式访问Tomcat中的内置MBean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆