Spring JMX Mbean 未出现或未在 jconsole 中注册

标签 spring tomcat jmx spring-jmx

只是在本地 tomcat 服务器上的 eclipse 中尝试一个简单的 Spring JMX 应用程序,但似乎无法注册 mbean,因此它们可以在 jconsole 中查看,在 eclipse 上下文中:component-scan 似乎确实拾取了 beans我创建了但是这些没有注册。当以编程方式注册 mbean 时,它会起作用。

这是我的配置 xml 文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id="mbeanServer" class="java.lang.management.ManagementFactory"
lazy-init="false" factory-method="getPlatformMBeanServer">
</bean>

<context:component-scan base-package="com.jmx.beans" />
<context:mbean-export server="mbeanServer" />

</beans>

我正在尝试使用注解注册的简单 bean

package com.jmx.beans;

import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.stereotype.Component;

@Component
@ManagedResource(objectName="bean:name=Hello")
public class Hello{

String message =  null;

@ManagedAttribute(description="get the message")
public String getMessage(){

    return this.message;
}

@ManagedAttribute(description="set the message")
public void setMessage(String Message){

    this.message = Message;

}
}

我还设置了 tomcat 服务器参数如下

-Dcom.sun.management.jmxremote 
-Dcom.sun.management.jmxremote.port=9990
-Dcom.sun.management.jmxremote.ssl=false 
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.hostname="localhost"

如有任何帮助,我们将不胜感激,谢谢

最佳答案

您为什么要编辑帖子以删除 <context:component-scan/> ?这是找到您的 @Component 所必需的.

我刚刚测试过,对我来说一切正常......

@Component
@ManagedResource
public class Foo {

    @ManagedAttribute
    public int getIt() {
        return 42;
    }
}

<context:mbean-server/>

<context:component-scan base-package="foo" />

<context:mbean-export/>

我用你的 MBean 服务器风格试过了,也很​​管用。

关于Spring JMX Mbean 未出现或未在 jconsole 中注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15411321/

相关文章:

jmx - 如何通过 JMX 远程连接 Flink?

spring - 微服务 : security and architectural issue for internal services

java - 使用 Processbuilder 从 Java Web 应用程序调用 Python 脚本

java - 使用 Spring Boot 自动转换 RequestBody 期间,ZonedDateTime 的时区更改为 UTC

spring-mvc - 在同一 jvm 中部署的多个 war 中使用 cassandra 驱动程序时出现 jmx 错误

java - Raspberry Debian Squeeze 中的安全管理器 Tomcat 7

java - 有什么可能的方法来指定正在运行的java虚拟机的名称吗?

java - Spring Java 配置 : how do you create a prototype-scoped @Bean with runtime arguments?

java - catelina.properties 中的 ${catalina.base} 未被 CATALINA_BASE 替换

java - 使用 Prometheus 和 Grafana 监控基于 Jetty 的 Java 应用程序