java.lang.IllegalStateException:此使用者已被关闭

时间:2019-12-17 15:13:39

标签: java apache-kafka kafka-consumer-api

使用此配置设置kafka使用者

kafkaconfig:
  acks: 1
  autoCommit: true
  bootstrapServers: example.com:9092
  topic: item
  groupId: EWok-group
  keyDeserializer: org.apache.kafka.common.serialization.StringDeserializer
  valueDeserializer: org.apache.kafka.common.serialization.StringDeserializer
  maxPollRecords: 1
  pollMillisTime: 15
  retries: 5
  heartBeatInterval: 300
  sessionTimeout: 100000
  maxPollInterval: 30000

代码

 while (true) {
            try {
                ConsumerRecords<String, String> consumerRecords = eWokIntegrationConsumer.poll(Duration.of(kafkaCommConfig.getPollMillisTime(), ChronoUnit.SECONDS));
                if (!consumerRecords.isEmpty()) {
                    LOG.info("Consumed Record Count: {}", consumerRecords.count());
                    consumerRecords.forEach(record -> {
                        System.out.printf("offset = %d, key = %s, value = %s\n", record.offset(), record.key(), record.value());
                        eWokMessageProcessor.onMessage(record.value());
                        eWokIntegrationConsumer.commitSync();
                    });
                } else {
                    LOG.info("Polling returned without any records.");
                }
            } catch (Exception exception) {
                LOG.error("Consumer was interrupted. But still continue to poll. Exception:", exception);
                eWokIntegrationConsumer.close();
            }
        }

10000 ms用于处理我们从kafka消费者那里收到的数据。

java.lang.IllegalStateException:此使用者已经关闭。

异常日志

java.lang.IllegalStateException: This consumer has already been closed.
    at org.apache.kafka.clients.consumer.KafkaConsumer.acquireAndEnsureOpen(KafkaConsumer.java:2202)
    at org.apache.kafka.clients.consumer.KafkaConsumer.commitSync(KafkaConsumer.java:1332)
    at org.apache.kafka.clients.consumer.KafkaConsumer.commitSync(KafkaConsumer.java:1298)

Kafka版本:kafka-clients-2.0.1

您能否提出任何建议,Kafka的配置应该如何使用?

1 个答案:

答案 0 :(得分:0)

我在源代码中的其他位置放置了System.exit(0)。这就是为什么使用者离开组并将其标记为已关闭的原因。

我已从源代码中删除System.exit(0),现在一切正常。