How to use multi-thread consumer in kafka 0.9.0?

后端 未结 3 1947
忘了有多久
忘了有多久 2020-12-15 10:56

The doc of kafka give an approach about with following describes:

One Consumer Per Thread:A simple option is to give each thread its own consumer > in

3条回答
  •  悲&欢浪女
    2020-12-15 11:19

    Kafka consumer is not thread safe. As you pointed out in your question, the document stated that

    A simple option is to give each thread its own consumer instance

    But in your code, you have the same consumer instance wrapped by different KafkaConsumerRunner instances. Thus multiple threads are accessing the same consumer instance. The kafka documentation clearly stated

    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.

    That's exactly the exception you received.

提交回复
热议问题