Search code examples
javahashmapjmxmbeans

Java Mbean attributes from HashMap


I currently have a bean which has a lot of attributes/ properties and I always have to create the interface and aswell the implementation of the interface. I want to avoid rewriting code and make my Bean more flexible and simple.

Here is my current bean:

public interface StatisticsMBean {
    public String getPropA();
    public String getPropB();
    public String getPropC();
    ...
}

public Statistics implements StatisticsMBean {
    public String propA;
    public String propB;
    public String propC;
    ...

    public String getPropA() {
        ...
    }

    public String getPropB() {
        ...
    }

    public String getPropC() {
        ...
    }

    ...
}

My idea was to put all properties/ attributes in a HashMap but I have no Idea how this could work because how would the interface look or is there another way?

I still want to be able to see all attributes later on in jconsole

Regards, S


Solution

  • Finally I found some code that was quiet helpful for my problem. It does not exactly do what I described above but it explains how to implement a DynamicMBean. It can easily be adopted for my solution.