【kafka】生产者API 回调 同步
2021-01-18 03:12
标签:static dac stack code cep 缓冲区 发送消息 send main 【kafka】生产者API 回调 同步 标签:static dac stack code cep 缓冲区 发送消息 send main 原文地址:https://www.cnblogs.com/mussessein/p/12187626.html普通实现
public class MyProducer {
public static void main(String[] args) {
/**
* 创建Kafka生产者配置信息:ProducerConfig类中记录了Kafka需要的所有参数信息
* 1.指定连接的Kafka集群
* 2.ack应答级别
* 3.发送失败的重试次数
* 4.批次大小(一次发送多少大小数据)
* 5.等待时间
* 6.RecordAccumulator缓冲区大小
* 7.指定key,value序列化类
*/
Properties properties = new Properties();
properties.put("bootstrap.servers", "localhost:9092");
properties.put("acks", "all");
properties.put("retries", 1);
properties.put("batch.size", 16384);
properties.put("liner.ms", 1);
properties.put("buffer.memory", 33554432);
properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
/**
* 通过配置文件创建生产者对象
*/
KafkaProducer
同步实现
public class MyProducerFuture {
public static void main(String[] args) throws ExecutionException, InterruptedException {
Properties properties = new Properties();
properties.put("bootstrap.servers", "localhost:9092");
properties.put("acks", "all");
properties.put("retries", 1);
properties.put("batch.size", 16384);
properties.put("liner.ms", 1);
properties.put("buffer.memory", 33554432);
properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer
回调实现
public class MyProducerCallback {
public static void main(String[] args) {
Properties properties = new Properties();
properties.put("bootstrap.servers", "localhost:9092");
properties.put("acks", "all");
properties.put("retries", 1);
properties.put("batch.size", 16384);
properties.put("liner.ms", 1);
properties.put("buffer.memory", 33554432);
properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer
文章标题:【kafka】生产者API 回调 同步
文章链接:http://soscw.com/index.php/essay/43478.html