JAVA操作RabbitMQ-routing路由模式

路由模式(Routing):路由模式跟发布订阅模式类似,然后在发布订阅模式的基础上改变了交换机类型(fanout => direct),订阅模式是分发到所有绑定到交换机的队列,路由模式只分发到绑定在交换机上面指定路由key的队列上。

P:消息的生产者
C:消息的消费者
红色:队列
X:交换机

 1. pom.xml引入rabbitmq依赖

<dependency>
     <groupId>com.rabbitmq</groupId>
     <artifactId>amqp-client</artifactId>
     <version>5.13.1</version>
</dependency>

2. 生产者

public class Producer {

    public static void main(String[] args) {
        // 创建链接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("123456");
        connectionFactory.setVirtualHost("/");
        // 创建Connection

        Connection connection = null;
        Channel channel = null;
        try {
            connection = connectionFactory.newConnection("生产者");
            channel = connection.createChannel();

            String message = "hello message";
            String exchangeName = "direct-exchange";
            String type = "direct";

            channel.exchangeDeclare(exchangeName,type,true);

            //创建队列
            // 队列名称,是否持久化,是否独占,是否自动删除(当最后一条消息消费完毕后自动删除),附加属性
            channel.queueDeclare("direct-queue1", true, false, false, null);
            channel.queueDeclare("direct-queue2", true, false, false, null);
            channel.queueDeclare("direct-queue3", true, false, false, null);

            channel.queueBind("direct-queue1",exchangeName,"sms");
            channel.queueBind("direct-queue2", exchangeName,"wechat");
            channel.queueBind("direct-queue3",exchangeName,"sms");

            // 发送消息到队列
            channel.basicPublish(exchangeName, "sms", null, message.getBytes(StandardCharsets.UTF_8));
            System.out.println("消息发送成功");

        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
        } finally {
            if (channel != null && channel.isOpen()) {
                try {
                    channel.close();
                } catch (IOException | TimeoutException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null && connection.isOpen()) {
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}

3. 消费者

public class Consumer {

    public static class C implements Runnable {

        @Override
        public void run() {
            Thread thread = Thread.currentThread();
            // 创建链接
            ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.setHost("127.0.0.1");
            connectionFactory.setPort(5672);
            connectionFactory.setUsername("admin");
            connectionFactory.setPassword("123456");
            connectionFactory.setVirtualHost("/");
            // 创建Connection

            Connection connection = null;
            Channel channel = null;
            try {
                connection = connectionFactory.newConnection("消费者");
                channel = connection.createChannel();
                String queueName = thread.getName();

                // 监听发送来的消息
                // 队列名称,是否自动确认消息,接收消息的回调,消费者取消时的回调
                channel.basicConsume(queueName, true,
                        (consumerTag, message) -> System.out.println(queueName+":收到消息:" + new String(message.getBody(), StandardCharsets.UTF_8)),
                        consumerTag -> System.out.println("消费者取消订阅"));

                while (true) {
                    //阻塞进程, 防止消费者程序退出
                }
            } catch (IOException | TimeoutException e) {
                e.printStackTrace();
            } finally {
                if (channel != null && channel.isOpen()) {
                    try {
                        channel.close();
                    } catch (IOException | TimeoutException e) {
                        e.printStackTrace();
                    }
                }
                if (connection != null && connection.isOpen()) {
                    try {
                        connection.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }


    public static void main(String[] args) {
        new Thread(new C(),"direct-queue1").start();
        new Thread(new C(),"direct-queue2").start();
        new Thread(new C(),"direct-queue3").start();

    }
}

4. 以上代码运行结果,可以看出只有 direct-queue1和direct-queue3收到消息,所以路由模式,只有路由key匹配的队列才能收到消息,其他的收不到

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值