From fce0fef98fd766ef0f7acd821276189f8e7b1f41 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 10 Nov 2020 12:25:30 -0500 Subject: [PATCH] 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`** --- .../config/IntegrationManagementConfigurer.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfigurer.java b/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfigurer.java index cc22e72bf4..7c1ee05e4b 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfigurer.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfigurer.java @@ -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 { /** * 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) {