Sender Result Channel
从版本 4.0.3 开始,可以配置 resultMetadataChannel
以接收 SenderResult<?>
,从而确定发送的成功/失败。
Starting with version 4.0.3, you can configure the resultMetadataChannel
to receive SenderResult<?>
s to determine success/failure of sends.
SenderResult
包含 correlationMetadata
,允许将结果与发送相关联;它还包含 RecordMetadata
,指示已发送记录的 TopicPartition
和偏移量。
The SenderResult
contains correlationMetadata
to allow you to correlate results with sends; it also contains RecordMetadata
, which indicates the TopicPartition
and offset of the sent record.
resultMetadataChannel
必须 是 FluxMessageChannel
实例。
The resultMetadataChannel
must be a FluxMessageChannel
instance.
以下是使用此功能(具有类型为 Integer
的相关元数据)的一个示例:
Here is an example of how to use this feature, with correlation metadata of type 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
标头:
To set the correlation metadata on an output record, set the CORRELATION_ID
header:
streamBridge.send("words1", MessageBuilder.withPayload("foobar")
.setCorrelationId(42)
.build());
在使用带有 Function
的该功能时,函数的输出类型必须是一个带有已设置至期望值的关联 ID 标头的 Message<?>
。
When using the feature with a Function
, the function output type must be a Message<?>
with the correlation id header set to the desired value.
元数据应该唯一,至少在发送过程中唯一。
Metadata should be unique, at least for the duration of the send.