Fix memory leak in MHistorConfig & MBeanExpHelper (#2531)

* Fix memory leak in MHistorConfig & MBeanExpHelper

The `MessageHistoryConfigurer` is a `BeanPostProcessor` which keeps a
store of the `TrackableComponent`.
Something similar we have with the `MBeanExporterHelper` and its local
stores populated by the `postProcessBeforeInitialization()`

* Implement `DestructionAwareBeanPostProcessor` in the
`MessageHistoryConfigurer` and `MBeanExporterHelper` to remove destroyed
beans from their caches

**Cherry-pick to 5.0.x**

* * Fix concurrency and checkstyle
* Apply Java 8 style for collections iterations
This commit is contained in:
Artem Bilan
2018-07-27 15:50:09 -04:00
committed by Gary Russell
parent adbbca13fb
commit 55a43d37c8
2 changed files with 51 additions and 27 deletions

View File

@@ -29,7 +29,7 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionValidationException;
import org.springframework.context.SmartLifecycle;
import org.springframework.integration.support.management.IntegrationManagedResource;
@@ -50,13 +50,13 @@ import org.springframework.util.StringUtils;
*/
@ManagedResource
@IntegrationManagedResource
public class MessageHistoryConfigurer implements SmartLifecycle, BeanFactoryAware, BeanPostProcessor {
public class MessageHistoryConfigurer implements SmartLifecycle, BeanFactoryAware, DestructionAwareBeanPostProcessor {
private final Log logger = LogFactory.getLog(this.getClass());
private final Set<TrackableComponent> currentlyTrackedComponents = ConcurrentHashMap.newKeySet();
private String[] componentNamePatterns = new String[] { "*" };
private String[] componentNamePatterns = new String[]{ "*" };
private boolean componentNamePatternsExplicitlySet;
@@ -98,7 +98,7 @@ public class MessageHistoryConfigurer implements SmartLifecycle, BeanFactoryAwar
*/
@ManagedAttribute(description = "comma-delimited list of patterns; must invoke stop() before changing.")
public void setComponentNamePatternsString(String componentNamePatterns) {
this.setComponentNamePatterns(StringUtils.delimitedListToStringArray(componentNamePatterns, ",", " "));
setComponentNamePatterns(StringUtils.delimitedListToStringArray(componentNamePatterns, ",", " "));
}
@ManagedAttribute
@@ -160,6 +160,16 @@ public class MessageHistoryConfigurer implements SmartLifecycle, BeanFactoryAwar
}
}
@Override
public boolean requiresDestruction(Object bean) {
return bean instanceof TrackableComponent;
}
@Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
this.currentlyTrackedComponents.remove(bean);
}
/*
* SmartLifecycle implementation
*/