欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 培训 > RabbitMQ中Direct交换机的用法

RabbitMQ中Direct交换机的用法

2025/2/26 6:53:00 来源:https://blog.csdn.net/qq_45623072/article/details/139958449  浏览:    关键词:RabbitMQ中Direct交换机的用法

前言:比如我们的支付完成之后需要进行修改支付状态还要完成短信通知用户需要同时并发两条指令我们可以使用direct交换机进行指定两个不同的业务去完成这两件事

比如我们现在有direct.queue1/direct.queue2两个消息队列,一个direct交换机

我们创建完成两个队列之后需要将交换机进行绑定到对应的队列,绑定时我们需要将key写进去,因为要根据不同的key找到指定的队列去完成不同的消息业务

使用java操作

消费者监听代码

    //使用direct交换机进行消息发送@RabbitListener(queues = "direct.queue1")public void listenDirectQueue1(String msg) throws InterruptedException {System.out.println("消费者收到DirectQueue11111111的消息:"+msg);Thread.sleep(20);}@RabbitListener(queues = "direct.queue2")public void listenDirectQueue2(String msg) throws InterruptedException {System.out.println("消费者收到DirectQueue2222222222222的消息:"+msg);Thread.sleep(200);}

发送者代码

    //使用Direct交换机进行消息转发@Testvoid testDirect() {String exchangeName = "hmall.direct";String msg = "blue";rabbitTemplate.convertAndSend(exchangeName, "blue",msg);}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词