首页 > 代码库 > kafka 生产者java编码

kafka 生产者java编码

public class KafkaProducerDemo {

	
	public static void main(String[] args) throws InterruptedException {
		/*
		 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");
		 */
		Map<String, Object> config = new HashMap<String, Object>();
		config.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
		config.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
		config.put("bootstrap.servers", "m2:9092");
		Producer<String, String> producer = new KafkaProducer<String, String>(config);

		// 发送业务消息
		// 读取文件 读取内存数据库 读socket端口
		for (int i = 1; i <= 100; i++) {
			Thread.sleep(500);
			producer.send(new ProducerRecord<String, String>("test2", i + ""));
		}
	}
}

  

kafka 生产者java编码