Helius
2021-06-21 5812839f3e23bb09e1a9b100b63273a646155709
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
package com.xzx.log.service;
 
import cn.hutool.core.util.StrUtil;
import com.rabbitmq.client.Channel;
import com.xzx.log.constants.Constants;
import com.xzx.log.constants.MqConstants;
import com.xzx.log.constants.QueueEnum;
import com.xzx.log.entity.OperationAppLog;
import com.xzx.log.mapper.OperationAppLogMapper;
import com.xzx.log.util.ExceptionUtils;
import com.xzx.log.util.MqUtil;
import com.xzx.log.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.IOException;
 
@Service
@Slf4j
public class OperationAppLogService {
 
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private OperationAppLogMapper operationAppLogMapper;
 
    /**
     * 监听队列
     *
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(queues = {MqConstants.OPERATION_APP_LOG_QUEUE})
    public void addQueue(Message message, Channel channel) throws IOException {
        long deliveryTag = MqUtil.getTag(message);
        OperationAppLog bean = MqUtil.getBean(message, OperationAppLog.class);
        boolean success=true;
        try {
            operationAppLogMapper.save(bean);
        }catch (Exception e) {
            success=false;
        }
        if(success) {
            channel.basicAck(deliveryTag, false);
        }else {
            channel.basicNack(deliveryTag, false, false);
        }
 
 
    }
 
 
}