spring-kafka ToStringSerializer 源码
spring-kafka ToStringSerializer 代码
文件路径:/spring-kafka/src/main/java/org/springframework/kafka/support/serializer/ToStringSerializer.java
/*
* Copyright 2016-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.kafka.support.serializer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.apache.kafka.common.header.Headers;
import org.apache.kafka.common.serialization.Serializer;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Generic {@link org.apache.kafka.common.serialization.Serializer Serializer} that relies on
* {@link Object#toString()} to get serialized representation of the entity.
*
* @param <T> class of the entity, representing messages
*
* @author Alexei Klenin
* @author Gary Russell
* @since 2.5
*/
public class ToStringSerializer<T> implements Serializer<T> {
/**
* Kafka config property for enabling/disabling adding type headers.
*/
public static final String ADD_TYPE_INFO_HEADERS = "spring.message.add.type.headers";
/**
* Header for the type of key.
*/
public static final String KEY_TYPE = "spring.message.key.type";
/**
* Header for the type of value.
*/
public static final String VALUE_TYPE = "spring.message.value.type";
private boolean addTypeInfo = true;
private Charset charset = StandardCharsets.UTF_8;
private String typeInfoHeader = VALUE_TYPE;
@Override
public void configure(Map<String, ?> configs, boolean isKey) {
if (isKey) {
this.typeInfoHeader = KEY_TYPE;
}
if (configs.containsKey(ADD_TYPE_INFO_HEADERS)) {
Object config = configs.get(ADD_TYPE_INFO_HEADERS);
if (config instanceof Boolean) {
this.addTypeInfo = (Boolean) config;
}
else if (config instanceof String) {
this.addTypeInfo = Boolean.parseBoolean((String) config);
}
else {
throw new IllegalStateException(
ADD_TYPE_INFO_HEADERS + " must be Boolean or String");
}
}
}
@Override
public byte[] serialize(String topic, @Nullable T data) {
return serialize(topic, null, data);
}
@Override
@Nullable
public byte[] serialize(String topic, @Nullable Headers headers, @Nullable T data) {
if (data == null) {
return null;
}
if (this.addTypeInfo && headers != null) {
headers.add(this.typeInfoHeader, data.getClass().getName().getBytes());
}
return data.toString().getBytes(this.charset);
}
@Override
public void close() {
// No-op
}
/**
* Get the addTypeInfo property.
* @return the addTypeInfo
*/
public boolean isAddTypeInfo() {
return this.addTypeInfo;
}
/**
* Set to false to disable adding type info headers.
* @param addTypeInfo true to add headers
*/
public void setAddTypeInfo(boolean addTypeInfo) {
this.addTypeInfo = addTypeInfo;
}
/**
* Set a charset to use when converting {@link String} to byte[]. Default UTF-8.
* @param charset the charset.
*/
public void setCharset(Charset charset) {
Assert.notNull(charset, "'charset' cannot be null");
this.charset = charset;
}
/**
* Get the configured charset.
* @return the charset.
*/
public Charset getCharset() {
return this.charset;
}
}
相关信息
相关文章
spring-kafka DelegatingByTopicDeserializer 源码
spring-kafka DelegatingByTopicSerialization 源码
spring-kafka DelegatingByTopicSerializer 源码
spring-kafka DelegatingByTypeSerializer 源码
spring-kafka DelegatingDeserializer 源码
spring-kafka DelegatingSerializer 源码
spring-kafka DeserializationException 源码
spring-kafka ErrorHandlingDeserializer 源码
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦