|
|
@@ -0,0 +1,170 @@
|
|
|
+package shop.alien.config.RabbitMq;
|
|
|
+
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.amqp.core.Message;
|
|
|
+import org.springframework.amqp.core.MessageProperties;
|
|
|
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
+import org.springframework.amqp.support.converter.MessageConverter;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * RabbitMQ 测试 Controller
|
|
|
+ * 用于测试 RabbitMQ 消息发送功能
|
|
|
+ *
|
|
|
+ * @author system
|
|
|
+ * @since 2025-01-XX
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+@RequestMapping("/rabbitmq/test")
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class RabbitMQTestController {
|
|
|
+
|
|
|
+ private final RabbitTemplate rabbitTemplate;
|
|
|
+ private final MessageConverter messageConverter;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试发送订单消息
|
|
|
+ */
|
|
|
+ @PostMapping("/send/order")
|
|
|
+ public Map<String, Object> sendOrderMessage(@RequestParam(required = false, defaultValue = "测试订单消息") String message) {
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 构建消息内容
|
|
|
+ Map<String, Object> orderData = new HashMap<>();
|
|
|
+ orderData.put("orderId", "ORDER-" + System.currentTimeMillis());
|
|
|
+ orderData.put("message", message);
|
|
|
+ orderData.put("timestamp", LocalDateTime.now().toString());
|
|
|
+
|
|
|
+ // 发送消息到订单交换机
|
|
|
+ rabbitTemplate.convertAndSend("order.exchange", "order.routing.key", orderData);
|
|
|
+
|
|
|
+ result.put("success", true);
|
|
|
+ result.put("message", "订单消息发送成功");
|
|
|
+ result.put("data", orderData);
|
|
|
+ log.info("订单消息发送成功: {}", orderData);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ result.put("success", false);
|
|
|
+ result.put("message", "订单消息发送失败: " + e.getMessage());
|
|
|
+ log.error("订单消息发送失败", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试发送用户通知消息
|
|
|
+ */
|
|
|
+ @PostMapping("/send/user-notice")
|
|
|
+ public Map<String, Object> sendUserNoticeMessage(@RequestParam(required = false, defaultValue = "测试用户通知") String message) {
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 构建消息内容
|
|
|
+ Map<String, Object> noticeData = new HashMap<>();
|
|
|
+ noticeData.put("userId", "USER-" + System.currentTimeMillis());
|
|
|
+ noticeData.put("message", message);
|
|
|
+ noticeData.put("timestamp", LocalDateTime.now().toString());
|
|
|
+ noticeData.put("type", "NOTICE");
|
|
|
+
|
|
|
+ // 发送消息到用户通知交换机(使用主题路由)
|
|
|
+ rabbitTemplate.convertAndSend("user.notice.exchange", "user.notice.test", noticeData);
|
|
|
+
|
|
|
+ result.put("success", true);
|
|
|
+ result.put("message", "用户通知消息发送成功");
|
|
|
+ result.put("data", noticeData);
|
|
|
+ log.info("用户通知消息发送成功: {}", noticeData);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ result.put("success", false);
|
|
|
+ result.put("message", "用户通知消息发送失败: " + e.getMessage());
|
|
|
+ log.error("用户通知消息发送失败", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试发送字符串消息
|
|
|
+ */
|
|
|
+ @PostMapping("/send/string")
|
|
|
+ public Map<String, Object> sendStringMessage(@RequestParam String message,
|
|
|
+ @RequestParam(required = false, defaultValue = "order.queue") String queue) {
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 直接发送字符串消息
|
|
|
+ rabbitTemplate.convertAndSend("order.exchange", "order.routing.key", message);
|
|
|
+
|
|
|
+ result.put("success", true);
|
|
|
+ result.put("message", "字符串消息发送成功");
|
|
|
+ result.put("data", message);
|
|
|
+ log.info("字符串消息发送成功: {}", message);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ result.put("success", false);
|
|
|
+ result.put("message", "字符串消息发送失败: " + e.getMessage());
|
|
|
+ log.error("字符串消息发送失败", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试消息确认机制 - 发送一个会失败的消息(路由到不存在的队列)
|
|
|
+ */
|
|
|
+ @PostMapping("/send/test-return")
|
|
|
+ public Map<String, Object> sendTestReturnMessage(@RequestParam(required = false, defaultValue = "测试返回消息") String message) {
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 发送到一个不存在的路由键,触发返回回调
|
|
|
+ rabbitTemplate.convertAndSend("order.exchange", "non.existent.routing.key", message);
|
|
|
+
|
|
|
+ result.put("success", true);
|
|
|
+ result.put("message", "测试返回消息已发送(应该会被退回)");
|
|
|
+ result.put("data", message);
|
|
|
+ log.info("测试返回消息发送成功(应该会被退回): {}", message);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ result.put("success", false);
|
|
|
+ result.put("message", "测试返回消息发送失败: " + e.getMessage());
|
|
|
+ log.error("测试返回消息发送失败", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试连接状态
|
|
|
+ */
|
|
|
+ @GetMapping("/connection/status")
|
|
|
+ public Map<String, Object> checkConnectionStatus() {
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 尝试发送一个测试消息来验证连接
|
|
|
+ String testMessage = "连接测试消息 - " + System.currentTimeMillis();
|
|
|
+ rabbitTemplate.convertAndSend("order.exchange", "order.routing.key", testMessage);
|
|
|
+
|
|
|
+ result.put("success", true);
|
|
|
+ result.put("message", "RabbitMQ 连接正常");
|
|
|
+ result.put("timestamp", LocalDateTime.now().toString());
|
|
|
+ log.info("RabbitMQ 连接状态检查成功");
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ result.put("success", false);
|
|
|
+ result.put("message", "RabbitMQ 连接失败: " + e.getMessage());
|
|
|
+ log.error("RabbitMQ 连接状态检查失败", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+}
|