How to set notification listener mappings with Spring JMX integration when I'm using @EnableMBeanExport

242 views Asked by At

As Spring reference https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#jmx-notifications-listeners said, I need to set listener mappings by invoking MBeanExporter.setNotificationListenerMappings method after the notification listener is declared.

When using XML-based config or @Bean annotation config to declare a MBeanExporter explicitly, the setNotificationListenerMappings operation is easily done. As the following code shows:

@Bean
public AnnotationMBeanExporter mBeanExporter() {
    Map<String, JmxNotificationListener> mappings = new HashMap<>();
    mappings.put("com.foo.spring-jmx-test:name=JmxService", new JmxNotificationListener());

    AnnotationMBeanExporter exporter = new AnnotationMBeanExporter();
    exporter.setRegistrationPolicy(RegistrationPolicy.IGNORE_EXISTING);
    exporter.setNotificationListenerMappings(mappings);
    return exporter;
}

But when using @EnableMBeanExport, which will automatically define a AnnotationMBeanExporter, I can't find a way to set listener mappings to MBeanExporter. So, is there a way to set notification listener mappings when I using @EnableMBeanExport?

Thanks.

1

There are 1 answers

0
Artem Bilan On BEST ANSWER

The @EnableMBeanExport registers in the application context an AnnotationMBeanExporter bean, so you can inject it into some your configuration and perform such a mapping registration:

@Autowired
AnnotationMBeanExporter exporter;

@PostConstruct
public void init() {
    this.exporter.setNotificationListenerMappings(...);
}