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);
|
}
|
|
|
|
|
|
}
|