序
自带的tomcat的metrics过于简单,没有线程池的任务队列信息,故这里扩展增加一下,方便监控。
参考TomcatPublicMetrics
public class AdvancedTomcatMetrics implements PublicMetrics,ApplicationContextAware{
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public Collection<Metric<?>> metrics() {
if (this.applicationContext instanceof EmbeddedWebApplicationContext) {
EmbeddedServletContainer embeddedServletContainer = ((EmbeddedWebApplicationContext)applicationContext)
.getEmbeddedServletContainer();
if (embeddedServletContainer instanceof TomcatEmbeddedServletContainer) {
Connector connector = ((TomcatEmbeddedServletContainer) embeddedServletContainer).getTomcat().getConnector();
ProtocolHandler handler = connector.getProtocolHandler();
org.apache.tomcat.util.threads.ThreadPoolExecutor executor = (ThreadPoolExecutor) handler.getExecutor();
//register tomcat thread pool stat
List<Metric<?>> metrics = new ArrayList<Metric<?>>();
metrics.add(new Metric<Integer>("tomcat.threads.active_count",executor.getActiveCount()));
metrics.add(new Metric<Integer>("tomcat.threads.largest_pool_size",executor.getLargestPoolSize()));
metrics.add(new Metric<Long>("tomcat.threads.task_count",executor.getTaskCount()));
metrics.add(new Metric<Long>("tomcat.threads.completed_task_count",executor.getCompletedTaskCount()));
metrics.add(new Metric<Integer>("tomcat.threads.submitted_count",executor.getSubmittedCount()));
// metrics.add(new Metric<Integer>("tomcat.threads.pool_size",executor.getPoolSize()));
// metrics.add(new Metric<Integer>("tomcat.threads.core_pool_size",executor.getCorePoolSize()));
// metrics.add(new Metric<Integer>("tomcat.threads.max_pool_size",executor.getMaximumPoolSize()));
return metrics;
}
}
return Collections.emptySet();
}
}
自动配置
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass({ Servlet.class, Tomcat.class })
@ConditionalOnWebApplication
public AdvancedTomcatMetrics advancedTomcatMetrics(){
return new AdvancedTomcatMetrics();
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。