How to use multi-thread consumer in kafka 0.9.0?

后端 未结 3 1948
忘了有多久
忘了有多久 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: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 records;
            while (!closed.get()) {
                synchronized (consumer) {
                    records = consumer.poll(100);
                }
                for (ConsumerRecord tmp : records) {
                    System.out.println(tmp.value());
                }
            }
        } catch (WakeupException e) {
            // Ignore exception if closing
            System.out.println(e);
            //if (!closed.get()) throw e;
        }
    }
    

提交回复
热议问题