Skip to content

Commit 51dfd52

Browse files
izeyesnicoll
authored andcommitted
Close TomcatMetrics on shutdown for cleanup
See micrometer-metrics/micrometer#1424 See gh-19880
1 parent c3d0b9c commit 51dfd52

File tree

1 file changed

+11
-2
lines changed
  • spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/tomcat

1 file changed

+11
-2
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/tomcat/TomcatMetricsBinder.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.catalina.Context;
2626
import org.apache.catalina.Manager;
2727

28+
import org.springframework.beans.factory.DisposableBean;
2829
import org.springframework.boot.context.event.ApplicationStartedEvent;
2930
import org.springframework.boot.web.context.WebServerApplicationContext;
3031
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
@@ -38,12 +39,14 @@
3839
* @author Andy Wilkinson
3940
* @since 2.1.0
4041
*/
41-
public class TomcatMetricsBinder implements ApplicationListener<ApplicationStartedEvent> {
42+
public class TomcatMetricsBinder implements ApplicationListener<ApplicationStartedEvent>, DisposableBean {
4243

4344
private final MeterRegistry meterRegistry;
4445

4546
private final Iterable<Tag> tags;
4647

48+
private volatile TomcatMetrics tomcatMetrics;
49+
4750
public TomcatMetricsBinder(MeterRegistry meterRegistry) {
4851
this(meterRegistry, Collections.emptyList());
4952
}
@@ -57,7 +60,8 @@ public TomcatMetricsBinder(MeterRegistry meterRegistry, Iterable<Tag> tags) {
5760
public void onApplicationEvent(ApplicationStartedEvent event) {
5861
ApplicationContext applicationContext = event.getApplicationContext();
5962
Manager manager = findManager(applicationContext);
60-
new TomcatMetrics(manager, this.tags).bindTo(this.meterRegistry);
63+
this.tomcatMetrics = new TomcatMetrics(manager, this.tags);
64+
this.tomcatMetrics.bindTo(this.meterRegistry);
6165
}
6266

6367
private Manager findManager(ApplicationContext applicationContext) {
@@ -80,4 +84,9 @@ private Context findContext(TomcatWebServer tomcatWebServer) {
8084
return null;
8185
}
8286

87+
@Override
88+
public void destroy() {
89+
this.tomcatMetrics.close();
90+
}
91+
8392
}

0 commit comments

Comments
 (0)