RabbitMQ的七种工作模式-路由模式(四)

4.路由模式
在这里插入图片描述

路由模式与发布订阅模式的区别:

1.路由模式的routeKey是需要定义的

2.Exchange交换机的类型是有区别的,可以在枚举类BuiltinExchangeType中找到

3.一个是广播方式,一个是定向传送

另外:
1.接收者要先于发送者启动,否则接受不到消息(这个我还不知道为什么,待后面了解了再记录)
2.如果写了多个交换机,名称记得不要重复,否则报错

Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method

生产者:

    import com.rabbitmq.client.BuiltinExchangeType;
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    
    /**
     * @ClassName : Publisher
     * @Description : 生产者
     * @Author : Becolette
     * @Date: 2020/9/27  23:08
     */
    
    public class RoutingSent {
    
        private static final String EXCHANGE_NAME = "direct_exchange";
    
        public static void main(String[] argv) throws Exception {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = factory.newConnection();
    
            Channel channel = connection.createChannel();
    
            /**
             * @param exchange the name of the exchange
             *                 交换机名称
             * @param type the exchange type
             *             交换机类型
             */
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
    
            String message = argv.length < 1 ? "info: Hello World!" : String.join(" ", argv);
    
            /**
             * @param exchange the exchange to publish the message to
             *                 交换机
             * @param routingKey the routing key
             *                   连接键
             * @param props other properties for the message - routing headers etc
             *              消息其他属性
             * @param body the message body
             *             消息体
             */
            channel.basicPublish(EXCHANGE_NAME, "routing", null, message.getBytes("UTF-8"));
            System.out.println(" [x] Sent '" + message + "'");
        }
    
    }

消费者:

    import com.rabbitmq.client.*;
    
    /**
     * @ClassName : Subscriber
     * @Description : 消费者
     * @Author : Becolette
     * @Date: 2020/9/27  23:09
     */
    
    public class RoutingRecv {
    
        private static final String EXCHANGE_NAME = "direct_exchange";
    
        public static void main(String[] argv) throws Exception {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = factory.newConnection();
    
            Channel channel = connection.createChannel();
    
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
            /**
             * 由于交换机采用的是群发,也就是广播的形式,所以自动生成queue
             * 并声明binding
             */
            String queueName = channel.queueDeclare().getQueue();
            channel.queueBind(queueName, EXCHANGE_NAME, "routing");
    
            System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    
            DeliverCallback deliverCallback = (consumerTag, delivery) -> {
                String message = new String(delivery.getBody(), "UTF-8");
                System.out.println(" [x] Received '" + message + "'");
            };
            channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
            });
        }
    
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值