|
@@ -0,0 +1,52 @@
|
|
|
+package com.sf;
|
|
|
+
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+
|
|
|
+@SpringBootTest
|
|
|
+public class RabbitMqTests {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RabbitTemplate rabbitTemplate;
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test() {
|
|
|
+ String queueName = "myQueue";
|
|
|
+ String message = "hello spring amqp ";
|
|
|
+ // 通过队列发送消息
|
|
|
+ for (int i = 0; i < 10; i++) {
|
|
|
+ rabbitTemplate.convertAndSend(queueName, message + i);
|
|
|
+ System.out.println(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testFanout() {
|
|
|
+ String exchangeName = "lovecoding.fanout";
|
|
|
+ String message = "hello fanout";
|
|
|
+ rabbitTemplate.convertAndSend(exchangeName,"",message);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testDirect() {
|
|
|
+ String exchangeName = "lovecoding.direct";
|
|
|
+ String message = "hello direct";
|
|
|
+ rabbitTemplate.convertAndSend(exchangeName,"222",message);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testTopic() {
|
|
|
+ String exchangeName = "lovecoding.topic";
|
|
|
+ String message = "hello topic";
|
|
|
+ rabbitTemplate.convertAndSend(exchangeName,"nangang.harbin",message);
|
|
|
+
|
|
|
+ String exchangeName1 = "lovecoding.topic1";
|
|
|
+ String message1 = "hello topic";
|
|
|
+ rabbitTemplate.convertAndSend(exchangeName1,"xuefu.nangang.harbin",message1);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|