java - Mbean JMX Spring 框架

标签 java spring spring-jmx

我有一个 Web 应用程序,其中有超过 40 个 Mbean。我使用Spring框架。

我做得很好,而且运作良好。但我有 40 Mbean,所以想概括一下。

@Component
@ManagedResource(objectName="ProjectCache:name=XMBean", log=true, logFile="jmx.log")
public class XMBean extends AbstractCacheMBean<String, XCO, XCache> { 

@ManagedOperation(description ="ProjectCache XCO key")
    @Override
    public List<String> showAllKeys(){  
        return super.getKey();      
    }

@ManagedOperation(description ="ProjectCache XCO")
public List<String> showAllElements(){  
    return super.findAll();     
}

@Override
public XCache getCache() {
    return getFacadeCache().getXCache();
}

@ManagedOperation(description ="ProjectCache XCO by key)
@Override
public String ShowbyKey(String key) {
    return super.findbyKey(key);
}

}

现在我有相同的方式类 YMbean、AMBean 等。

我在应用程序 mbean.xml 中配置了 Spring。

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

    <!-- this bean must not be lazily initialized if the exporting is to happen -->
    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
        <property name="server" ref="mbeanServer"/>
        <property name="assembler" ref="assembler" />
        <property name="namingStrategy" ref="namingStrategy" />         
    </bean>

    <bean id="jmxAttributeSource" class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" />

    <!-- will create management interface using annotation metadata -->
    <bean id="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
        <property name="attributeSource" ref="jmxAttributeSource" />
    </bean>

    <!-- will pick up the ObjectName from the annotation -->
    <bean id="namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
        <property name="attributeSource" ref="jmxAttributeSource" />
    </bean>

    <bean id="xMBean" 
        class="in.projet.business.mbean.XMBean"> 
        <property name="memoryCache" ref="repository" /> 
</bean>

同样,我将准备 YMbean 类并在 xml 中进行初始化。

我应该做什么而不需要修改 XML 无论我创建的类的数量或数量,都不需要更新 XML。

我将要使用的所有 Mbean 的属性都是相同的。 欢迎所有想法或意见。

谢谢

最佳答案

删除所有配置并使用命名空间替换,并且仅使用一次。您的 MBean 也是 @Components这样您就可以简单地扫描它们。这只会让您留下以下 xml 行

<context:component-scan base-package="in.projet.business.mbean" />
<context:mbean-export/>

或者,如果您想保留当前配置而不是命名空间,请至少将其替换为以下内容并删除所有其他 bean。这可以在您的应用程序上下文中自动检测 MBean(这与 <context:mbean-export /> 的作用基本相同。

有关更多信息,我强烈建议JMX chapter引用指南。

关于java - Mbean JMX Spring 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23383831/

相关文章:

java - 在 mongo 和 spring boot 中使用具有未确定架构的 querydsl

java - 公共(public)静态无效是什么意思?什么是类(class)?

xml - 将 activemq 代理配置拆分为多个文件

java - MBean 未显示在 jvisualVM 中

java - 如何针对不同浏览器运行导出的 Selenium IDE 测试

java - Eclipse 上的 Android 原生 c++ - 在 stdlib 上给出错误,但可以工作

java - 在jsf中使用spring时的bean实例化异常

java - Spring MVC 3.2.3 Tiles 2 HTTP Status 500 - Servlet appServlet 的 Servlet.init() 引发异常

java - 如何从 JMX 服务器获取 Mbean

Java Spring Framework jmx 托管注释@ManagedAttribute 未在 MBeanServerConnection/Jconsole/Visual vm/bean 列表中显示方法