“墓碑”记录的空有效载荷与日志压缩

使用日志压缩时,你可以发送和接收消息用于识别密钥删除的有效载荷。 你也可以收到值是其他原因,比如可能返回的解串器当它无法反序列化某个值时。spring-doc.cadn.net.cn

生成零有效载荷

发送一个通过使用脉冲星模板你可以使用流流API并将空输入到值参数中newMessage()例如:spring-doc.cadn.net.cn

pulsarTemplate
        .newMessage(null)
        .withTopic("my-topic")
        .withSchema(Schema.STRING)
        .withMessageCustomizer((mb) -> mb.key("key:1234"))
        .send();
发送空值时必须指定模式类型,因为系统无法从中确定消息的类型有效载荷。

消耗空有效载荷

@PulsarListener@PulsarReader有效载荷根据其消息参数类型传递到监听器方法,具体如下:spring-doc.cadn.net.cn

参数类型 转账价值

原始spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

用户自定义spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

org.apache.pulsar.client.api.Message<T>spring-doc.cadn.net.cn

非零的脉冲星消息getValue()返回spring-doc.cadn.net.cn

org.springframework.messaging.Message<T>spring-doc.cadn.net.cn

非空 Spring 消息 其getPayload()返回脉冲星零spring-doc.cadn.net.cn

List<X>spring-doc.cadn.net.cn

非空列表,其条目(X)是上述类型之一,并相应地表现(即原始元素为等等)spring-doc.cadn.net.cn

org.apache.pulsar.client.api.Messages<T>spring-doc.cadn.net.cn

非空容器,包含非空脉冲星消息,其getValue()返回spring-doc.cadn.net.cn

当传入值为(即具有原始类型或用户定义类型的单记录监听器)你必须使用@Payload参数注释必要 = 错误.
使用Spring时org.springframework.messaging.Message对于你的监听器有效载荷类型,其通用类型信息必须足够宽泛以接受Message<PulsarNull>(例如:消息,留言<?>Message<Object>). 这是因为 Spring 消息不允许其有效载荷为空值,而是使用脉冲星零占 位 符。

如果是压缩日志的墓碑消息,通常还需要密钥,以便应用程序判断哪个密钥是”删除". 以下示例展示了这样的构型:spring-doc.cadn.net.cn

@PulsarListener(
        topics = "my-topic",
        subscriptionName = "my-topic-sub",
        schemaType = SchemaType.STRING)
void myListener(
        @Payload(required = false) String msg,
        @Header(PulsarHeaders.KEY) String key) {
    ...
}
@PulsarReader尚未支持@Header参数,因此在对数压缩场景中其实用性较低。