监听队列
@Component@RabbitListener(queues = "topic.message")public class TopicReceiver1 { @RabbitHandler
public void process(String message) {
System.out.println("接收者 TopicReceiver1," + message);
}
} @Component@RabbitListener(queues = "topic.message.s")public class TopicReceiver2 { @RabbitHandler
public void process(String message) {
System.out.println("接收者 TopicReceiver2," + message);
}
} @Component@RabbitListener(queues = "topic.ymq")public class TopicReceiver3 { @RabbitHandler
public void process(String message) {
System.out.println("接收者 TopicReceiver3," + message);
}
} 发送消息
package io.ymq.rabbitmq.test;import io.ymq.rabbitmq.run.Startup;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.amqp.core.AmqpTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;/**
* 描述: 配置转发消息模式队列
*
* @author: yanpenglei
* @create: 2017/10/25 1:20
*/@RunWith(SpringRunner.class)@SpringBootTest(classes = Startup.class)public class RabbitTopicTest { @Autowired
private AmqpTemplate rabbitTemplate; @Test
public void sendMessageTest() {
String context = "此消息在,配置转发消息模式队列下, 有 TopicReceiver1 TopicReceiver2 TopicReceiver3 可以收到";
String routeKey = "topic.message";
String exchange = "topicExchange";
context = "context:" + exchange + ",routeKey:" + routeKey + ",context:" + context;
System.out.println("sendMessageTest : " + context); this.rabbitTemplate.convertAndSend(exchange, routeKey, context);
} @Test
public void sendMessagesTest() {
String context = "此消息在,配置转发消息模式队列下,有 TopicReceiver2 TopicReceiver3 可以收到";
String routeKey = "topic.message.s";
String exchange = "topicExchange";
context = "context:" + exchange + ",routeKey:" + routeKey + ",context:" + context;
System.out.println("sendMessagesTest : " + context); this.rabbitTemplate.convertAndSend(exchange, routeKey, context);
} @Test
public void sendYmqTest() {
String context = "此消息在,配置转发消息模式队列下,有 TopicReceiver3 可以收到";
String routeKey = "topic.ymq";
String exchange = "topicExchange";
context = "context:" + exchange + ",routeKey:" + routeKey + ",context:" + context;
System.out.println("sendYmqTest : " + context); this.rabbitTemplate.convertAndSend(exchange, routeKey, context);
}
} 按顺序执行:响应
接收者 TopicReceiver2,context:topicExchange,routeKey:topic.message,context:此消息在,配置转发消息模式队列下, 有 TopicReceiver1 TopicReceiver2 TopicReceiver3 可以收到
接收者 TopicReceiver1,context:topicExchange,routeKey:topic.message,context:此消息在,配置转发消息模式队列下, 有 TopicReceiver1 TopicReceiver2 TopicReceiver3 可以收到
接收者 TopicReceiver3,context:topicExchange,routeKey:topic.message,context:此消息在,配置转发消息模式队列下, 有 TopicReceiver1 TopicReceiver2 TopicReceiver3 可以收到
接收者 TopicReceiver3,context:topicExchange,routeKey:topic.message.s,context:此消息在,配置转发消息模式队列下,有 TopicReceiver2 TopicReceiver3 可以收到
接收者 TopicReceiver2,context:topicExchange,routeKey:topic.message.s,context:此消息在,配置转发消息模式队列下,有 TopicReceiver2 TopicReceiver3 可以收到
接收者 TopicReceiver3,context:topicExchange,routeKey:topic.ymq,context:此消息在,配置转发消息模式队列下,有 TopicReceiver3 可以收到
代码我已放到 Github ,导入spring-boot-rabbitmq 项目
github https://github.com/souyunku/spring-boot-examples/tree/master/spring-boot-rabbitmq
Contact