繁体   English   中英

spring-kafka:如何为 ConcurrentMessageListenerContainer 提供 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.

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