zoukankan      html  css  js  c++  java
  • SpringAMQP 发布订阅-DirectExchange

    Direct Exchange 会将接收到的消息根据规则路由到指定的Queue,因此称为路由模式(routes)。

    • 每一个Queue都与Exchange设置一个BindingKey
    • 发布者发送消息时,指定消息的RoutingKey
    • Exchange将消息路由到BindingKey与消息RoutingKey一致的队列

    DirectExchange的使用

    实现思路如下:

    • 在consumer服务中,编写两个消费者方法,分别监听direct.queue1和direct.queue2
      • 利用@RabbitListener声明Exchange、Queue、BindingKey
    • 在publisher中编写测试方法
      • 向指定Exchange和RoutingKey(RoutingKey就是BindingKey)发送消息

    在consumer服务中,编写两个消费者方法

        @RabbitListener(bindings = @QueueBinding(
                value = @Queue("direct.queue1"),
                exchange = @Exchange("marw.direct"),
                key={"red","blue"}
                ))
        public void listenDirectQueue1(String msg) throws InterruptedException {
            System.out.println("listenDirectQueue1 消费者接收到消息 :【" + msg + "】");
        }
    
        @RabbitListener(bindings = @QueueBinding(
                value = @Queue("direct.queue2"),
                exchange = @Exchange("marw.direct"),
                key={"red","yellow"}
        ))
        public void listenDirectQueue2(String msg) throws InterruptedException {
            System.err.println("listenDirectQueue2 消费者接收到消息 :【" + msg + "】");
        }

    在publisher中编写测试方法

        @Test
        public void testDirectQueue() throws InterruptedException {
            String queueName = "marw.direct";
            String message = "hello, Direct queue message";
            rabbitTemplate.convertAndSend(queueName, "red", message);
        }
  • 相关阅读:
    获得音视频信息
    stf-多设备管理平台搭建
    接口用例设计
    阿里云Ubuntu服务器搭建SVN
    Jenkins首次启动卡在加载页面
    初使Jenkins构建Python项目
    MongoDB使用python脚本写入数据
    python re操作
    使用Python脚本写入MongoDB数据
    chromedriver与chrome版本对应表及下载地址
  • 原文地址:https://www.cnblogs.com/WarBlog/p/15479143.html
Copyright © 2011-2022 走看看