Skip to content

Commit a3ee610

Browse files
garyrussellartembilan
authored andcommitted
GH-2000: Doc RetryTopic Publish with Deser. Errors
Resolves #2000 Backport `DelegatingByTypeSerializer`. This is for 2.7.x; cherry pick forward to main. * Doc polishing. # Conflicts: # spring-kafka-docs/src/main/asciidoc/kafka.adoc # spring-kafka/src/main/java/org/springframework/kafka/support/serializer/DelegatingByTypeSerializer.java
1 parent 505bd84 commit a3ee610

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

spring-kafka-docs/src/main/asciidoc/kafka.adoc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4452,6 +4452,26 @@ consumerProps.put(ErrorHandlingDeserializer.VALUE_FUNCTION, FailedFooProvider.cl
44524452
----
44534453
====
44544454

4455+
IMPORTANT: If the consumer is configured with an `ErrorHandlingDeserializer` it is important to configure the `KafkaTemplate` and its producer with a serializer that can handle normal objects as well as raw `byte[]` values, which result from deserialization exceptions.
4456+
The generic value type of the template should be `Object`.
4457+
One technique is to use the `DelegatingByTypeSerializer`; an example follows:
4458+
4459+
====
4460+
[source, java]
4461+
----
4462+
@Bean
4463+
public ProducerFactory<String, Object> producerFactory() {
4464+
return new DefaultKafkaProducerFactory<>(producerConfiguration(), new StringSerializer(),
4465+
new DelegatingByTypeSerializer(Map.of(byte[].class, new ByteArraySerializer(),
4466+
MyNormalObject.class, new JsonSerializer<Object>())));
4467+
}
4468+
4469+
@Bean
4470+
public KafkaTemplate<String, Object> kafkaTemplate() {
4471+
return new KafkaTemplate<>(producerFactory());
4472+
}
4473+
----
4474+
====
44554475
When using an `ErrorHandlingDeserializer` with a batch listener, you must check for the deserialization exceptions in message headers.
44564476
When used with a `RecoveringBatchErrorHandler`, you can use that header to determine which record the exception failed on and communicate to the error handler via a `BatchListenerFailedException`.
44574477

spring-kafka-docs/src/main/asciidoc/retrytopic.adoc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,27 @@ public RetryTopicConfiguration myOtherRetryTopic(KafkaTemplate<String, MyOtherPo
127127

128128
NOTE: The retry topics' and dlt's consumers will be assigned to a consumer group with a group id that is the combination of the one with you provide in the `groupId` parameter of the `@KafkaListener` annotation with the topic's suffix. If you don't provide any they'll all belong to the same group, and rebalance on a retry topic will cause an unnecessary rebalance on the main topic.
129129

130+
IMPORTANT: If the consumer is configured with an <<error-handling-deserializer,`ErrorHandlingDeserializer`>>, to handle deserilialization exceptions, it is important to configure the `KafkaTemplate` and its producer with a serializer that can handle normal objects as well as raw `byte[]` values, which result from deserialization exceptions.
131+
The generic value type of the template should be `Object`.
132+
One technique is to use the `DelegatingByTypeSerializer`; an example follows:
133+
134+
====
135+
[source, java]
136+
----
137+
@Bean
138+
public ProducerFactory<String, Object> producerFactory() {
139+
return new DefaultKafkaProducerFactory<>(producerConfiguration(), new StringSerializer(),
140+
new DelegatingByTypeSerializer(Map.of(byte[].class, new ByteArraySerializer(),
141+
MyNormalObject.class, new JsonSerializer<Object>())));
142+
}
143+
144+
@Bean
145+
public KafkaTemplate<String, Object> kafkaTemplate() {
146+
return new KafkaTemplate<>(producerFactory());
147+
}
148+
----
149+
====
150+
130151
==== Features
131152

132153
Most of the features are available both for the `@RetryableTopic` annotation and the `RetryTopicConfiguration` beans.

spring-kafka/src/main/java/org/springframework/kafka/support/serializer/DelegatingByTypeSerializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* Delegates to a serializer based on type.
3131
*
3232
* @author Gary Russell
33-
* @since 2.8
33+
* @since 2.7.9
3434
*
3535
*/
3636
public class DelegatingByTypeSerializer implements Serializer<Object> {

0 commit comments

Comments
 (0)