package com.ibeetl.admin.console.rabbitmq;
|
|
import cn.hutool.core.util.IdUtil;
|
import cn.hutool.core.util.RandomUtil;
|
import cn.hutool.json.JSONUtil;
|
import com.ibeetl.admin.console.service.RedisService;
|
import com.ibeetl.admin.core.util.enums.CoreDictType;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.amqp.AmqpException;
|
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.stereotype.Component;
|
|
|
@Component
|
@Slf4j
|
public class MqUtil {
|
|
@Autowired
|
private AmqpTemplate amqpTemplate;
|
|
@Autowired
|
RedisService redisUtil;
|
|
/**
|
* 发送队列
|
* @param queueName
|
* @param t 队列KEY
|
* @param <T> 对象
|
*/
|
public <T> void send(String queueName,T t) {
|
String s = JSONUtil.toJsonStr(t);
|
String uuid = IdUtil.fastSimpleUUID()+RandomUtil.randomNumbers(16);
|
redisUtil.set(CoreDictType.REDIS_LOG_KEY+uuid,queueName);
|
Message message = MessageBuilder.withBody(s.getBytes())
|
.setContentType(MessageProperties.CONTENT_TYPE_JSON).setContentEncoding("utf-8")
|
.setMessageId(uuid).build();
|
try {
|
amqpTemplate.convertAndSend(queueName,message);
|
log.info("发送队列:【{}】成功",queueName);
|
} catch (AmqpException e) {
|
redisUtil.remove(CoreDictType.REDIS_LOG_KEY+uuid);
|
log.error("发送队列失败:"+queueName,e);
|
}
|
}
|
|
|
}
|