Skip to content

Commit 4bcd02a

Browse files
committed
Merge pull request #19880 from izeye
* pr/19880: Polish "Close TomcatMetrics on shutdown for cleanup" Close TomcatMetrics on shutdown for cleanup Closes gh-19880
2 parents c3d0b9c + 7fd8cce commit 4bcd02a

File tree

1 file changed

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

1 file changed

+12
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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)