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