Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
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;
Expand All @@ -50,7 +50,7 @@
*/
@ManagedResource
@IntegrationManagedResource
public class MessageHistoryConfigurer implements SmartLifecycle, BeanFactoryAware, BeanPostProcessor {
public class MessageHistoryConfigurer implements SmartLifecycle, BeanFactoryAware, DestructionAwareBeanPostProcessor {

private final Log logger = LogFactory.getLog(this.getClass());

Expand Down Expand Up @@ -160,6 +160,16 @@ private void trackComponentIfAny(TrackableComponent component) {
}
}

@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
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,7 +25,7 @@
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.integration.monitor.IntegrationMBeanExporter;
Expand All @@ -40,21 +40,22 @@
*
* @author Oleg Zhurakousky
* @author Artem Bilan
*
* @since 2.1
*
*/
class MBeanExporterHelper implements BeanPostProcessor, Ordered {
class MBeanExporterHelper implements DestructionAwareBeanPostProcessor, Ordered {

private final List<MBeanExporter> mBeanExportersForExcludes = new ArrayList<MBeanExporter>();
private final List<MBeanExporter> mBeanExportersForExcludes = new ArrayList<>();

private final Set<String> siBeanNames = new HashSet<String>();
private final Set<String> siBeanNames = new HashSet<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be made concurrent to handle dynamic add/remove. See MessageHistoryConfigurer.


@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if ("$autoCreateChannelCandidates".equals(beanName)) {
@SuppressWarnings("unchecked")
Collection<String> autoCreateChannelCandidatesNames = (Collection<String>) new DirectFieldAccessor(bean)
.getPropertyValue("channelNames");
.getPropertyValue("channelNames");
this.siBeanNames.addAll(autoCreateChannelCandidatesNames);
if (!this.mBeanExportersForExcludes.isEmpty()) {
for (String autoCreateChannelCandidatesName : autoCreateChannelCandidatesNames) {
Expand Down Expand Up @@ -91,6 +92,23 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
return bean;
}

@Override
public boolean requiresDestruction(Object bean) {
return (bean instanceof MBeanExporter && !(bean instanceof IntegrationMBeanExporter)) ||
AnnotatedElementUtils.isAnnotated(AopUtils.getTargetClass(bean),
IntegrationManagedResource.class.getName());
}

@Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
if (bean instanceof MBeanExporter) {
mBeanExportersForExcludes.remove(bean);
}
else {
this.siBeanNames.remove(beanName);
}
}

@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
Expand Down