Class DelegatingSerializer
- java.lang.Object
-
- org.springframework.kafka.support.serializer.DelegatingSerializer
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,org.apache.kafka.common.serialization.Serializer<java.lang.Object>
public class DelegatingSerializer extends java.lang.Object implements org.apache.kafka.common.serialization.Serializer<java.lang.Object>
ASerializer
that delegates to other serializers based on a serialization selector header. If the header is missing, and the type is supported bySerdes
we will delegate to that serializer type.- Since:
- 2.3
- Author:
- Gary Russell
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
KEY_SERIALIZATION_SELECTOR
Name of the header containing the serialization selector for keys.static java.lang.String
KEY_SERIALIZATION_SELECTOR_CONFIG
Name of the configuration property containing the serialization selector map for keys with formatselector:class,...
.static java.lang.String
VALUE_SERIALIZATION_SELECTOR
Name of the header containing the serialization selector for values.static java.lang.String
VALUE_SERIALIZATION_SELECTOR_CONFIG
Name of the configuration property containing the serialization selector map for values with formatselector:class,...
.
-
Constructor Summary
Constructors Constructor Description DelegatingSerializer()
Construct an instance that will be configured inconfigure(Map, boolean)
with producer propertiesVALUE_SERIALIZATION_SELECTOR_CONFIG
andKEY_SERIALIZATION_SELECTOR_CONFIG
.DelegatingSerializer(java.util.Map<java.lang.String,org.apache.kafka.common.serialization.Serializer<?>> delegates)
Construct an instance with the supplied mapping of selectors to delegate serializers.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addDelegate(java.lang.String selector, org.apache.kafka.common.serialization.Serializer<?> serializer)
void
close()
void
configure(java.util.Map<java.lang.String,?> configs, boolean isKey)
protected static java.util.Map<java.lang.String,org.apache.kafka.common.serialization.Serializer<?>>
createDelegates(java.lang.String mappings, java.util.Map<java.lang.String,?> configs, boolean isKey)
protected static void
createInstanceAndConfigure(java.util.Map<java.lang.String,?> configs, boolean isKey, java.util.Map<java.lang.String,org.apache.kafka.common.serialization.Serializer<?>> delegateMap, java.lang.String selector, java.lang.String className)
protected static void
instantiateAndConfigure(java.util.Map<java.lang.String,?> configs, boolean isKey, java.util.Map<java.lang.String,org.apache.kafka.common.serialization.Serializer<?>> delegateMap, java.lang.String selector, java.lang.Class<?> clazz)
org.apache.kafka.common.serialization.Serializer<?>
removeDelegate(java.lang.String selector)
byte[]
serialize(java.lang.String topic, java.lang.Object data)
byte[]
serialize(java.lang.String topic, org.apache.kafka.common.header.Headers headers, java.lang.Object data)
-
-
-
Field Detail
-
VALUE_SERIALIZATION_SELECTOR
public static final java.lang.String VALUE_SERIALIZATION_SELECTOR
Name of the header containing the serialization selector for values.- See Also:
- Constant Field Values
-
KEY_SERIALIZATION_SELECTOR
public static final java.lang.String KEY_SERIALIZATION_SELECTOR
Name of the header containing the serialization selector for keys.- See Also:
- Constant Field Values
-
VALUE_SERIALIZATION_SELECTOR_CONFIG
public static final java.lang.String VALUE_SERIALIZATION_SELECTOR_CONFIG
Name of the configuration property containing the serialization selector map for values with formatselector:class,...
.- See Also:
- Constant Field Values
-
KEY_SERIALIZATION_SELECTOR_CONFIG
public static final java.lang.String KEY_SERIALIZATION_SELECTOR_CONFIG
Name of the configuration property containing the serialization selector map for keys with formatselector:class,...
.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DelegatingSerializer
public DelegatingSerializer()
Construct an instance that will be configured inconfigure(Map, boolean)
with producer propertiesVALUE_SERIALIZATION_SELECTOR_CONFIG
andKEY_SERIALIZATION_SELECTOR_CONFIG
.
-
DelegatingSerializer
public DelegatingSerializer(java.util.Map<java.lang.String,org.apache.kafka.common.serialization.Serializer<?>> delegates)
Construct an instance with the supplied mapping of selectors to delegate serializers. The selector must be supplied in theKEY_SERIALIZATION_SELECTOR
and/orVALUE_SERIALIZATION_SELECTOR
headers. It is not necessary to configure standard serializers supported bySerdes
.- Parameters:
delegates
- the map of delegates.
-
-
Method Detail
-
configure
public void configure(java.util.Map<java.lang.String,?> configs, boolean isKey)
- Specified by:
configure
in interfaceorg.apache.kafka.common.serialization.Serializer<java.lang.Object>
-
createDelegates
protected static java.util.Map<java.lang.String,org.apache.kafka.common.serialization.Serializer<?>> createDelegates(java.lang.String mappings, java.util.Map<java.lang.String,?> configs, boolean isKey)
-
createInstanceAndConfigure
protected static void createInstanceAndConfigure(java.util.Map<java.lang.String,?> configs, boolean isKey, java.util.Map<java.lang.String,org.apache.kafka.common.serialization.Serializer<?>> delegateMap, java.lang.String selector, java.lang.String className)
-
instantiateAndConfigure
protected static void instantiateAndConfigure(java.util.Map<java.lang.String,?> configs, boolean isKey, java.util.Map<java.lang.String,org.apache.kafka.common.serialization.Serializer<?>> delegateMap, java.lang.String selector, java.lang.Class<?> clazz)
-
addDelegate
public void addDelegate(java.lang.String selector, org.apache.kafka.common.serialization.Serializer<?> serializer)
-
removeDelegate
@Nullable public org.apache.kafka.common.serialization.Serializer<?> removeDelegate(java.lang.String selector)
-
serialize
public byte[] serialize(java.lang.String topic, java.lang.Object data)
- Specified by:
serialize
in interfaceorg.apache.kafka.common.serialization.Serializer<java.lang.Object>
-
serialize
public byte[] serialize(java.lang.String topic, org.apache.kafka.common.header.Headers headers, java.lang.Object data)
- Specified by:
serialize
in interfaceorg.apache.kafka.common.serialization.Serializer<java.lang.Object>
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfaceorg.apache.kafka.common.serialization.Serializer<java.lang.Object>
-
-