当我使用@EnableMBeanExport时,如何使用Spring JMX集成设置通知侦听器映射

问题描述 投票:0回答:1

正如Spring引用https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#jmx-notifications-listeners所说,我需要在声明通知侦听器之后通过调用MBeanExporter.setNotificationListenerMappings方法来设置侦听器映射。

当使用基于XML的配置或@Bean批注配置明确声明MBeanExporter时,setNotificationListenerMappings操作很容易完成。如下面的代码所示:

@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;
}

但是当使用自动定义AnnotationMBeanExporter的@EnableMBeanExport时,我找不到将听众映射设置为MBeanExporter的方法。那么,当我使用@EnableMBeanExport时,有没有办法设置通知监听器映射?

谢谢。

java spring spring-integration jmx mbeanexporter
1个回答
1
投票

@EnableMBeanExport在应用程序上下文中注册了一个AnnotationMBeanExporter bean,因此您可以将其注入到某些配置中并执行这样的映射注册:

@Autowired
AnnotationMBeanExporter exporter;

@PostConstruct
public void init() {
    this.exporter.setNotificationListenerMappings(...);
}
© www.soinside.com 2019 - 2024. All rights reserved.