spring - 将工厂创建的 SimpleMessageListenerContainer 公开为 mbean

spring - exposing SimpleMessageListenerContainer created from factory as mbean

使用 spring 4. 我正在创建一个

SimpleMessageListenerContainer

来自

的工厂方法
@Configuration @Bean

        @Bean( name = "container" )
        SimpleMessageListenerContainer container(){
...
...

如何在 JMX 上公开这个实例?由于@ManagedResource 只能应用于 class

@Bean
public MBeanExporter exporter() {
    MBeanExporter mBeanExporter = new MBeanExporter();
    mBeanExporter.setBeans(
        Collections.singletonMap("myDomain:type=container,name=myContainter", "container"));
    return mBeanExporter;
}

这将暴露所有 public methods/attributes。如果你想限制暴露的内容,你可以使用 MethodNameBasedMBeanInfoAssembler:

@Bean
public MBeanExporter exporter() {
    MBeanExporter mBeanExporter = new MBeanExporter();
    mBeanExporter.setBeans(
        Collections.singletonMap("myDomain:type=container,name=myContainter", "container"));
    MethodNameBasedMBeanInfoAssembler assembler = new MethodNameBasedMBeanInfoAssembler();
    assembler.setManagedMethods(new String[] { "start", "stop" });
    mBeanExporter.setAssembler(assembler);
    return mBeanExporter;
}