-
Notifications
You must be signed in to change notification settings - Fork 1.1k
GH-3696: DeserializationEx support for KafkaMS #8689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,8 @@ | |
| import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
| import org.apache.kafka.clients.consumer.ConsumerRebalanceListener; | ||
| import org.apache.kafka.common.TopicPartition; | ||
| import org.apache.kafka.common.serialization.Deserializer; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import org.springframework.integration.channel.NullChannel; | ||
|
|
@@ -32,11 +34,17 @@ | |
| import org.springframework.kafka.core.DefaultKafkaProducerFactory; | ||
| import org.springframework.kafka.core.KafkaTemplate; | ||
| import org.springframework.kafka.listener.ConsumerProperties; | ||
| import org.springframework.kafka.support.serializer.DeserializationException; | ||
| import org.springframework.kafka.support.serializer.ErrorHandlingDeserializer; | ||
| import org.springframework.kafka.test.utils.KafkaTestUtils; | ||
| import org.springframework.messaging.Message; | ||
| import org.springframework.messaging.support.GenericMessage; | ||
| import org.springframework.util.ClassUtils; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
| import static org.awaitility.Awaitility.await; | ||
| import static org.mockito.Mockito.mock; | ||
|
|
||
| /** | ||
| * @author Gary Russell | ||
|
|
@@ -50,9 +58,17 @@ class MessageSourceIntegrationTests { | |
|
|
||
| static final String TOPIC1 = "MessageSourceIntegrationTests1"; | ||
|
|
||
| static final String TOPIC2 = "MessageSourceIntegrationTests2"; | ||
|
|
||
| static String brokers; | ||
|
|
||
| @BeforeAll | ||
| static void setup() { | ||
| brokers = System.getProperty("spring.global.embedded.kafka.brokers"); | ||
| } | ||
|
|
||
| @Test | ||
| void testSource() throws Exception { | ||
| String brokers = System.getProperty("spring.global.embedded.kafka.brokers"); | ||
| Map<String, Object> consumerProps = KafkaTestUtils.consumerProps(brokers, "testSource", "false"); | ||
| consumerProps.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 2); | ||
| consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); | ||
|
|
@@ -122,4 +138,66 @@ public void onPartitionsAssigned(Collection<TopicPartition> partitions) { | |
| template.destroy(); | ||
| } | ||
|
|
||
| @Test | ||
| void deserializationErrorIsThrownFromSource() throws Exception { | ||
| Map<String, Object> consumerProps = KafkaTestUtils.consumerProps(brokers, "testErrorChannelSource", "false"); | ||
| consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); | ||
|
||
| consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ErrorHandlingDeserializer.class); | ||
| consumerProps.put(ErrorHandlingDeserializer.VALUE_DESERIALIZER_CLASS, FailingDeserializer.class); | ||
|
|
||
| DefaultKafkaConsumerFactory<Integer, String> consumerFactory = new DefaultKafkaConsumerFactory<>(consumerProps); | ||
| ConsumerProperties consumerProperties = new ConsumerProperties(TOPIC2); | ||
| CountDownLatch assigned = new CountDownLatch(1); | ||
| consumerProperties.setConsumerRebalanceListener( | ||
| new ConsumerRebalanceListener() { | ||
|
|
||
| @Override | ||
| public void onPartitionsRevoked(Collection<TopicPartition> partitions) { | ||
| } | ||
|
|
||
| @Override | ||
| public void onPartitionsAssigned(Collection<TopicPartition> partitions) { | ||
| assigned.countDown(); | ||
| } | ||
|
|
||
| }); | ||
|
|
||
| consumerProperties.setPollTimeout(10); | ||
|
|
||
| KafkaMessageSource<Integer, String> source = new KafkaMessageSource<>(consumerFactory, consumerProperties); | ||
| source.setBeanClassLoader(ClassUtils.getDefaultClassLoader()); | ||
| source.setBeanFactory(mock()); | ||
| source.afterPropertiesSet(); | ||
| source.start(); | ||
|
|
||
| Map<String, Object> producerProps = KafkaTestUtils.producerProps(brokers); | ||
| DefaultKafkaProducerFactory<Object, Object> producerFactory = new DefaultKafkaProducerFactory<>(producerProps); | ||
| KafkaTemplate<Object, Object> template = new KafkaTemplate<>(producerFactory); | ||
|
|
||
| String testData = "test data"; | ||
| template.send(TOPIC2, testData); | ||
|
|
||
| source.receive(); // Trigger Kafka Consumer creation and poll() | ||
|
||
| assertThat(assigned.await(10, TimeUnit.SECONDS)).isTrue(); | ||
|
|
||
| await().untilAsserted(() -> | ||
| assertThatExceptionOfType(DeserializationException.class) | ||
| .isThrownBy(source::receive) | ||
| .hasFieldOrPropertyWithValue("data", testData.getBytes()) | ||
| .withMessage("failed to deserialize") | ||
| .withStackTraceContaining("failed deserialization")); | ||
|
|
||
| source.destroy(); | ||
| template.destroy(); | ||
| } | ||
|
|
||
| public static class FailingDeserializer implements Deserializer<String> { | ||
|
|
||
| @Override | ||
| public String deserialize(String topic, byte[] data) { | ||
| throw new RuntimeException("failed deserialization"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will move most of the duplicated code to
SK.ErrorHandlingUtils; then you can use it from there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spring-projects/spring-kafka#2756