此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 spring-cloud-stream 4.1.4spring-doc.cadn.net.cn

发送方结果通道

从版本 4.0.3 开始,您可以配置resultMetadataChannel接收SenderResult<?>S 来确定发送的成功/失败。spring-doc.cadn.net.cn

SenderResult包含correlationMetadata允许您将结果与发送相关联;它还包含RecordMetadata,它表示TopicPartition和 offset 的发送记录。spring-doc.cadn.net.cn

resultMetadataChannel 必须是FluxMessageChannel实例。spring-doc.cadn.net.cn

以下是如何使用此功能的示例,其中包含Integer:spring-doc.cadn.net.cn

@Bean
FluxMessageChannel sendResults() {
    return new FluxMessageChannel();
}

@ServiceActivator(inputChannel = "sendResults")
void handleResults(SenderResult<Integer> result) {
    if (result.exception() != null) {
        failureFor(result);
    }
    else {
        successFor(result);
    }
}

要在输出记录上设置关联元数据,请将CORRELATION_ID页眉:spring-doc.cadn.net.cn

streamBridge.send("words1", MessageBuilder.withPayload("foobar")
        .setCorrelationId(42)
        .build());

当将该功能与Function,函数输出类型必须为Message<?>并将 Correlation ID 标头设置为所需的值。spring-doc.cadn.net.cn

元数据应该是唯一的,至少在发送期间是唯一的。spring-doc.cadn.net.cn