123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package com.free.mq;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import com.fasterxml.jackson.annotation.JsonInclude.Include;
- import java.util.Map;
- import java.util.HashMap;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
- import com.free.config.FormatJavaTimeModule;
- import com.free.entity.TransferApply;
- import com.free.utils.Utils;
- @Service
- public class MqService {
- private static final Logger log = LoggerFactory.getLogger(MqService.class);
- @Autowired
- RabbitTemplate rabbitTemplate;
- /**
- * 发送mq消息
- *
- * @param ex 交换机名
- * @param queue 队列名
- * @param msg 消息内容
- * @throws JsonProcessingException JSON处理异常
- */
- public void sendMsg(String ex, String queue, Map msg) throws JsonProcessingException {
- System.out.println(msg);
- String strMsg = null;
- ObjectMapper mapper = new ObjectMapper();
- // 空值放过
- mapper.setSerializationInclusion(Include.NON_NULL);
- // LocalDateTime处理
- JavaTimeModule jtmf = FormatJavaTimeModule.init();
- mapper.registerModule(jtmf);
- strMsg = mapper.writeValueAsString(msg);
- rabbitTemplate.convertAndSend(ex, queue, strMsg);
- }
- /**
- * 转人工对话mq消息
- * @param data ChatRecord数据
- */
- public void sendChatToApply(Map data) {
- Map map = new HashMap<>();
- map.put("type", "chat");
- map.put("data", data);
- String apply_id = (String) data.get("apply_id");
- String queue = MqListeners.queueName + "." + apply_id;
- try {
- this.sendMsg(MqListeners.exName, queue, map);
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- log.error("转人工对话通知mq发送失败", e);
- }
- }
- /**
- * 向管理员订阅队列发送消息,通知管理员去处理转人工申请
- *
- * @param data 转人工申请数据
- */
- public void sendMsgToAdminForApplyCreate(Map data) {
- String ex = MqListeners.adminExName;
- String queue = MqListeners.adminQueue;
- Map map = new HashMap<>();
- map.put("data", data);
- map.put("type", "notice");
- try {
- this.sendMsg(ex, queue, map);
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- log.error("转人工申请通知mq发送失败", e);
- }
- }
- /**
- * 转人工已受理通知
- *
- * @param apply
- */
- public void sendMsgToApplicant(TransferApply apply) {
- String ex = MqListeners.exName;
- String routerKey = MqListeners.queueName + "." + apply.getUser_id();
- Map map = new HashMap<>();
- Map data = Utils.objectToMap(apply);
- map.put("data", data);
- map.put("type", "notice");
- System.out.println(data);
- try {
- this.sendMsg(ex, routerKey, map);
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- log.error("转人工申请已通过通知,mq发送失败", e);
- }
- }
- }
|