How to use multi-thread consumer in kafka 0.9.0?

后端 未结 3 1894
忘了有多久
忘了有多久 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.

    0 讨论(0)
  • 2020-12-15 11:20

    It is throwing the exception on your call to subscribe. this.consumer.subscribe(topicName);

    Move that block into a synchronized block like this:

    @Override
    public void run() {
        try {
            synchronized (consumer) {
                this.consumer.subscribe(topicName);
            }
            ConsumerRecords<String, String> records;
            while (!closed.get()) {
                synchronized (consumer) {
                    records = consumer.poll(100);
                }
                for (ConsumerRecord<String, String> tmp : records) {
                    System.out.println(tmp.value());
                }
            }
        } catch (WakeupException e) {
            // Ignore exception if closing
            System.out.println(e);
            //if (!closed.get()) throw e;
        }
    }
    
    0 讨论(0)
  • 2020-12-15 11:28

    Maybe is not your case, but if you are mergin processing of data of serveral topics, then you can read data from multiple topics with the same consumer. If not, then is preferable to create separate jobs consuming each topic.

    0 讨论(0)
提交回复
热议问题