You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Resolves#1964
Select the `Serializer`/`Deserializer` based on the topic name.
* Extract common code; fix removeDelegate; add default; CASE_SENSITIVE can be Boolean.
Copy file name to clipboardExpand all lines: spring-kafka-docs/src/main/asciidoc/kafka.adoc
+58-16
Original file line number
Diff line number
Diff line change
@@ -4136,6 +4136,8 @@ Alternatively, as long as you don't use the fluent API to configure properties,
4136
4136
[[delegating-serialization]]
4137
4137
===== Delegating Serializer and Deserializer
4138
4138
4139
+
===== Using Headers
4140
+
4139
4141
Version 2.3 introduced the `DelegatingSerializer` and `DelegatingDeserializer`, which allow producing and consuming records with different key and/or value types.
4140
4142
Producers must set a header `DelegatingSerializer.VALUE_SERIALIZATION_SELECTOR` to a selector value that is used to select which serializer to use for the value and `DelegatingSerializer.KEY_SERIALIZATION_SELECTOR` for the key; if a match is not found, an `IllegalStateException` is thrown.
4141
4143
@@ -4178,15 +4180,55 @@ For another technique to send different types to different topics, see <<routing
4178
4180
----
4179
4181
@Bean
4180
4182
public ProducerFactory<Integer, Object> producerFactory(Map<String, Object> config) {
4181
-
return new DefaultKafkaProducerFactory<>(config,
4182
-
null, new DelegatingByTypeSerializer(Map.of(
4183
-
byte[].class, new ByteArraySerializer(),
4184
-
Bytes.class, new BytesSerializer(),
4185
-
String.class, new StringSerializer())));
4183
+
return new DefaultKafkaProducerFactory<>(config,
4184
+
null, new DelegatingByTypeSerializer(Map.of(
4185
+
byte[].class, new ByteArraySerializer(),
4186
+
Bytes.class, new BytesSerializer(),
4187
+
String.class, new StringSerializer())));
4188
+
}
4189
+
----
4190
+
====
4191
+
4192
+
===== By Topic
4193
+
4194
+
Starting with version 2.8, the `DelegatingByTopicSerializer` and `DelegatingByTopicDeserializer` allow selection of a serializer/deserializer based on the topic name.
4195
+
Regex `Pattern` s are used to lookup the instance to use.
4196
+
The map can be configured using a constructor, or via properties (a comma delimited list of `pattern:serializer`).
Use `KEY_SERIALIZATION_TOPIC_CONFIG` when using this for keys.
4212
+
4213
+
====
4214
+
[source, java]
4215
+
----
4216
+
@Bean
4217
+
public ProducerFactory<Integer, Object> producerFactory(Map<String, Object> config) {
4218
+
return new DefaultKafkaProducerFactory<>(config,
4219
+
null,
4220
+
new DelegatingByTopicSerializer(Map.of(
4221
+
Pattern.compile("topic[0-4]"), new ByteArraySerializer(),
4222
+
Pattern.compile("topic[5-9]"), new StringSerializer())),
4223
+
new JsonSerializer<Object>()); // default
4186
4224
}
4187
4225
----
4188
4226
====
4189
4227
4228
+
You can specify a default serializer/deserializer to use when there is no pattern match using `DelegatingByTopicSerialization.KEY_SERIALIZATION_TOPIC_DEFAULT` and `DelegatingByTopicSerialization.VALUE_SERIALIZATION_TOPIC_DEFAULT`.
4229
+
4230
+
An additional property `DelegatingByTopicSerialization.CASE_SENSITIVE` (default `true`), when set to `false` makes the topic lookup case insensitive.
4231
+
4190
4232
[[retrying-deserialization]]
4191
4233
===== Retrying Deserializer
4192
4234
@@ -4261,7 +4303,7 @@ For convenience, starting with version 2.3, the framework also provides a `Strin
4261
4303
4262
4304
Starting with version 2.7.1, message payload conversion can be delegated to a `spring-messaging` `SmartMessageConverter`; this enables conversion, for example, to be based on the `MessageHeaders.CONTENT_TYPE` header.
4263
4305
4264
-
IMPORTANT:The `KafkaMessageConverter.fromMessage()` method is called for outbound conversion to a `ProducerRecord` with the message payload in the `ProducerRecord.value()` property.
4306
+
IMPORTANT:The `KafkaMessageConverter.fromMessage()` method is called for outbound conversion to a `ProducerRecord` with the message payload in the `ProducerRecord.value()` property.
4265
4307
The `KafkaMessageConverter.toMessage()` method is called for inbound conversion from `ConsumerRecord` with the payload being the `ConsumerRecord.value()` property.
4266
4308
The `SmartMessageConverter.toMessage()` method is called to create a new outbound `Message<?>` from the `Message` passed to`fromMessage()` (usually by `KafkaTemplate.send(Message<?> msg)`).
4267
4309
Similarly, in the `KafkaMessageConverter.toMessage()` method, after the converter has created a new `Message<?>` from the `ConsumerRecord`, the `SmartMessageConverter.fromMessage()` method is called and then the final inbound message is created with the newly converted payload.
@@ -5065,13 +5107,13 @@ The error handler can be configured with one or more `RetryListener` s, receivin
5065
5107
@FunctionalInterface
5066
5108
public interface RetryListener {
5067
5109
5068
-
void failedDelivery(ConsumerRecord<?, ?> record, Exception ex, int deliveryAttempt);
5110
+
void failedDelivery(ConsumerRecord<?, ?> record, Exception ex, int deliveryAttempt);
0 commit comments