From b292f81d464b87c9b754bb72d359fdd0c05e5f88 Mon Sep 17 00:00:00 2001 From: Ilayaperumal Gopinathan Date: Tue, 4 Oct 2016 19:03:40 +0530 Subject: [PATCH] Set producer listener obtained from the context - When creating the KafkaMessageChannelBinder set the producer listener that is being autowired in the configuration - Add test This resolves #49 --- spring-cloud-stream-binder-kafka/pom.xml | 5 ++ .../config/KafkaBinderConfiguration.java | 2 +- .../binder/kafka/AbstractKafkaTestBinder.java | 15 ++++++ .../kafka/KafkaBinderConfigurationTest.java | 53 +++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/KafkaBinderConfigurationTest.java diff --git a/spring-cloud-stream-binder-kafka/pom.xml b/spring-cloud-stream-binder-kafka/pom.xml index 4d6f8ac10..118c36f0a 100644 --- a/spring-cloud-stream-binder-kafka/pom.xml +++ b/spring-cloud-stream-binder-kafka/pom.xml @@ -66,6 +66,11 @@ spring-integration-kafka ${spring-integration-kafka.version} + + org.springframework.boot + spring-boot-test + test + org.springframework.kafka spring-kafka-test diff --git a/spring-cloud-stream-binder-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/config/KafkaBinderConfiguration.java b/spring-cloud-stream-binder-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/config/KafkaBinderConfiguration.java index f80a46dde..438cbeddd 100644 --- a/spring-cloud-stream-binder-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/config/KafkaBinderConfiguration.java +++ b/spring-cloud-stream-binder-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/config/KafkaBinderConfiguration.java @@ -87,7 +87,7 @@ public class KafkaBinderConfiguration { KafkaMessageChannelBinder kafkaMessageChannelBinder = new KafkaMessageChannelBinder( this.configurationProperties); kafkaMessageChannelBinder.setCodec(this.codec); - //kafkaMessageChannelBinder.setProducerListener(producerListener); + kafkaMessageChannelBinder.setProducerListener(producerListener); kafkaMessageChannelBinder.setExtendedBindingProperties(this.kafkaExtendedBindingProperties); kafkaMessageChannelBinder.setAdminUtilsOperation(adminUtilsOperation); return kafkaMessageChannelBinder; diff --git a/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/AbstractKafkaTestBinder.java b/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/AbstractKafkaTestBinder.java index 483ab9775..e928575fd 100644 --- a/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/AbstractKafkaTestBinder.java +++ b/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/AbstractKafkaTestBinder.java @@ -1,3 +1,18 @@ +/* + * Copyright 2014-2016 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 + * + * http://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.binder.kafka; import java.util.List; diff --git a/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/KafkaBinderConfigurationTest.java b/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/KafkaBinderConfigurationTest.java new file mode 100644 index 000000000..2922ce6ef --- /dev/null +++ b/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/KafkaBinderConfigurationTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2016 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 + * + * http://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.binder.kafka; + +import static org.junit.Assert.assertNotNull; + +import java.lang.reflect.Field; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.stream.binder.kafka.config.KafkaBinderConfiguration; +import org.springframework.kafka.support.ProducerListener; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.util.ReflectionUtils; + +/** + * @author Ilayaperumal Gopinathan + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = KafkaBinderConfiguration.class) +public class KafkaBinderConfigurationTest { + + @Autowired + private KafkaMessageChannelBinder kafkaMessageChannelBinder; + + @Test + public void testKafkaBinderProducerListener() { + assertNotNull(this.kafkaMessageChannelBinder); + Field producerListenerField = ReflectionUtils.findField( + KafkaMessageChannelBinder.class, "producerListener", + ProducerListener.class); + ReflectionUtils.makeAccessible(producerListenerField); + ProducerListener producerListener = (ProducerListener) ReflectionUtils.getField( + producerListenerField, this.kafkaMessageChannelBinder); + assertNotNull(producerListener); + } +}