From 72232ea02663f47e982a6c062ab82ed886f2cc85 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`** # Conflicts: # spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfigurer.java --- .../IntegrationManagementConfigurer.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 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 a86796215d..c5f7eac2e3 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 @@ -28,11 +28,12 @@ import org.apache.commons.logging.LogFactory; 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.DestructionAwareBeanPostProcessor; 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; @@ -61,7 +62,7 @@ import org.springframework.util.StringUtils; @SuppressWarnings("deprecation") public class IntegrationManagementConfigurer implements SmartInitializingSingleton, ApplicationContextAware, BeanNameAware, - DestructionAwareBeanPostProcessor, DisposableBean { + DestructionAwareBeanPostProcessor, ApplicationListener { private static final Log LOGGER = LogFactory.getLog(IntegrationManagementConfigurer.class); @@ -468,6 +469,13 @@ public class IntegrationManagementConfigurer .build()); } + @Override public void onApplicationEvent(ContextClosedEvent event) { + if (event.getApplicationContext().equals(this.applicationContext)) { + this.gauges.forEach(MeterFacade::remove); + this.gauges.clear(); + } + } + public String[] getChannelNames() { return this.channelsByName.keySet().toArray(new String[0]); } @@ -518,13 +526,6 @@ public class IntegrationManagementConfigurer return null; } - @Override - public void destroy() { - this.gauges.forEach(MeterFacade::remove); - this.gauges.clear(); - } - - private static ManagementOverrides getOverrides(IntegrationManagement bean) { return bean.getOverrides() != null ? bean.getOverrides() : new ManagementOverrides(); }