提问者:小点点

在JMX中将ThreadPoolTaskExector公开为MBean


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

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

我在配置类中创建了一个<code>ThreadPoolTaskExecutor</code>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. 这是最有效的方法吗?在豆子上面没有可以放的注释吗?@ManagedOperation在方法级别不起作用。

共1个答案

匿名用户

这里有一种方法…

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

它会被boot的出口商取走。

编辑

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

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