Helius
2021-08-05 fdb91cc72f7cbe8c095a1ce6442c9259ff01ff06
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.xzx.gc.common.utils;
 
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.json.JSONUtil;
import com.google.common.base.CaseFormat;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.constant.MqConstants;
import com.xzx.gc.common.dto.log.OperationAppLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
 
import java.util.Date;
 
@Component
@Slf4j
public class MqUtil {
 
    @Autowired
    private AmqpTemplate amqpTemplate;
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private IdUtils idUtils;
 
    /**
     * 发送队列
     * @param queueName
     * @param t 队列KEY
     * @param <T> 对象
     */
    @Async
    public <T> void send(String queueName,T t) {
        if(t!=null&&!SpringUtil.isCloud()){
            ReflectUtil.setFieldValue(t,"id",idUtils.generate( CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, queueName),0));
            ReflectUtil.setFieldValue(t,"createTime",DateUtil.now());
            ReflectUtil.setFieldValue(t,"queueName",queueName);
            String s = JSONUtil.toJsonStr(t);
            String uuid = IdUtil.fastSimpleUUID()+RandomUtil.randomNumbers(16);
            Message message = MessageBuilder.withBody(s.getBytes())
                    .setContentType(MessageProperties.CONTENT_TYPE_JSON).setContentEncoding("utf-8")
                    .setMessageId(uuid).build();
            try {
                amqpTemplate.convertAndSend(queueName, message);
            } catch (Exception e) {
                redisUtil.rpush(Constants.REDIS_LOG_KEY + "errQueue", s);
                e.printStackTrace();
            }
 
        }
    }
 
 
    /**
     * 发送操作账户日志
     * @param operationAppLog
     */
    @Async
    public void sendApp(OperationAppLog operationAppLog){
        operationAppLog.setCreateTime(new Date());
        send(MqConstants.OPERATION_APP_LOG_QUEUE, operationAppLog);
    }
 
 
 
 
 
}