From 9dde0602e6706d939f4d7c9a85114eafa20d5a32 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Sat, 17 Feb 2018 16:48:34 -0500 Subject: [PATCH] GH-1229 general cleanup in BinderAwareChannelResolver Resolves #1229 --- .../binding/BinderAwareChannelResolver.java | 58 +++++++------------ 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/BinderAwareChannelResolver.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/BinderAwareChannelResolver.java index a2a249d87..60117e12c 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/BinderAwareChannelResolver.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/BinderAwareChannelResolver.java @@ -89,9 +89,8 @@ public class BinderAwareChannelResolver extends BeanFactoryMessageChannelDestina @Override public void setBeanFactory(BeanFactory beanFactory) { super.setBeanFactory(beanFactory); - if (beanFactory instanceof ConfigurableListableBeanFactory) { - this.beanFactory = (ConfigurableListableBeanFactory) beanFactory; - } + Assert.isTrue(beanFactory instanceof ConfigurableListableBeanFactory, "'beanFactory' must be an instance of ConfigurableListableBeanFactory"); + this.beanFactory = (ConfigurableListableBeanFactory) beanFactory; } @SuppressWarnings("unchecked") @@ -104,46 +103,33 @@ public class BinderAwareChannelResolver extends BeanFactoryMessageChannelDestina // intentionally empty; will check again while holding the monitor } synchronized (this) { - DestinationResolutionException destinationResolutionException; + BindingServiceProperties bindingServiceProperties = this.bindingService.getBindingServiceProperties(); + String[] dynamicDestinations = bindingServiceProperties.getDynamicDestinations(); + boolean dynamicAllowed = ObjectUtils.isEmpty(dynamicDestinations) || ObjectUtils.containsElement(dynamicDestinations, channelName); try { return super.resolveDestination(channelName); } catch (DestinationResolutionException e) { - destinationResolutionException = e; - } - MessageChannel channel = null; - if (this.beanFactory != null) { - String[] dynamicDestinations = null; - BindingServiceProperties bindingServiceProperties = this.bindingService.getBindingServiceProperties(); - if (bindingServiceProperties != null) { - dynamicDestinations = bindingServiceProperties.getDynamicDestinations(); + if (!dynamicAllowed) { + throw e; } - boolean dynamicAllowed = ObjectUtils.isEmpty(dynamicDestinations) - || ObjectUtils.containsElement(dynamicDestinations, channelName); - if (dynamicAllowed) { - channel = this.bindingTargetFactory.createOutput(channelName); - this.beanFactory.registerSingleton(channelName, channel); + } + + MessageChannel channel = this.bindingTargetFactory.createOutput(channelName); + this.beanFactory.registerSingleton(channelName, channel); + + this.instrumentChannelWithGlobalInterceptors(channel, channelName); - this.instrumentChannelWithGlobalInterceptors(channel, channelName); - - channel = (MessageChannel) this.beanFactory.initializeBean(channel, channelName); - if (this.newBindingCallback != null) { - ProducerProperties producerProperties = this.bindingService.getBindingServiceProperties() - .getProducerProperties(channelName); - Object extendedProducerProperties = - this.bindingService.getExtendedProducerProperties(channel, channelName); - this.newBindingCallback.configure(channelName, channel, producerProperties, - extendedProducerProperties); - this.bindingService.getBindingServiceProperties().updateProducerProperties(channelName, - producerProperties); - } - Binding binding = this.bindingService.bindProducer(channel, channelName); - this.dynamicDestinationsBindable.addOutputBinding(channelName, binding); - } - else { - throw destinationResolutionException; - } + channel = (MessageChannel) this.beanFactory.initializeBean(channel, channelName); + if (this.newBindingCallback != null) { + ProducerProperties producerProperties = bindingServiceProperties.getProducerProperties(channelName); + Object extendedProducerProperties = this.bindingService.getExtendedProducerProperties(channel, channelName); + this.newBindingCallback.configure(channelName, channel, producerProperties, extendedProducerProperties); + bindingServiceProperties.updateProducerProperties(channelName, producerProperties); } + Binding binding = this.bindingService.bindProducer(channel, channelName); + this.dynamicDestinationsBindable.addOutputBinding(channelName, binding); + return channel; } }