Apache Kafka Producer Consumer API问题

时间:2017-09-25 09:35:20

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

我正在尝试创建一个简单的Java API来测试Kafka Producer和Consumer。当我在Mac机器上的不同终端上运行生产者和消费者时,它的工作正常。但是当我尝试使用java api代码连接到Kafka服务器时,出现此错误:

线程中的异常" main"显示java.lang.NullPointerException     在kafkatest2.ProducerTest.main(ProducerTest.java:34)

制作人代码:

    package kafkatest2;
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
public class ProducerTest {
  public static void main(String[] args) {
    Properties props = new Properties();
    props.put("bootstrap.servers", "localhost:9092");
    props.put("acks", "all");
    props.put("retries", 0);
   props.put("batch.size", 16384);
    props.put("linger.ms", 1);
    props.put("buffer.memory", 33554432);
props.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");

    Producer<String, String> producer = null;
    try {
          producer = new KafkaProducer<>(props);
      for (int i = 0; i < 10; i++) {
        String msg = "Message " + i;
        producer.send(new ProducerRecord<String, String>("tested", msg));
        System.out.println("Sent:" + msg);
      }
    } catch (Exception e) {
      e.printStackTrace();

    } finally {
    System.out.println("last");
      producer.close();
    }

  }

}

消费者代码:

 package kafkatest2;

import java.util.Arrays;
import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

public class ConsumerTest {

  public static void main(String[] args) {
    Properties props = new Properties();
    props.put("zookeeper.connect", "127.0.0.1:2181");
    props.put("group.id", "test-consumer-group");
    props.put("enable.auto.commit", "true");
    props.put("auto.commit.interval.ms", "1000");
    props.put("auto.offset.reset", "earliest");
    props.put("session.timeout.ms", "30000");
    props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

    KafkaConsumer<String, String> kafkaConsumer1 = new KafkaConsumer<>(props);
    kafkaConsumer1.subscribe(Arrays.asList("tested"));
    while (true) {
      ConsumerRecords<String, String> records = kafkaConsumer1.poll(10);
      for (ConsumerRecord<String, String> record : records) {
        System.out.println("Partition: " + record.partition() + " Offset: " + record.offset() + " Value: " + record.value() + " ThreadID: " + Thread.currentThread().getId());
      }
    }

  }

}

请让我知道我错过了什么?是否存在配置值的问题

谢谢, VIPUL

1 个答案:

答案 0 :(得分:0)

堆栈跟踪表示生产者代码中的NPE:

Exception in thread "main" java.lang.NullPointerException at 
kafkatest2.ProducerTest.main(ProducerTest.java:34)

如果producer = new KafkaProducer<>(props);抛出异常,就会发生这种情况。如果是这样,当您输入finally块时,将不会定义局部变量producer,因此调用producer.close()将抛出NPE。只需将呼叫包裹在if (producer != null)块中即可。