MqService.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.free.mq;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import com.fasterxml.jackson.annotation.JsonInclude.Include;
  5. import java.util.Map;
  6. import java.util.HashMap;
  7. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10. import com.fasterxml.jackson.core.JsonProcessingException;
  11. import com.fasterxml.jackson.databind.ObjectMapper;
  12. import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
  13. import com.free.config.FormatJavaTimeModule;
  14. import com.free.entity.TransferApply;
  15. import com.free.utils.Utils;
  16. @Service
  17. public class MqService {
  18. private static final Logger log = LoggerFactory.getLogger(MqService.class);
  19. @Autowired
  20. RabbitTemplate rabbitTemplate;
  21. /**
  22. * 发送mq消息
  23. *
  24. * @param ex 交换机名
  25. * @param queue 队列名
  26. * @param msg 消息内容
  27. * @throws JsonProcessingException JSON处理异常
  28. */
  29. public void sendMsg(String ex, String queue, Map msg) throws JsonProcessingException {
  30. System.out.println(msg);
  31. String strMsg = null;
  32. ObjectMapper mapper = new ObjectMapper();
  33. // 空值放过
  34. mapper.setSerializationInclusion(Include.NON_NULL);
  35. // LocalDateTime处理
  36. JavaTimeModule jtmf = FormatJavaTimeModule.init();
  37. mapper.registerModule(jtmf);
  38. strMsg = mapper.writeValueAsString(msg);
  39. rabbitTemplate.convertAndSend(ex, queue, strMsg);
  40. }
  41. /**
  42. * 转人工对话mq消息
  43. * @param data ChatRecord数据
  44. */
  45. public void sendChatToApply(Map data) {
  46. Map map = new HashMap<>();
  47. map.put("type", "chat");
  48. map.put("data", data);
  49. String apply_id = (String) data.get("apply_id");
  50. String queue = MqListeners.queueName + "." + apply_id;
  51. try {
  52. this.sendMsg(MqListeners.exName, queue, map);
  53. } catch (JsonProcessingException e) {
  54. e.printStackTrace();
  55. log.error("转人工对话通知mq发送失败", e);
  56. }
  57. }
  58. /**
  59. * 向管理员订阅队列发送消息,通知管理员去处理转人工申请
  60. *
  61. * @param data 转人工申请数据
  62. */
  63. public void sendMsgToAdminForApplyCreate(Map data) {
  64. String ex = MqListeners.adminExName;
  65. String queue = MqListeners.adminQueue;
  66. Map map = new HashMap<>();
  67. map.put("data", data);
  68. map.put("type", "notice");
  69. try {
  70. this.sendMsg(ex, queue, map);
  71. } catch (JsonProcessingException e) {
  72. e.printStackTrace();
  73. log.error("转人工申请通知mq发送失败", e);
  74. }
  75. }
  76. /**
  77. * 转人工已受理通知
  78. *
  79. * @param apply
  80. */
  81. public void sendMsgToApplicant(TransferApply apply) {
  82. String ex = MqListeners.exName;
  83. String routerKey = MqListeners.queueName + "." + apply.getUser_id();
  84. Map map = new HashMap<>();
  85. Map data = Utils.objectToMap(apply);
  86. map.put("data", data);
  87. map.put("type", "notice");
  88. System.out.println(data);
  89. try {
  90. this.sendMsg(ex, routerKey, map);
  91. } catch (JsonProcessingException e) {
  92. e.printStackTrace();
  93. log.error("转人工申请已通过通知,mq发送失败", e);
  94. }
  95. }
  96. }