我正在使用主题上的日志压缩来构建Kafka应用程序,但无法发送逻辑删除值(KafkaNull)
我曾尝试对序列化程序使用默认配置,但当该配置不起作用时,我使用了建议的“ Publish null/tombstone message with raw headers”更改,将application.properties设置为:
spring.cloud.stream.output.producer.useNativeEncoding=true
spring.cloud.stream.kafka.binder.configuration.value.serializer=org.springframework.kafka.support.serializer.JsonSerializer
我要向流发送消息的代码是
this.stockTopics.compactedStocks().send(MessageBuilder
.withPayload(KafkaNull.INSTANCE)
.setHeader(KafkaHeaders.MESSAGE_KEY,company.getBytes())
.build())
this.stopTopics.compactedStocks()返回一个messageStream,我可以向其中发送消息。
每次我尝试使用KafkaNull实例作为有效负载发送该消息时,都会收到错误Failed to convert message: 'GenericMessage [payload=org.springframework.kafka.support.KafkaNull@1c2d8163, headers={id=f81857e7-fbd0-56f5-8418-6a1944e7f2b1, kafka_messageKey=[B@36ec022a, contentType=application/json, timestamp=1547827957485}]' to outbound message.
我希望消息仅以空值发送给使用者,但显然会出错。
答案 0 :(得分:2)
为此我打开了一个GitHub issue。
编辑
解决方法-这行得通...
@SpringBootApplication
@EnableBinding(Source.class)
public class So54257687Application {
public static void main(String[] args) {
SpringApplication.run(So54257687Application.class, args);
}
@Bean
public ApplicationRunner runner(MessageChannel output) {
return args -> output.send(new GenericMessage<>(KafkaNull.INSTANCE));
}
@KafkaListener(id = "foo", topics = "output")
public void listen(@Payload(required = false) byte[] in) {
System.out.println(in);
}
@Bean
@StreamMessageConverter
public MessageConverter kafkaNullConverter() {
class KafkaNullConverter extends AbstractMessageConverter {
KafkaNullConverter() {
super(Collections.emptyList());
}
@Override
protected boolean supports(Class<?> clazz) {
return KafkaNull.class.equals(clazz);
}
@Override
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint) {
return message.getPayload();
}
@Override
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint) {
return payload;
}
}
return new KafkaNullConverter();
}
}