MallRetryPersonLabelConsumer.java 4.69 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.DPersonLabelDao;
import com.viontech.keliu.entity.PersonLabelContent;
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 MallRetryPersonLabelConsumer {
    @Autowired
    private ObjectMapper objectMapper;
    @Resource
    private DPersonLabelDao dPersonLabelDao;
    @Resource
    private SpeedStatService speedStatService;

    @KafkaListener(topics = KafkaConstants.TOPIC_MALL_RETRY_PERSONLABEL
            , autoStartup = "${vion.consumer.mallRetryPersonLabel.autoStartup:false}"
            , groupId = "MallRetryPersonLabelToDb"
            , concurrency = "${vion.consumer.mallRetryPersonLabel.concurrency:1}")
    public void consumerMallRetryPersonLabel(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<PersonLabelContent> labelList = new ArrayList<>();
                        for (ConsumerRecord<String, String> consumerRecord : recordValues) {
                            try {
                                PersonLabelContent dataContent = objectMapper.readValue(consumerRecord.value(), PersonLabelContent.class);
                                if (dataContent != null) {
                                    labelList.add(dataContent);
                                }
                            } catch (Exception ee) {
                                log.error("处理Mall_Retry_PersonLabel[{}], JsonDeserializerThrowable={}", entry.getKey(), ee.getMessage(), ee);
                            }
                        }
                        if (!CollectionUtils.isEmpty(labelList)) {
                            for (PersonLabelContent content : labelList) {
                                try {
                                    dPersonLabelDao.insert(content);
                                    speedStatService.stat(RedisConstants.PDS_MALLRETRYPERSONLABEL_WRITE, 1);
                                } catch (Exception ex) {
                                    log.error("处理Mall_Retry_PersonLabel[{}], mallid={}, unid={}, insert.Exception={}", entry.getKey(), content.getMallId(), content.getRecognitionUnid(), 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_PersonLabel[{}], batchHandle耗时:{} ms", entry.getKey(), System.currentTimeMillis() - startTime);
                } catch (Throwable e) {
                    log.error("处理Mall_Retry_PersonLabel[{}], Throwable={}", entry.getKey(), e.getMessage(), e);
                }
            }
        } catch (Throwable exx) {
            log.error("处理Mall_Retry_PersonLabel.Throwable={}", exx.getMessage(), exx);
        }
    }
}