package com.producer;
import com.alibaba.fastjson.JSON;
import com.baturu.message.Message;
import com.dto.OrderDetailTimeCostCompleteMessageDTO;
import com.dto.TimeCostCompleteMessageDTO;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.*;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.*;
import org.apache.kafka.clients.producer.Producer;
/**
* Date: 2019-05-20 00:04
*/
@Slf4j
public class ProducerDemo {
// 以下两个属性也可以作为send方法的参数,更灵活些
private static Properties properties = getConf();
private static String jsonMsg =getJsonMsg();
static void send() {
Producer<String, String> producer = null;
try {
producer = new KafkaProducer<>(properties);
ProducerRecord<String, String> record = new ProducerRecord<>("TOPIC", jsonMsg);
producer.send(record, (metadata, e)-> {
if (metadata != null) {
log.info("sent record(key={} value={}) meta(partition={}, offset={})",
record.key(), record.value(), metadata.partition(),
metadata.offset());
} else {
log.error(e.getMessage(), e);
}
});
} catch (Exception e) {
log.error(e.getMessage(), e);
}finally {
// 一定要关闭资源,否则消息发送不成功
try {
if (producer != null) {
producer.close();
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
private static Properties getConf() {
Properties properties = new Properties();
// ProducerConfig 类定义了生产者相关的配置
// 指定kafka 连接的集群
properties.put("bootstrap.servers", "xxx");
// ack应答级别
properties.put("acks", "all");
// 重试次数
properties.put("retries", 0);
// 单次发送批次的大小
properties.put("batch.size", 16384);
// 批次大小不足的情况等待发送时间
properties.put("linger.ms", 1);
// RecordAccumulator 缓冲区大小
properties.put("buffer.memory", 33554432);
// key - value序列化
properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
return properties;
}
private static String getJsonMsg() {
TimeCostCompleteMessageDTO messageDTO = TimeCostCompleteMessageDTO.builder()
.orderId(xxxxx)
.build();
Message message = new Message();
message.setContent(messageDTO);
retrun JSON.toJSONString(message);
}
}
【消息队列】kafka producer demo
最新推荐文章于 2024-02-16 23:08:36 发布