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

时间:2018-11-28 12:45:30

标签: spring spring-boot spring-jmx

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

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

我在配置类中创建了一个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在方法级别不起作用。

1 个答案:

答案 0 :(得分:0)

这是一种方法...

esp+5

esp+10

它将由引导程序的导出器提取。

编辑

或者,只需覆盖引导程序的默认自动配置的导出程序...

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

}