From 1fb479ff6db89a696c68692a90b4634b99cfa64c Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Mon, 30 Sep 2019 19:18:22 -0400 Subject: [PATCH] GH-1397 Ensure dynamic destinations properly configured Resolves #1397 --- .../binding/BinderAwareChannelResolver.java | 8 +- .../DynamicDestinationFunctionTests.java | 100 ++++++++++++++++++ 2 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 spring-cloud-stream/src/test/java/org/springframework/cloud/stream/function/DynamicDestinationFunctionTests.java 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 879863440..9fb85d0f3 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 @@ -102,16 +102,16 @@ public class BinderAwareChannelResolver } else { channel = this.bindingTargetFactory.createOutput(channelName); + ProducerProperties producerProperties = bindingServiceProperties + .getProducerProperties(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); } + bindingServiceProperties.updateProducerProperties(channelName, + producerProperties); this.beanFactory.registerSingleton(channelName, channel); channel = (MessageChannel) this.beanFactory.initializeBean(channel, channelName); diff --git a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/function/DynamicDestinationFunctionTests.java b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/function/DynamicDestinationFunctionTests.java new file mode 100644 index 000000000..f59715e50 --- /dev/null +++ b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/function/DynamicDestinationFunctionTests.java @@ -0,0 +1,100 @@ +/* + * Copyright 2019-2019 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.stream.function; + +import java.util.function.Consumer; + +import org.junit.After; +import org.junit.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy; +import org.springframework.cloud.stream.binder.test.InputDestination; +import org.springframework.cloud.stream.binder.test.OutputDestination; +import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration; +import org.springframework.cloud.stream.binding.BinderAwareChannelResolver; +import org.springframework.cloud.stream.config.BindingServiceProperties; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.messaging.Message; +import org.springframework.messaging.support.GenericMessage; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * + * @author Oleg Zhurakousky + * + */ +public class DynamicDestinationFunctionTests { + + @After + public void after() { + System.clearProperty("spring.cloud.stream.function.definition"); + System.clearProperty("spring.cloud.function.definition"); + } + + @Test + public void testEmptyConfiguration() { + + try (ConfigurableApplicationContext context = new SpringApplicationBuilder(TestChannelBinderConfiguration + .getCompleteConfiguration(SampleConfiguration.class)).web(WebApplicationType.NONE).run( + "--spring.jmx.enabled=false", + "--spring.cloud.stream.bindings.fooDestination.producer.partitionKeyExtractorName=keyExtractor")) { + InputDestination input = context.getBean(InputDestination.class); + input.send(new GenericMessage("fooDestination")); + + BindingServiceProperties serviceProperties = context.getBean(BindingServiceProperties.class); + assertThat("keyExtractor").isEqualTo( + serviceProperties.getProducerProperties("fooDestination").getPartitionKeyExtractorName()); + + OutputDestination output = context.getBean(OutputDestination.class); + assertThat(output.receive(1000).getPayload()).isEqualTo("fooDestination".getBytes()); + } + + } + + @EnableAutoConfiguration + public static class SampleConfiguration { + + @Autowired + private BinderAwareChannelResolver resolver; + + @Bean + public PartitionKeyExtractorStrategy keyExtractor() { + return new PartitionKeyExtractorStrategy() { + + @Override + public Object extractKey(Message message) { + return 0; + } + }; + } + + @Bean + public Consumer cons() { + return value -> { + System.out.println("Sending to " + value); + resolver.resolveDestination(value).send(new GenericMessage(value)); + }; + } + } + +}