在我的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中。现在我的问题是:
ThreadPoolTaskExecutor
添加到现有的托管bean中。我已经尝试过但无法成功。@ManagedOperation
在方法级别不起作用。 答案 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();
}
}