40 lines
1.2 KiB
Java
Raw Normal View History

2025-10-30 10:46:28 +08:00
package com.cdzy.operations.component;
2025-10-30 14:54:39 +08:00
import com.cdzy.operations.service.CommandService;
2025-10-30 10:46:28 +08:00
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
2025-10-30 14:54:39 +08:00
import jakarta.annotation.Resource;
2025-10-30 10:46:28 +08:00
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
/**
* kafka消费者
*
* @author xiafan
*/
@Slf4j
@Component
public class KafkaConsumer {
2025-10-30 14:54:39 +08:00
@Resource
CommandService commandService;
2025-10-30 10:46:28 +08:00
/**
* 消费者监听消息
*
* @param record 消息
*/
@KafkaListener(topics = {"msg_rsp"})
public void onMessage(ConsumerRecord<?, ?> record) throws JsonProcessingException {
log.info("[KAFKA接收] 主题: {}, 内容: {}", record.topic(), record.value());
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readValue(record.value().toString(), JsonNode.class);
2025-10-30 14:54:39 +08:00
String tid = jsonNode.get("tid").asText();
commandService.onComplete(tid,true);
2025-10-30 10:46:28 +08:00
}
}