【发布时间】:2020-06-24 19:59:03
【问题描述】:
我目前正在使用 Spring Kafka 以及 Spring 的 @Retry 来使用来自主题的消息。所以基本上,如果出现错误,我会重试处理消费者消息。但是在这样做的同时,我想避免KafkaMessageListenerContainer 抛出的异常消息。相反,我想显示一条自定义消息。我尝试在ConcurrentKafkaListenerContainerFactory 中添加错误处理程序,但这样做时,我的重试不会被调用。
有人可以指导我如何显示自定义异常消息以及@Retry 场景吗?以下是我的代码 sn-ps:
ConcurrentKafkaListenerContainerFactory Bean 配置
@Bean
ConcurrentKafkaListenerContainerFactory << ? , ? > concurrentKafkaListenerContainerFactory(ConcurrentKafkaListenerContainerFactoryConfigurer configurer, ConsumerFactory < Object, Object > kafkaConsumerFactory) {
ConcurrentKafkaListenerContainerFactory < Object, Object > kafkaListenerContainerFactory =
new ConcurrentKafkaListenerContainerFactory < > ();
configurer.configure(kafkaListenerContainerFactory, kafkaConsumerFactory);
kafkaListenerContainerFactory.setConcurrency(1);
// Set Error Handler
/*kafkaListenerContainerFactory.setErrorHandler(((thrownException, data) -> {
log.info("Retries exhausted);
}));*/
return kafkaListenerContainerFactory;
}
卡夫卡消费者
@KafkaListener(
topics = "${spring.kafka.reprocess-topic}",
groupId = "${spring.kafka.consumer.group-id}",
containerFactory = "concurrentKafkaListenerContainerFactory"
)
@Retryable(
include = RestClientException.class,
maxAttemptsExpression = "${spring.kafka.consumer.max-attempts}",
backoff = @Backoff(delayExpression = "${spring.kafka.consumer.backoff-delay}")
)
public void onMessage(ConsumerRecord < String, String > consumerRecord) throws Exception {
// Consume the record
log.info("Consumed Record from topic : {} ", consumerRecord.topic());
// process the record
messageHandler.handleMessage(consumerRecord.value());
}
【问题讨论】:
标签: spring-boot apache-kafka spring-kafka