mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Close TomcatMetrics on shutdown for cleanup
See micrometer-metrics/micrometer#1424 See gh-19880
This commit is contained in:
parent
c3d0b9c43a
commit
51dfd528bd
@ -25,6 +25,7 @@ import org.apache.catalina.Container;
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.Manager;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||
import org.springframework.boot.web.context.WebServerApplicationContext;
|
||||
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
|
||||
@ -38,12 +39,14 @@ import org.springframework.context.ApplicationListener;
|
||||
* @author Andy Wilkinson
|
||||
* @since 2.1.0
|
||||
*/
|
||||
public class TomcatMetricsBinder implements ApplicationListener<ApplicationStartedEvent> {
|
||||
public class TomcatMetricsBinder implements ApplicationListener<ApplicationStartedEvent>, DisposableBean {
|
||||
|
||||
private final MeterRegistry meterRegistry;
|
||||
|
||||
private final Iterable<Tag> tags;
|
||||
|
||||
private volatile TomcatMetrics tomcatMetrics;
|
||||
|
||||
public TomcatMetricsBinder(MeterRegistry meterRegistry) {
|
||||
this(meterRegistry, Collections.emptyList());
|
||||
}
|
||||
@ -57,7 +60,8 @@ public class TomcatMetricsBinder implements ApplicationListener<ApplicationStart
|
||||
public void onApplicationEvent(ApplicationStartedEvent event) {
|
||||
ApplicationContext applicationContext = event.getApplicationContext();
|
||||
Manager manager = findManager(applicationContext);
|
||||
new TomcatMetrics(manager, this.tags).bindTo(this.meterRegistry);
|
||||
this.tomcatMetrics = new TomcatMetrics(manager, this.tags);
|
||||
this.tomcatMetrics.bindTo(this.meterRegistry);
|
||||
}
|
||||
|
||||
private Manager findManager(ApplicationContext applicationContext) {
|
||||
@ -80,4 +84,9 @@ public class TomcatMetricsBinder implements ApplicationListener<ApplicationStart
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
this.tomcatMetrics.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user