Clean Up Auto-Declare Context on Unbind
This commit is contained in:
committed by
Marius Bogoevici
parent
192aa79baa
commit
e2485e3290
@@ -26,9 +26,6 @@ import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import com.rabbitmq.client.AMQP;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import com.rabbitmq.client.Envelope;
|
||||
import org.aopalliance.aop.Advice;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -96,6 +93,10 @@ import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.rabbitmq.client.AMQP;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import com.rabbitmq.client.Envelope;
|
||||
|
||||
/**
|
||||
* A {@link org.springframework.cloud.stream.binder.Binder} implementation backed by RabbitMQ.
|
||||
*
|
||||
@@ -716,7 +717,7 @@ public class RabbitMessageChannelBinder extends MessageChannelBinderSupport impl
|
||||
@Override
|
||||
protected void afterUnbind(Binding<MessageChannel> binding) {
|
||||
if (Binding.Type.consumer.equals(binding.getType())) {
|
||||
cleanAutoDeclareContext(binding.getName());
|
||||
cleanAutoDeclareContext(binding);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -728,13 +729,25 @@ public class RabbitMessageChannelBinder extends MessageChannelBinderSupport impl
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanAutoDeclareContext(String name) {
|
||||
private void cleanAutoDeclareContext(Binding<MessageChannel> binding) {
|
||||
Assert.isTrue(binding.getPropertiesAccessor() instanceof RabbitPropertiesAccessor,
|
||||
"Binding was not created by this binder");
|
||||
String prefix = ((RabbitPropertiesAccessor) binding.getPropertiesAccessor()).getPrefix(this.defaultPrefix);
|
||||
String name = binding.getName();
|
||||
synchronized(this.autoDeclareContext) {
|
||||
if (this.autoDeclareContext.containsBean(name)) {
|
||||
ConfigurableListableBeanFactory beanFactory = this.autoDeclareContext.getBeanFactory();
|
||||
if (beanFactory instanceof DefaultListableBeanFactory) {
|
||||
((DefaultListableBeanFactory) beanFactory).destroySingleton(name);
|
||||
}
|
||||
removeSingleton(applyPrefix(prefix,name) + ".binding");
|
||||
removeSingleton(applyPrefix(prefix,name));
|
||||
String dlq = applyPrefix(prefix,name) + ".dlq";
|
||||
removeSingleton(dlq + ".binding");
|
||||
removeSingleton(dlq);
|
||||
}
|
||||
}
|
||||
|
||||
private void removeSingleton(String name) {
|
||||
if (this.autoDeclareContext.containsBean(name)) {
|
||||
ConfigurableListableBeanFactory beanFactory = this.autoDeclareContext.getBeanFactory();
|
||||
if (beanFactory instanceof DefaultListableBeanFactory) {
|
||||
((DefaultListableBeanFactory) beanFactory).destroySingleton(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +62,7 @@ import org.springframework.cloud.stream.binder.Binding;
|
||||
import org.springframework.cloud.stream.binder.PartitionCapableBinderTests;
|
||||
import org.springframework.cloud.stream.binder.Spy;
|
||||
import org.springframework.cloud.stream.test.junit.rabbit.RabbitTestSupport;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.expression.spel.standard.SpelExpression;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
@@ -330,6 +331,13 @@ public class RabbitBinderTests extends PartitionCapableBinderTests {
|
||||
assertTrue(n < 100);
|
||||
|
||||
binder.unbind(consumerBinding);
|
||||
|
||||
ApplicationContext context = TestUtils.getPropertyValue(binder, "binder.autoDeclareContext",
|
||||
ApplicationContext.class);
|
||||
assertFalse(context.containsBean(TEST_PREFIX + "dlqtest.default.binding"));
|
||||
assertFalse(context.containsBean(TEST_PREFIX + "dlqtest.default"));
|
||||
assertFalse(context.containsBean(TEST_PREFIX + "dlqtest.default.dlq.binding"));
|
||||
assertFalse(context.containsBean(TEST_PREFIX + "dlqtest.default.dlq"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -702,7 +710,7 @@ public class RabbitBinderTests extends PartitionCapableBinderTests {
|
||||
binder.unbind(pubSubProducerBinding);
|
||||
binder.unbind(nonDurableConsumerBinding);
|
||||
binder.unbind(durableConsumerBinding);
|
||||
|
||||
|
||||
binder.cleanup();
|
||||
|
||||
proxy.stop();
|
||||
|
||||
Reference in New Issue
Block a user