GH-3425: Remove mngmt gauges from CtxClosedEvent

Fixes https://github.com/spring-projects/spring-integration/issues/3425

It turns out that `IntegrationManagementConfigurer.destroy()` is still too
late to remove `gauges` and Micrometer tries to gather them on application
context close, when many beans are already destroyed

* Catch `ContextClosedEvent` in the `IntegrationManagementConfigurer`
and removed `gauges` from there
* Remove `destroy()` impl since it is out of use already:
the `IntegrationManagementConfigurer` is not supposed to be in the
target application directly, so it looks safe to remove the `DisposableBean`
altogether

**Cherry-pick to `5.3.x` & `5.2.x`**
This commit is contained in:
Artem Bilan
2020-11-10 12:25:30 -05:00
committed by Gary Russell
parent 0331933c16
commit fce0fef98f

View File

@@ -23,11 +23,12 @@ import java.util.Set;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.support.management.IntegrationManagement;
import org.springframework.integration.support.management.IntegrationManagement.ManagementOverrides;
@@ -53,7 +54,7 @@ import org.springframework.util.Assert;
*/
public class IntegrationManagementConfigurer
implements SmartInitializingSingleton, ApplicationContextAware, BeanNameAware, BeanPostProcessor,
DisposableBean {
ApplicationListener<ContextClosedEvent> {
/**
* Bean name of the configurer.
@@ -169,10 +170,11 @@ public class IntegrationManagementConfigurer
.build());
}
@Override
public void destroy() {
this.gauges.forEach(MeterFacade::remove);
this.gauges.clear();
@Override public void onApplicationEvent(ContextClosedEvent event) {
if (event.getApplicationContext().equals(this.applicationContext)) {
this.gauges.forEach(MeterFacade::remove);
this.gauges.clear();
}
}
private static ManagementOverrides getOverrides(IntegrationManagement bean) {