MqController.java 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package com.xunmei.api.controller;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.xunmei.api.mq.producer.RabbitMqProducer;
  4. import com.xunmei.common.core.domain.R;
  5. import lombok.AllArgsConstructor;
  6. import lombok.Data;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.web.bind.annotation.*;
  9. import java.time.LocalDateTime;
  10. /**
  11. * @Description: RabbitmqController
  12. */
  13. @Slf4j
  14. @RestController
  15. @AllArgsConstructor
  16. @RequestMapping("/mq")
  17. public class MqController {
  18. //消息发送者
  19. private final RabbitMqProducer rabbitMqProducer;
  20. /**
  21. * 发送普通消息Rabbitmq
  22. * bindingName 绑定队列名称
  23. * @param msg 消息内容
  24. */
  25. @GetMapping("/sendMessage/{msg}/{bindingName}")
  26. public R<Void> sendMessage(@PathVariable("msg") String msg, @PathVariable("bindingName") String bindingName) {
  27. log.info(bindingName + "发送消息: " + msg);
  28. rabbitMqProducer.sendMsg(msg, bindingName);
  29. return R.ok();
  30. }
  31. /**
  32. * 发送延迟消息
  33. *
  34. * @param message 消息实体
  35. * @return
  36. */
  37. @PostMapping("/sendDelayedMessage")
  38. public R<Void> sendDelayedMessage(@RequestBody Message message) {
  39. log.info("发送延时消息: " + LocalDateTime.now() + " " + message);
  40. rabbitMqProducer.sendDelayMsg(JSON.toJSONString(message), message.getBindingName(), message.getSeconds());// 延迟时间(秒)
  41. return R.ok();
  42. }
  43. }
  44. @Data
  45. class Message{
  46. private String bindingName;
  47. private Integer seconds;
  48. }