1、pruducer未关闭,或者main线程关闭太快,会导致producer生成者的消息并未完全发送给kafka
以下代码显示了,如果当前producer发送失败,则需要隔一段时间才会有数字打印出来
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Properties;
/*
* A Kafka client that publishes records to the Kafka cluster.
The producer is thread safe and sharing a single producer instance across threads will generally be faster than having multiple instances.
Here is a simple example of using the producer to send records with strings containing sequential numbers as the key/value pairs.
*/
public class KafkaProducerTest3 {
private static Producer<String, String> producer = null;
static {
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("acks", "all");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
producer = new KafkaProducer<String, String>(props);
}
public static void sendMsg(String topic,String value){
producer.send(new ProducerRecord<>(topic,value));
// producer.close();
}
public static void main(String[] args) {
// 这样直接发送数据会造成问题,kafka并没有接收到数据,因为主线程关闭太快
for (int i = 0; i < 1000; i++) {
sendMsg("dd","fffffff");
System.out.println(i);
}
// 加上主线程耗时,或者while true 就可以解决
while (true){}
}
}