![](/img/trans.png)
[英]spring-kafka: DeadLetterPublishingRecoverer vs RetryTopicConfiguration
[英]spring-kafka: How to provide RetryTopicConfiguration to ConcurrentMessageListenerContainer?
我必須在不使用注釋的情況下設置 Kafka 消費者。 我有這樣的事情:
public class MyCustomListener<V> implements MessageListener<String, V> {
private final String topicName;
private final Class<V> recordType;
private final String recordTypeName;
private final KafkaProperties props;
private final Logger logger;
private final ConcurrentMessageListenerContainer<String, V> listenerContainer;
public MyCustomListener(
@NotNull String topicName,
@NotNull Class<V> recordType,
@NotNull KafkaProperties props,
Logger logger
) {
this.topicName = topicName;
this.recordType = recordType;
this.recordTypeName = recordType.getSimpleName();
this.props = props;
this.logger = logger;
this.listenerContainer = setup();
}
protected ConcurrentMessageListenerContainer<String, V> setup() {
logger.info("Setting up Kafka listener container for topic {}/{}", topicName, recordTypeName);
var consumerFactory =
new DefaultKafkaConsumerFactory<>(
new HashMap<>() {{
put(BOOTSTRAP_SERVERS_CONFIG, props.getBootstrapServers());
put(GROUP_ID_CONFIG, props.getConsumer().getGroupId());
}},
new StringDeserializer(),
new MyDataDeserializer<>()
);
var containerProps = new ContainerProperties(topicName);
containerProps.setMessageListener(this);
var container = new ConcurrentMessageListenerContainer<>(consumerFactory, containerProps);
container.start();
return container;
}
@Override
public final void onMessage(ConsumerRecord<String, V> record) {
logger.info(
"(#{}) Received {} message in Kafka: ({})",
Thread.currentThread().getId(),
recordTypeName,
record.key()
);
# Actual processing of record code here ...
}
}
以上大部分工作。
如何將
RetryTopicConfiguration
關聯/提供到上述設置? 這樣我就可以發布重試主題和 DLT 主題以及重試配置中指定的退避。
我可以將重試主題配置部分Bean
,例如:
public RetryTopicConfiguration myRetryTopicConfig(
KafkaProperties props,
KafkaTemplate<String, MyData> template,
...
) {
return RetryTopicConfigurationBuilder
.newInstance()
.fixedBackOff(..)
.useSingleTopicForFixedDelays()
...
.create(template);
}
目前不支持將 RetryTopicConfiguration 與手動創建的沒有@KafkaListener
注釋的偵聽器/容器一起使用:
目前,非阻塞重試機制是圍繞@KafkaListener 構建的; 您必須進行大量手動接線才能使用手動創建的容器進行設置。 我們希望在未來讓此類用例變得更容易,也許在 3.0 中。
您可以在項目的存儲庫中對此問題進行投票或評論以表達您的興趣 - 3.0
將於今年晚些時候發布。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.