MallRetryFaceCaptureConsumer.java 4.74 KB
package com.viontech.keliu.consumer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.viontech.keliu.constants.KafkaConstants;
import com.viontech.keliu.constants.RedisConstants;
import com.viontech.keliu.dao.DFaceRecognitionDao;
import com.viontech.keliu.entity.FaceDataContent;
import com.viontech.keliu.service.SpeedStatService;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.TopicPartition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class MallRetryFaceCaptureConsumer {
    @Autowired
    private ObjectMapper objectMapper;
    @Resource
    private DFaceRecognitionDao dFaceRecognitionDao;
    @Resource
    private SpeedStatService speedStatService;

    @KafkaListener(topics = KafkaConstants.TOPIC_MALL_RETRY_FACECAPTURE
            , autoStartup = "${vion.consumer.mallRetryFaceCapture.autoStartup:false}"
            , groupId = "MallRetryFaceCaptureToDb"
            , concurrency = "${vion.consumer.mallRetryFaceCapture.concurrency:1}")
    public void consumerMallRetryFaceCapture(List<ConsumerRecord<String, String>> recordList, Consumer<?, ?> consumer) {
        if (CollectionUtils.isEmpty(recordList)) {
            return;
        }
        try {
            Map<String, List<ConsumerRecord<String, String>>> topicPartitionDataMap = recordList.stream().collect(Collectors.groupingBy(d -> d.topic() + "-" + d.partition()));
            for (Map.Entry<String, List<ConsumerRecord<String, String>>> entry : topicPartitionDataMap.entrySet()) {
                try {
                    long startTime = System.currentTimeMillis();
                    List<ConsumerRecord<String, String>> recordValues = entry.getValue();
                    if (!CollectionUtils.isEmpty(recordValues)) {
                        ConsumerRecord<String, String> lastRecord = recordValues.get(recordValues.size() - 1);
                        List<FaceDataContent> faceDataList = new ArrayList<>();
                        for (ConsumerRecord<String, String> consumerRecord : recordValues) {
                            try {
                                FaceDataContent faceDataContent = objectMapper.readValue(consumerRecord.value(), FaceDataContent.class);
                                if (faceDataContent != null) {
                                    faceDataList.add(faceDataContent);
                                }
                            } catch (Exception ee) {
                                log.error("处理Mall_Retry_FaceCapture[{}], JsonDeserializerThrowable={}", entry.getKey(), ee.getMessage(), ee);
                            }
                        }
                        if (!CollectionUtils.isEmpty(faceDataList)) {
                            for (FaceDataContent faceDataContent : faceDataList) {
                                try {
                                    dFaceRecognitionDao.insert(faceDataContent);
                                    speedStatService.stat(RedisConstants.PDS_MALLRETRYFACECAPTURE_WRITE, 1);
                                } catch (Exception ex) {
                                    log.error("处理Mall_Retry_FaceCapture[{}], mallid={}, unid={}, insert.Exception={}", entry.getKey(), faceDataContent.getMallId(), faceDataContent.getUnid(), ex.getMessage(), ex);
                                }
                            }
                        }
                        // 提交Offset
                        Map<TopicPartition, OffsetAndMetadata> offsets = new HashMap<>();
                        offsets.put(
                                new TopicPartition(lastRecord.topic(), lastRecord.partition()),
                                new OffsetAndMetadata(lastRecord.offset() + 1) // 提交下一条偏移量
                        );
                        consumer.commitSync(offsets);
                    }
                    log.info("处理Mall_Retry_FaceCapture[{}], batchHandle耗时:{} ms", entry.getKey(), System.currentTimeMillis() - startTime);
                } catch (Throwable e) {
                    log.error("处理Mall_Retry_FaceCapture[{}], Throwable={}", entry.getKey(), e.getMessage(), e);
                }
            }
        } catch (Throwable exx) {
            log.error("处理Mall_Retry_FaceCapture.Throwable={}", exx.getMessage(), exx);
        }
    }
}