简体   繁体   English

导出带有Spring JMX注释的对象

[英]Export an object with Spring JMX annotations

I'm trying to export some information with Spring JMX annotation driven (I have no xml :D). 我正在尝试使用Spring JMX批注驱动来导出一些信息(我没有xml:D)。 I have achieved to export Strings and Integer types but I haven't been able to export any object of a class I created. 我已经实现了导出Strings和Integer类型的功能,但是还无法导出我创建的类的任何对象。

This is my @Configuration class. 这是我的@Configuration类。

@Configuration
@EnableMBeanExport
@ComponentScan({"my.packages"})
public class AppManager {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(AppManager.class);
        context.refresh();


        try {
            TimeUnit.MINUTES.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Bean(name = "jmxExporter")
    public Exporter jmxExporter() {
        return new Exporter();
    }
}

This is some class I have with some attributes I want to get. 这是我拥有的类,具有要获取的某些属性。

public class MyClass implements Serializable {

    private int param1;
    private int param2;
    private int param3;

    public MyClass() {
        // calculate all params
    }

    public int getParam1() {
        return this.param1;
    }

    public int getParam2() {
        return this.param2;
    }

    public int getParam3() {
        return this.param3;
    }

}

This is the class that exports it's attributes. 这是导出其属性的类。

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

@ManagedResource(objectName = "my.packages:type=JMX,name=Exporter")
public class Exporter {

    @ManagedAttribute
    public String getString() { //This works!
        return "Test string";
    }

    @ManagedAttribute
    public MyClass getMyClass() { //This does not work
        return new MyClass();
    }

}

I need to create MyClass object every time because it has some real-time information that I can't export separately. 我需要每次创建MyClass对象,因为它具有一些无法单独导出的实时信息。

In JConsole the value of attribute is "Unavailable". 在JConsole中,属性的值为“不可用”。

I'm pretty new to JMX and obviously missing something. 我对JMX很陌生,很明显缺少一些东西。

Thank you for your help! 谢谢您的帮助!

I resolved it by returning CompositeData. 我通过返回CompositeData解决了它。

@ManagedAttribute
public CompositeData getMyClass() { 
    return createCompositeDataForMyClass();
}

I built a CompositeDataSupport for that and it worked. 我为此构建了一个CompositeDataSupport,它可以工作。

return new CompositeDataSupport(compositeType, itemNames, itemValues);

Where compositeType is a CompositeType, itemNames is a String[] and itemValues is an Object[]. 其中CompositeType是CompositeType,itemNames是String [],itemValues是Object []。

The CompositeType can be built with something like this CompositeType可以用这样的东西来构建

new CompositeType(typeName, typeDescription, itemNames, itemDescriptions, itemTypes);

typeName, typeDescription are Strings. typeName,typeDescription是字符串。 itemNames and itemDescriptions are String[]. itemNames和itemDescriptions是String []。 itemTypes is an OpenType[]. itemTypes是一个OpenType []。 SimpleType and CompositeType can be used to build OpenType. SimpleType和CompositeType可用于构建OpenType。

All this objects must be imported with 所有这些对象必须使用

import javax.management.openmbean.*;

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

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