简体   繁体   English

JMX MXBean将所有属性均未定义-Spring 3.0.x / Tomcat 6.0

[英]JMX MXBean Attributes all UNDEFINED - Spring 3.0.x/Tomcat 6.0

I've been trying to get a sample JMX MXBean working in a Spring-configured webapp, but any basic attributes on the MXBean are coming up as UNDEFINED when I connect with jconsole. 我一直在尝试让示例JMX MXBean在Spring配置的Web应用程序中工作,但是当我与jconsole连接时,MXBean上的所有基本属性都将显示为UNDEFINED。

Java interface/classes: Java接口/类:

public interface IJmxBean { // marker interface for spring config, see below
}

public interface MgmtMXBean { // lexical convention for MXBeans - mgmt interface
    public int getAttribute();
}

public class Mgmt implements IJmxBean, MgmtMXBean { // actual JMX bean
    private IServiceBean serviceBean;    // service bean injected by Spring
    private int attribute = 0;

    @Override
    public int getAttribute() {
        if(serviceBean != null) {
            attribute = serviceBean.getRequestedAttribute();
        }
        return attribute;
    }

    public void setServiceBean(IServiceBean serviceBean) { 
        this.serviceBean = serviceBean;
    }
}

Spring JMX config: Spring JMX配置:

<beans>
    <context:component-scan base-package="...">
        <context:include-filter type="assignable" expression="...IJmxBean" />
    </context:component-scan>
    <context:mbean-export />
</beans>

Here's what I know so far: 这是我目前所知道的:

  • The element is correctly instantiating a bean named "mgmt". 该元素正确地实例化了一个名为“ mgmt”的bean。 I've got logging in a zero-argument public constructor that indicates it gets constructed. 我已经登录了一个零参数的公共构造函数,该构造函数表明它已被构造。

  • is correctly automatically detecting and registering the MgmtMXBean interface with my Tomcat 6.0 container. 在我的Tomcat 6.0容器中正确地自动检测并注册了MgmtMXBean接口。 I can connect to the MBeanServer in Tomcat with jconsole and drill down to the Mgmt MXBean. 我可以使用jconsole连接到Tomcat中的MBeanServer,然后深入到Mgmt MXBean。

  • When examining the MXBean, "Attribute" is always listed as UNDEFINED, but jconsole can tell the correct type of the attribute. 检查MXBean时,“属性”总是列为UNDEFINED,但是jconsole可以告诉属性正确的类型。 Further, hitting "Refresh" in jconsole does not actually invoke the getter method of "Attribute"- I have logging in the getter method to indicate if it is being invoked (similar to the constructor logging that works) and I see nothing in the logs. 此外,在jconsole中单击“刷新”实际上不会调用“属性”的getter方法-我已登录getter方法以指示是否正在调用它(类似于有效的构造方法日志记录),并且在日志中什么也看不到。

At this point I'm not sure what I'm doing wrong. 在这一点上,我不确定自己在做什么错。 I've tried a number of things, including constructing an explicit Spring MBeanExporter instance and registering the MXBean by hand, but it either results in the MBean/MXBean not getting registered with Tomcat's MBean server or an Attribute value of UNDEFINED. 我已经尝试了很多方法,包括构造一个显式的Spring MBeanExporter实例并手动注册MXBean,但这会导致MBean / MXBean未在Tomcat的MBean服务器上注册或Attribute值为UNDEFINED。

For various reasons, I'd prefer not to have to use Spring's @ManagedResource/@ManagedAttribute annotations. 由于各种原因,我不希望不必使用Spring的@ ManagedResource / @ ManagedAttribute批注。

Is there something that I'm missing in the Spring docs or MBean/MXBean specs? Spring文档或MBean / MXBean规范中是否缺少某些内容?

ISSUE RESOLVED: Thanks to prompting by Jon Stevens (above), I went back and re-examined my code and Spring configuration files: 已解决的问题:感谢乔恩·史蒂文斯(Jon Stevens)的提示(以上),我回过头来重新检查了我的代码和Spring配置文件:

Throwing an exception in the getAttribute() method is a sure way to get "Unavailable" to show up as the attribute's value in JConsole. getAttribute()方法中引发异常是确保“不可用”显示为JConsole中的属性值的肯定方法。 In my case: 就我而言:

  • The Spring JMX config file I was using was lacking the default-autowire="" attribute on the root <beans> element; 我使用的Spring JMX配置文件在根<beans>元素上缺少default-autowire=""属性;
  • The code presented above checks to see if serviceBean != null . 上面提供的代码检查是否有serviceBean != null Apparently I write better code on stackoverflow.com than in my test code, since my test code wasn't checking for that. 显然,我在stackoverflow.com上编写的代码比在我的测试代码中写的更好,因为我的测试代码并未对此进行检查。 Nor did I have implements InitializingBean or @PostConstruct to check for serviceBean != null like I normally do on almost all the other beans I use; 我也没有implements InitializingBean@PostConstruct来检查serviceBean != null就像我通常在使用的几乎所有其他bean上一样。
  • The code invoking the service bean was before the logging, so I never saw any log messages about getter methods being entered; 调用服务Bean的代码在记录之前,因此我从未看到有关输入getter方法的任何日志消息。
  • JConsole doesn't report when attribute methods throw exceptions; 当属性方法抛出异常时,JConsole不会报告。
  • The NPE did not show up in the Tomcat logs. NPE没有显示在Tomcat日志中。

Once I resolved the issue with serviceBean == null , everything worked perfectly. 一旦解决了serviceBean == null的问题,一切就可以正常工作。 Regardless, +1 to Jon for providing a working demo, since there are literally 50 different ways to configure MBeans/MXBeans within Spring. 无论如何,请Jon +1以提供有效的演示,因为实际上有50种不同的方法可以在Spring中配置MBean / MXBean。

I've recently built a sample Spring based webapp that very cleanly enables JMX for latest versions of Spring, Hibernate and Ehcache. 我最近构建了一个基于Spring的示例Web应用程序,该应用程序非常干净地为JMX启用了Spring,Hibernate和Ehcache的最新版本。

It has examples for both EntityManager based access and DAO access (including transactions!). 它具有基于EntityManager和DAO访问(包括事务!)的示例。 It also shows how to do annotation based injection in order to negate having to use Spring's xml config for beans. 它还显示了如何进行基于注解的注入,以便不必为豆使用Spring的xml配置。 There is even a SpringMVC based example servlet using annotations. 甚至还有一个使用注释的基于SpringMVC的示例servlet。 Basically, this is a Spring based version of a fairly powerful application server running on top of any servlet engine. 基本上,这是运行在任何servlet引擎之上的功能强大的应用程序服务器的基于Spring的版本。

It isn't documented yet, but I'll get to that soon. 它尚未记录,但我会尽快解决。 Take a look at the configuration files and source code and it should be pretty clear. 看一下配置文件和源代码,它应该很清楚。

The motivation behind this is that I got tired of all of the crazy blog posts with 50 different ways to set things up and finally made a single simple source that people can work from. 其背后的动机是,我厌倦了用50种不同方式进行设置的所有疯狂博客文章,并最终使人们可以使用一个简单的来源。 It is up on github so feel free to fork the project and do whatever you want with it. 它位于github上,因此可以随意分叉该项目并对其进行任何操作。

https://github.com/lookfirst/fallback https://github.com/lookfirst/fallback

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM