简体   繁体   English

spring-kafka:如何为 ConcurrentMessageListenerContainer 提供 RetryTopicConfiguration?

[英]spring-kafka: How to provide RetryTopicConfiguration to ConcurrentMessageListenerContainer?

I have to set up Kafka consumers without using annotations.我必须在不使用注释的情况下设置 Kafka 消费者。 I have something like this:我有这样的事情:

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 ...
  }
}


The above works for the most part.以上大部分工作。

How do I associate/provide RetryTopicConfiguration to the above setup?如何将RetryTopicConfiguration关联/提供到上述设置? So that I am able to publish to retry topic and DLT topics and backoff as specified in the retry config.这样我就可以发布重试主题和 DLT 主题以及重试配置中指定的退避。

I am fine with just the retry topic config part being a Bean , say:我可以将重试主题配置部分Bean ,例如:

public RetryTopicConfiguration myRetryTopicConfig(
    KafkaProperties props,
    KafkaTemplate<String, MyData> template,
    ...
  ) {
  return RetryTopicConfigurationBuilder
    .newInstance()
    .fixedBackOff(..)
    .useSingleTopicForFixedDelays()
    ...
    .create(template);
}

Such usage of RetryTopicConfiguration with manually created listeners / containers without @KafkaListener annotations is currently not supported:目前不支持将 RetryTopicConfiguration 与手动创建的没有@KafkaListener注释的侦听器/容器一起使用:

Currently, the non-blocking retries mechanism is built around @KafkaListener;目前,非阻塞重试机制是围绕@KafkaListener 构建的; you would have to do a lot of manual wiring to set it up with manually created container(s).您必须进行大量手动接线才能使用手动创建的容器进行设置。 We hope to make it easier for such use cases in future, perhaps in 3.0.我们希望在未来让此类用例变得更容易,也许在 3.0 中。

You can upvote or comment this issue in the project's repository to show your interest - the 3.0 is due later this year.您可以在项目的存储库中对此问题进行投票或评论以表达您的兴趣 - 3.0将于今年晚些时候发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM