在 JMX 中将 ThreadPoolTask​​Executor 公开为 MBean

Exposing ThreadPoolTaskExecutor as MBean in JMX

我已经在我的 Spring 引导项目中从 ThreadPoolExecutor 切换到 ThreadPoolTaskExecutor 只是因为根据它的文档:

This class is well suited for management and monitoring (e.g. through JMX)

我在我的配置 class 中创建了一个 ThreadPoolTaskExecutor 的 bean,如下所示:

  @Bean
  ThreadPoolTaskExecutor profileTaskExecutor() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setThreadGroupName(getClass().getSimpleName());
    taskExecutor.setCorePoolSize(corePoolSize);
    taskExecutor.setMaxPoolSize(maxPoolSize);
    taskExecutor.setKeepAliveSeconds(KEEP_ALIVE_MINUTES);
    taskExecutor.setQueueCapacity(1);
    taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
    return taskExecutor;
  }

  @Bean
  protected MBeanExporter mbeanExporter() {
    MBeanExporter exporter = new MBeanExporter();
    Map<String, Object> beans = new HashMap<>();
    beans.put("org.springframework.boot:type=Executors,name=ProfileServiceExecutor", profileTaskExecutor());
    exporter.setBeans(beans);
    return exporter;
  }

这运行良好并通过 JMX 公开了我的 ThreadPoolTaskExecutor。现在的问题是,因为我正在创建一个新的 MBeanExporter,所以我的其他 ManagedOperations 被覆盖并且没有出现在 JConsole 中。现在我的问题是:

  1. 有没有办法将 ThreadPoolTaskExecutor 添加到现有的托管 bean。我已经尝试过但未能成功。
  2. 这是最有效的方法吗?我可以在 bean 上面添加任何注释吗? @ManagedOperation 不适用于方法级别。

这是一种方法...

@ManagedResource
public class MyExecutor extends ThreadPoolTaskExecutor {

    private static final long serialVersionUID = 1L;

    @ManagedAttribute
    @Override
    public int getCorePoolSize() {
        return super.getCorePoolSize();
    }

    @ManagedAttribute
    @Override
    public int getMaxPoolSize() {
        return super.getMaxPoolSize();
    }

    @ManagedAttribute
    @Override
    public int getKeepAliveSeconds() {
        return super.getKeepAliveSeconds();
    }

    @ManagedAttribute
    @Override
    public int getPoolSize() {
        return super.getPoolSize();
    }

    @ManagedAttribute
    @Override
    public int getActiveCount() {
        return super.getActiveCount();
    }

}

@Bean
MyExecutor exec() {
    MyExecutor taskExecutor = new MyExecutor();
    taskExecutor.setThreadGroupName(getClass().getSimpleName());
    taskExecutor.setCorePoolSize(10);
    taskExecutor.setMaxPoolSize(20);
    taskExecutor.setKeepAliveSeconds(5);
    taskExecutor.setQueueCapacity(1);
    taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
    return taskExecutor;
}

它将被引导的导出器拾取。

编辑

或者,简单地覆盖引导的默认自动配置导出器...

@Bean
@Primary
public AnnotationMBeanExporter mbeanExporter(ObjectNamingStrategy namingStrategy,
        Environment environment, BeanFactory beanFactory) {

    AnnotationMBeanExporter exporter = new AnnotationMBeanExporter();
    exporter.setRegistrationPolicy(RegistrationPolicy.FAIL_ON_EXISTING);
    exporter.setNamingStrategy(namingStrategy);
    String serverBean = environment.getProperty("spring.jmx.server",
            "mbeanServer");
    if (StringUtils.hasLength(serverBean)) {
        exporter.setServer(beanFactory.getBean(serverBean, MBeanServer.class));
    }
    Map<String, Object> beans = new HashMap<>();
    beans.put("org.springframework.boot:type=Executors,name=ProfileServiceExecutor", profileTaskExecutor());
    exporter.setBeans(beans);
    return exporter;
}