kafka消费

https://blog.csdn.net/AlbenXie/article/details/120341867
配置
// 设置为批量消费,每个批次数量在Kafka配置参数中设置
factory.setBatchListener(true);
type: batch
------------------------
concurrency: 在消费的时候,一共生成n个消费者来消费消息,类似于多线程,但数量一般小于等于分区数
public void listenBatch2(ConsumerRecord<String,String> record, Acknowledgment acknowledgment) {
@KafkaListener(topics = "#{kafkaConfig.topicName}")
//    public void listen3(ConsumerRecord<String,String> record, Acknowledgment acknowledgment){
//    @KafkaListener(topics = "testkafka", concurrency = "3")
public void listenBatch1(List<ConsumerRecord<String, String>> record, Acknowledgment acknowledgment) {
String msg = record.value();



// @KafkaListener(topics = "topic2", concurrency = "3", groupId = "0")
public void listenRealtimeData2(String msg, Acknowledgment acknowledgment) {
// 转换为对象
JSONArray jsonArray = JSON.parseArray(msg);
XXX xxxList = jsonArray.toJavaList(XXX.class);
ListenableFuture<SendResult<String, String>> send =
kafkaTemplate.send("test-topic", JSONObject.toJSONString(对象));
//                    send.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
// //发送消息成功回调
// @Override
// public void onSuccess(SendResult<String, String> result) {
//getRecordMetadata里面存在发送的topic和partition等信息
// String topic = result.getRecordMetadata().topic();
// int partition = result.getRecordMetadata().partition();
// long offset = result.getRecordMetadata().offset();
// log.info("发送消息成功回调topic->:" + topic + "-partition=" + partition + "-offset=" + offset);
// }
// //发送消息失败回调
// @Override
// public void onFailure(Throwable ex) {
// log.info("发送消息失败回调=:" + ex.getMessage());
// }
// });





@KafkaListener(containerFactory ="firstKafkaListenerContainerFactory", topics = "topic1",
concurrency = "3", groupId = "a8")
public void listenTermAlarmBatch(List<String> msg, Acknowledgment acknowledgment) {
for(String jsonStr : msg) {
JSONArray jsonArray = JSON.parseArray(jsonStr);
XXXX xxxList= jsonArray.toJavaList(XXXX.class);
for (XXXX xxx: xxxList) {



---

根据key,对应处理
//        String msg = record.value();
// JSONObject jsMsg = JSON.parseObject(msg);
// for (Map.Entry<String, Object> entry : jsMsg.entrySet()) {
// ProtocolTypeEnum protocolTypeEnum = ProtocolTypeEnum.valueOf(entry.getKey());
// protocolTypeEnum.preHandle(entry.getValue().toString());
// }






posted on 2024-03-21 16:43  daofree  阅读(10)  评论(0编辑  收藏  举报