基于注解声明队列和交换机
一,生产两个消费监听者(消费端)
1 @RabbitListener(bindings = @QueueBinding( 2 value = @Queue(name = "direct.queue1"), //队列名称 3 exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT), //交换机名称,类型 4 key = {"red", "blue"} //路由名称 5 )) 6 public void listenDirectQueue1(String msg){ 7 System.out.println("消费者接收到direct.queue1的消息:【" + msg + "】"); 8 } 9 10 @RabbitListener(bindings = @QueueBinding( 11 value = @Queue(name = "direct.queue2"), 12 exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT), 13 key = {"red", "yellow"} 14 )) 15 public void listenDirectQueue2(String msg){ 16 System.out.println("消费者接收到direct.queue2的消息:【" + msg + "】"); 17 }
消息发送,需要指名交换机名称,路由器名称,还有发送的内容(发送端)
1 @Test 2 public void testSendDirectExchange() { 3 // 交换机名称 4 String exchangeName = "itcast.direct"; 5 // 消息 6 String message = "hello Mq!"; 7 // 发送消息 8 rabbitTemplate.convertAndSend(exchangeName, "red", message); 9 }

浙公网安备 33010602011771号