当我使用CuratorFrameworkFactory.newClient()时,获取KafkaConsumer对于多线程访问错误并不安全 [英] Getting KafkaConsumer is not safe for multi-threaded access error when I use CuratorFrameworkFactory.newClient()
问题描述
我在下面收到此错误:
java.util.ConcurrentModificationException: KafkaConsumer is not safe for multi-threaded access
当我将其包含在代码中时:
when I include this in my code:
CuratorFrameworkFactory.newClient()
我不明白是什么原因导致了错误.有帮助吗?
I don't understand what's causing the error. Any help?
谢谢.
推荐答案
Kafka Javdocs明确提到:
Kafka Javdocs clearly mentions that :
Kafka使用者不是线程安全的.所有网络I/O都发生在进行调用的应用程序线程中.用户有责任确保正确同步多线程访问.不同步的访问将导致ConcurrentModificationException.
The Kafka consumer is NOT thread-safe. All network I/O happens in the thread of the application making the call. It is the responsibility of the user to ensure that multi-threaded access is properly synchronized. Un-synchronized access will result in ConcurrentModificationException.
More details present at http://kafka.apache.org/21/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html#multithreaded
这篇关于当我使用CuratorFrameworkFactory.newClient()时,获取KafkaConsumer对于多线程访问错误并不安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!