admin
2024-10-17 b30fb8afd3cd6228bda9b182dc412bb3c8daf69c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.yeshi.buwan.util.mq.rabbit.consumer;
 
import com.google.gson.Gson;
import com.rabbitmq.client.Channel;
import com.yeshi.buwan.dto.mq.PPTVMQMsg;
import com.yeshi.buwan.service.inter.juhe.PPTVService;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.util.mq.rabbit.RabbitmqMsgConsumeUtil;
import com.yeshi.buwan.videos.pptv.entity.PPTVSeries;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
 
/**
 * @author hxh
 * @title: QueueHelloWorldListener
 * @description:
 * @date 2024/9/26 13:47
 */
@Component
public class VideoUpdatePPTVListener {
    private final static Logger logger = LoggerFactory.getLogger(VideoUpdatePPTVListener.class);
 
    @Resource
    private RabbitTemplate rabbitTemplate;
 
    @Resource
    private PPTVService pptvService;
 
    public void onMessage(Message message, Channel channel) throws Exception {
        RabbitmqMsgConsumeUtil.processMessage(message, channel, rabbitTemplate, () -> {
            String result = new String(message.getBody(), StandardCharsets.UTF_8);
            //TODO 处理消息
            if(!StringUtil.isNullOrEmpty(result)) {
                PPTVMQMsg pptvmqMsg = new Gson().fromJson(result, PPTVMQMsg.class);
                try {
                    switch (pptvmqMsg.getType()) {
                        case PPTVMQMsg.TYPE_ADD_OR_UPDATE:
                            PPTVSeries pptvSeries = pptvService.getSeriesDetail(pptvmqMsg.getInfoId());
                            if (pptvSeries != null) {
                                pptvService.addToVideoInfo(pptvSeries);
                            }
                            break;
 
                        case PPTVMQMsg.TYPE_DELETE:
                            pptvService.offLineSeries(pptvmqMsg.getInfoId());
                            break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error("PPTV添加到视频出错:" + e.getMessage());
                    logger.error("infoId:" + pptvmqMsg.getInfoId());
                }
 
            }
        });
    }
}