Spring kafka setErrorHandler 已弃用替换(引导 2.6.4)
在 Spring Boot 2.6.4 上,此方法已被弃用。
public ConcurrentKafkaListenerContainerFactory<Object, Object> kafkaListenerContainerFactory(
ConcurrentKafkaListenerContainerFactoryConfigurer configurer) {
var factory = new ConcurrentKafkaListenerContainerFactory<Object, Object>();
configurer.configure(factory, consumerFactory());
// deprecated
factory.setErrorHandler(new GlobalErrorHandler());
return factory;
}
全局错误处理程序类
public class GlobalErrorHandler implements ConsumerAwareErrorHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalErrorHandler.class);
@Override
public void handle(Exception thrownException, ConsumerRecord<?, ?> data, Consumer<?, ?> consumer) {
// my custom global logic (e.g. notify ops team via slack)
}
}
的替代示例是什么?文档说我应该使用 setCommonErrorHandler ,但是如何实现 CommonErrorHandler 接口,因为那里没有可以重写的方法。
要点是,我必须根据某些条件(消息 tpye,在 kafka 消息头中可用)向运营团队发送松弛通知,
这不是阻塞,只是一条烦人的已弃用消息。 谢谢
On spring boot 2.6.4, this method is deprecated.
public ConcurrentKafkaListenerContainerFactory<Object, Object> kafkaListenerContainerFactory(
ConcurrentKafkaListenerContainerFactoryConfigurer configurer) {
var factory = new ConcurrentKafkaListenerContainerFactory<Object, Object>();
configurer.configure(factory, consumerFactory());
// deprecated
factory.setErrorHandler(new GlobalErrorHandler());
return factory;
}
The global error handler class
public class GlobalErrorHandler implements ConsumerAwareErrorHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalErrorHandler.class);
@Override
public void handle(Exception thrownException, ConsumerRecord<?, ?> data, Consumer<?, ?> consumer) {
// my custom global logic (e.g. notify ops team via slack)
}
}
What is the replacement sample for this? The doc says I should use setCommonErrorHandler
, but how to implements the CommonErrorHandler
interface, as no method to be overriden there.
Point is, I have to send slack notification to ops team, based on certain condition (the message tpye, which is available on kafka message header)
This is not blocking, just an annoying deprecated message though.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我面临着完全相同的问题,所以我改变了方法实现 ConsumerAwareErrorHandler
并实现
像文档中描述的那样
并且它有效!在KafkaConfig.class中
I was facing exactly the same problem, so I changed the method implementation ConsumerAwareErrorHandler by
and implemented
like described in the docs and it works!
In KafkaConfig.class
请参阅 Spring for Apache Kafka 文档;旧版错误处理程序已替换为 CommonErrorHandler 实现。
有什么新变化?
https: //docs.spring.io/spring-kafka/docs/current/reference/html/#x28-eh
容器错误处理程序
https: //docs.spring.io/spring-kafka/docs/current/reference/html/#error-handlers
See the Spring for Apache Kafka documentation; legacy error handlers are replaced with
CommonErrorHandler
implementations.What's New?
https://docs.spring.io/spring-kafka/docs/current/reference/html/#x28-eh
Container Error Handlers
https://docs.spring.io/spring-kafka/docs/current/reference/html/#error-handlers
对于使用较新版本的 Spring-Boot 3.0.x 的任何人,以下是基于 @GarryRussel 输入的实现:
从 Spring Boot 2.9 开始,handleRecord() 已被弃用
handleOne() 是它的替代品。
For anyone who is on the the newer versions of Spring-Boot 3.0.x, here is the implementation based on @GarryRussel's input:
handleRecord() has been deprecated from Spring Boot 2.9
handleOne() is its replacement.