ps.本文所有源码都基于kafka-0.10.0.1
Kafka提供了Java版本的生产者实现--KafkaProducer,使用KafkaProducer的API可以轻松实现同步/异步发送消息、批量发送、超时重发等复杂的功能,KafkaProducer是线程安全的,多个线程之间可以共享实用同一个KafkaProducer对象。
下面先看一个使用上的小例子:
public static void main(String[] args) { boolean isAsync = args.length == 0 || !args[0].trim().equalsIgnoreCase("sync"); Properties properties = new Properties(); properties.put("bootstrap.servers", "localhost:9092"); properties.put("client.id", "DemoProducer"); properties.put("key.serializer", "org.apache.kafka.common.serialization.IntegerSerializer"); properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); KafkaProducer producer = new KafkaProducer<>(properties); String topic = "test"; int messageNo = 1; while(true) { String messageStr = "Message_" + messageNo; long startTime = System.currentTimeMillis(); if(isAsync) { //ProducerRecord可以当成一个消息,包含key,value,和topic //异步要有回调函数 producer.send(new ProducerRecord(topic, messageNo, messageStr), new DemoCallBack(startTime, messageNo, messageStr)); } else { //同步发送 try { //KafkaProducer.send()返回的类型是Future<RecordMetadata>