发送者结果通道
从 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头部需设置为所需的值。
元数据应该是唯一的,至少在发送期间如此。