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
This commit is contained in:
Artem Bilan
2020-11-10 12:25:30 -05:00
parent 927ba8f033
commit 72232ea026

View File

@@ -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<ContextClosedEvent> {
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();
}