网站首页> 文章专栏> RabbitMQ-Java客户端Direct交互机
Java客户端Direct交互机
1,在控制台中创建队列和交换机
direct.queue1
direct.queue2
hamll.direct
2,在交换机里面添加 routing key
3,创建消费者
@Slf4j
@Component
public class SpringRabbitListener {
@RabbitListener(queues = "direct.queue1")
public void listenDirectQueueMessage1(String msg){
System.out.println("消费者1 收到了 direct.queue1:" + msg);
}
@RabbitListener(queues = "direct.queue2")
public void listenDirectQueueMessage2(String msg){
System.out.println("消费者2 收到了 direct.queue2:" + msg);
}
}
4,创建生产者
@Test
void testDirect() {
String exchangeNme = "hamll.direct";
String routingkey = "blue";
String msg = "red 两个消费者都能接收";
rabbitTemplate.convertAndSend(exchangeNme,routingkey,msg);
}
然后启动消费者,在启动生产者,如果发送消息到达消息者时,就证明Direct交互机搭建成功!
2024-05-12 19:10:27 回复