发送者结果通道
从 4.0.3 版本开始,您可以配置 resultMetadataChannel 以接收 SenderResult<?>,从而确定发送的成功/失败。
SenderResult 包含 correlationMetadata 以允许您将结果与发送关联起来;它还包含 RecordMetadata,指示已发送记录的 TopicPartition 和偏移量。
resultMetadataChannel 必须 是 FluxMessageChannel 实例。
以下是一个如何使用此功能的示例,关联元数据类型为 Integer
@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 标头
streamBridge.send("words1", MessageBuilder.withPayload("foobar")
.setCorrelationId(42)
.build());
当与 Function 一起使用该功能时,函数输出类型必须是 Message<?>,并且关联 ID 标头设置为所需的值。
元数据应该是唯一的,至少在发送期间是如此。