傳送者結果通道
從 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 標頭設定為所需的值。
元資料應該是唯一的,至少在傳送期間是如此。