我正在尝试使用Java编写一个天真的Kafka Producer。该应用程序接受两个输入:
我写了以下代码。当我运行它时,我看到System.out.println
语句打印了预期的值,但由于某种原因不会向Kafka生成消息。我应该改变什么才能使它发挥作用?
package com.myname.kafka.producer;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
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 NaiveKafkaProducer {
private static final Properties properties = new Properties();
private static Producer<String, String> producer;
private static String topic;
private static BufferedReader br;
static {
properties.put("bootstrap.servers", "localhost:9092");
properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("request.required.acks", "all");
System.out.println("Creating Kafka producer with the following properties :: " + properties);
producer = new KafkaProducer<>(properties);
}
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
try {
if(args.length != 0) {
topic = args[0];
File file = new File(args[1]);
br = new BufferedReader((Reader) new FileReader(file));
}
} catch (Exception e) {
System.out.println("Check input arguments. Error thrown while populating arguments to local variables");
e.printStackTrace();
}
String msg;
while ((msg = br.readLine()) != null) {
System.out.println("Message to publish : " + msg);
System.out.println("Topic : " + topic);
producer.send(new ProducerRecord<String, String>(topic, "", msg));
}
return;
}
}
令人惊讶的是,以下代码可以工作(我已经对所有内容进行了硬编码):
package com.myname.kafka.producer;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
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 NaiveKafkaProducer {
private static final Properties properties = new Properties();
private static Producer<String, String> producer;
private static String topic;
private static BufferedReader br;
static {
properties.put("bootstrap.servers", "localhost:9092");
properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("request.required.acks", "all");
System.out.println("Creating Kafka producer with the following properties :: " + properties);
producer = new KafkaProducer<>(properties);
}
public static void main(String[] args) throws IOException {
try {
String[] msgs = new String[2];
msgs[0] = "message 1";
msgs[1] = "message 2";
topic = "mytopic"
for(String msg:msgs){
producer.send(new ProducerRecord<String, String>(topic, "", msg));
}
producer.close();
} catch (Exception e) {
System.out.println("Exception caught in main method while trying to produce the messages to Kafka");
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
在第二个代码段中调用了一个批评方法,并在第一个
中丢失了 producer.close();
来自该方法的文件:
关闭此制片人。此方法将阻塞,直到所有先前发送的请求完成。
当您调用方法produce
时,实际上并不意味着消息已生成。方法返回future
。您可以通过在每个生成方法的结果上调用get()
来等待生成每条消息。