|
| 1 | +/* |
| 2 | + * Copyright 2016-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.kafka.listener; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.concurrent.CountDownLatch; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | + |
| 25 | +import org.apache.kafka.clients.consumer.ConsumerConfig; |
| 26 | +import org.apache.kafka.clients.producer.ProducerConfig; |
| 27 | +import org.apache.kafka.common.serialization.ByteArrayDeserializer; |
| 28 | +import org.apache.kafka.common.serialization.ByteArraySerializer; |
| 29 | +import org.apache.kafka.common.serialization.IntegerDeserializer; |
| 30 | +import org.apache.kafka.common.serialization.IntegerSerializer; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | + |
| 33 | +import org.springframework.beans.factory.annotation.Autowired; |
| 34 | +import org.springframework.context.annotation.Bean; |
| 35 | +import org.springframework.context.annotation.Configuration; |
| 36 | +import org.springframework.kafka.annotation.EnableKafka; |
| 37 | +import org.springframework.kafka.annotation.KafkaListener; |
| 38 | +import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; |
| 39 | +import org.springframework.kafka.config.KafkaListenerContainerFactory; |
| 40 | +import org.springframework.kafka.core.DefaultKafkaConsumerFactory; |
| 41 | +import org.springframework.kafka.core.DefaultKafkaProducerFactory; |
| 42 | +import org.springframework.kafka.core.KafkaTemplate; |
| 43 | +import org.springframework.kafka.core.ProducerFactory; |
| 44 | +import org.springframework.kafka.test.EmbeddedKafkaBroker; |
| 45 | +import org.springframework.kafka.test.context.EmbeddedKafka; |
| 46 | +import org.springframework.kafka.test.utils.KafkaTestUtils; |
| 47 | +import org.springframework.messaging.Message; |
| 48 | +import org.springframework.messaging.MessageHeaders; |
| 49 | +import org.springframework.messaging.converter.SmartMessageConverter; |
| 50 | +import org.springframework.messaging.support.MessageBuilder; |
| 51 | +import org.springframework.test.annotation.DirtiesContext; |
| 52 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 53 | + |
| 54 | +import static org.assertj.core.api.Assertions.assertThat; |
| 55 | + |
| 56 | +/** |
| 57 | + * Integration tests for SmartMessageConverter support in batch listeners. |
| 58 | + * |
| 59 | + * @author George Mahfoud |
| 60 | + * @since 3.3.11 |
| 61 | + */ |
| 62 | +@SpringJUnitConfig |
| 63 | +@DirtiesContext |
| 64 | +@EmbeddedKafka(partitions = 1, topics = { "smartBatchTopic", "smartBatchTopic2" }) |
| 65 | +class BatchSmartMessageConverterTests { |
| 66 | + |
| 67 | + @Autowired |
| 68 | + private KafkaTemplate<Integer, byte[]> template; |
| 69 | + |
| 70 | + @Autowired |
| 71 | + private Config config; |
| 72 | + |
| 73 | + @Test |
| 74 | + void testContentTypeConverterWithBatchListener() throws Exception { |
| 75 | + BatchListener listener = this.config.batchListener(); |
| 76 | + listener.reset(2); |
| 77 | + |
| 78 | + this.template.send("smartBatchTopic", "hello".getBytes()); |
| 79 | + this.template.send("smartBatchTopic", "world".getBytes()); |
| 80 | + |
| 81 | + assertThat(listener.latch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 82 | + assertThat(listener.received).containsExactly("hello", "world"); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void testMultipleListenersWithDifferentConverters() throws Exception { |
| 87 | + BatchListener listener1 = this.config.batchListener(); |
| 88 | + BatchListener2 listener2 = this.config.batchListener2(); |
| 89 | + listener1.reset(1); |
| 90 | + listener2.reset(1); |
| 91 | + |
| 92 | + String listener1Data = "listener1Data"; |
| 93 | + String listener2Data = "listener2Data"; |
| 94 | + this.template.send("smartBatchTopic", listener1Data.getBytes()); |
| 95 | + this.template.send("smartBatchTopic2", listener2Data.getBytes()); |
| 96 | + |
| 97 | + assertThat(listener1.latch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 98 | + assertThat(listener2.latch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 99 | + assertThat(listener1.received).containsExactly(listener1Data); |
| 100 | + assertThat(listener2.received).containsExactly(listener2Data.toUpperCase()); |
| 101 | + } |
| 102 | + |
| 103 | + @Configuration |
| 104 | + @EnableKafka |
| 105 | + public static class Config { |
| 106 | + |
| 107 | + @Bean |
| 108 | + public KafkaListenerContainerFactory<?> kafkaListenerContainerFactory(EmbeddedKafkaBroker embeddedKafka) { |
| 109 | + ConcurrentKafkaListenerContainerFactory<Integer, byte[]> factory = |
| 110 | + new ConcurrentKafkaListenerContainerFactory<>(); |
| 111 | + factory.setConsumerFactory(consumerFactory(embeddedKafka)); |
| 112 | + factory.setBatchListener(true); |
| 113 | + return factory; |
| 114 | + } |
| 115 | + |
| 116 | + @Bean |
| 117 | + public DefaultKafkaConsumerFactory<Integer, byte[]> consumerFactory(EmbeddedKafkaBroker embeddedKafka) { |
| 118 | + return new DefaultKafkaConsumerFactory<>(consumerConfigs(embeddedKafka)); |
| 119 | + } |
| 120 | + |
| 121 | + @Bean |
| 122 | + public Map<String, Object> consumerConfigs(EmbeddedKafkaBroker embeddedKafka) { |
| 123 | + Map<String, Object> consumerProps = |
| 124 | + KafkaTestUtils.consumerProps("smartBatchGroup", "false", embeddedKafka); |
| 125 | + consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class); |
| 126 | + consumerProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, IntegerDeserializer.class); |
| 127 | + return consumerProps; |
| 128 | + } |
| 129 | + |
| 130 | + @Bean |
| 131 | + public KafkaTemplate<Integer, byte[]> template(EmbeddedKafkaBroker embeddedKafka) { |
| 132 | + return new KafkaTemplate<>(producerFactory(embeddedKafka)); |
| 133 | + } |
| 134 | + |
| 135 | + @Bean |
| 136 | + public ProducerFactory<Integer, byte[]> producerFactory(EmbeddedKafkaBroker embeddedKafka) { |
| 137 | + return new DefaultKafkaProducerFactory<>(producerConfigs(embeddedKafka)); |
| 138 | + } |
| 139 | + |
| 140 | + @Bean |
| 141 | + public Map<String, Object> producerConfigs(EmbeddedKafkaBroker embeddedKafka) { |
| 142 | + Map<String, Object> props = KafkaTestUtils.producerProps(embeddedKafka); |
| 143 | + props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class); |
| 144 | + props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class); |
| 145 | + return props; |
| 146 | + } |
| 147 | + |
| 148 | + @Bean |
| 149 | + public SmartMessageConverter byteArrayToStringConverter() { |
| 150 | + return new ByteArrayConverter(bytes -> new String(bytes)); |
| 151 | + } |
| 152 | + |
| 153 | + @Bean |
| 154 | + public SmartMessageConverter byteArrayToUpperCaseConverter() { |
| 155 | + return new ByteArrayConverter(bytes -> new String(bytes).toUpperCase()); |
| 156 | + } |
| 157 | + |
| 158 | + @Bean |
| 159 | + public BatchListener batchListener() { |
| 160 | + return new BatchListener(); |
| 161 | + } |
| 162 | + |
| 163 | + @Bean |
| 164 | + public BatchListener2 batchListener2() { |
| 165 | + return new BatchListener2(); |
| 166 | + } |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | + public static class BatchListener { |
| 171 | + |
| 172 | + private CountDownLatch latch = new CountDownLatch(2); |
| 173 | + |
| 174 | + private final List<String> received = new ArrayList<>(); |
| 175 | + |
| 176 | + @KafkaListener( |
| 177 | + id = "batchSmartListener", |
| 178 | + topics = "smartBatchTopic", |
| 179 | + groupId = "smartBatchGroup", |
| 180 | + contentTypeConverter = "byteArrayToStringConverter", |
| 181 | + batch = "true" |
| 182 | + ) |
| 183 | + public void listen(List<String> messages) { |
| 184 | + messages.forEach(message -> { |
| 185 | + this.received.add(message); |
| 186 | + this.latch.countDown(); |
| 187 | + }); |
| 188 | + } |
| 189 | + |
| 190 | + void reset(int expectedCount) { |
| 191 | + this.received.clear(); |
| 192 | + this.latch = new CountDownLatch(expectedCount); |
| 193 | + } |
| 194 | + |
| 195 | + } |
| 196 | + |
| 197 | + public static class BatchListener2 { |
| 198 | + |
| 199 | + private CountDownLatch latch = new CountDownLatch(1); |
| 200 | + |
| 201 | + private final List<String> received = new ArrayList<>(); |
| 202 | + |
| 203 | + @KafkaListener( |
| 204 | + id = "batchSmartListener2", |
| 205 | + topics = "smartBatchTopic2", |
| 206 | + groupId = "smartBatchGroup2", |
| 207 | + contentTypeConverter = "byteArrayToUpperCaseConverter", |
| 208 | + batch = "true" |
| 209 | + ) |
| 210 | + public void listen(List<String> messages) { |
| 211 | + messages.forEach(message -> { |
| 212 | + this.received.add(message); |
| 213 | + this.latch.countDown(); |
| 214 | + }); |
| 215 | + } |
| 216 | + |
| 217 | + void reset(int expectedCount) { |
| 218 | + this.received.clear(); |
| 219 | + this.latch = new CountDownLatch(expectedCount); |
| 220 | + } |
| 221 | + |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Simple SmartMessageConverter for testing that converts byte[] to String using a function. |
| 226 | + */ |
| 227 | + static class ByteArrayConverter implements SmartMessageConverter { |
| 228 | + |
| 229 | + private final java.util.function.Function<byte[], String> converter; |
| 230 | + |
| 231 | + ByteArrayConverter(java.util.function.Function<byte[], String> converter) { |
| 232 | + this.converter = converter; |
| 233 | + } |
| 234 | + |
| 235 | + @Override |
| 236 | + public Object fromMessage(Message<?> message, Class<?> targetClass) { |
| 237 | + Object payload = message.getPayload(); |
| 238 | + return (payload instanceof byte[] bytes) ? this.converter.apply(bytes) : payload; |
| 239 | + } |
| 240 | + |
| 241 | + @Override |
| 242 | + public Object fromMessage(Message<?> message, Class<?> targetClass, Object conversionHint) { |
| 243 | + return fromMessage(message, targetClass); |
| 244 | + } |
| 245 | + |
| 246 | + @Override |
| 247 | + public Message<?> toMessage(Object payload, MessageHeaders headers) { |
| 248 | + return MessageBuilder.withPayload(payload).copyHeaders(headers).build(); |
| 249 | + } |
| 250 | + |
| 251 | + @Override |
| 252 | + public Message<?> toMessage(Object payload, MessageHeaders headers, Object conversionHint) { |
| 253 | + return toMessage(payload, headers); |
| 254 | + } |
| 255 | + |
| 256 | + } |
| 257 | +} |
0 commit comments