Search code examples
javaspringjmx

Dynamically instantiate JMX MBeans


I have a white label/multi-tennant server providing the same services, but branded for multiple customers. I want to use JMX to monitor the status of each customer (nbr of calls, nbr of errors, etc).

I know how to use Spring JMX annotations to wire up my POJOs (per the code below). What I really want is one MBean per customer, but because the customers are dynamically loaded up at server startup time I don't know how to wire this up using annotations.

Is this possible? If not, is it possible to instantiate my MBeans at startup time ?

@Component
@ManagedResource(objectName = "TravelAPI:name=Customer") 
public class CustomerStatus extends GeneralCustomerStatus {

    @ManagedAttribute
    String customerId;
    .
    .
    .


}

Solution

  • What I really want is one MBean per customer, but because the customers are dynamically loaded up at server startup time I don't know how to wire this up using annotations.

    What we do is to have the entity that is actually instantiating your dynamic objects, register them with JMX via the MBeanExporter. We inject the MBeanExporter instance into the factory entity and then call MBeanExporter.registerManagedResource(...).

    For example we do something like:

    ...
    mbeanExporter.registerManagedResource(beanInstance);
    ...
    
    @Required
    public void setMbeanExporter(MBeanExporter mbeanExporter) {
        this.mbeanExporter = mbeanExporter;
    }
    

    We also use a NamingPolicy so that the dynamic objects can provide their own names to make them unique. See more details about that here:

    Change @ManagedResource objectName dynamically

    As an aside, my SimpleJMX package has some code to help with dynamic objects.