RabbitMQ(六)之Exchange 交换机

一、Exchange

Exchange:接收消息,并根据路由键转发消息所绑定的队列。
在这里插入图片描述
生产者产生消息,把消息投递到交换机上,并且根据路由键把消息绑定到相对应的队列上。消费者从队列里面取得消息进行处理。

二、属性

  1. Name:交换机名称
  2. Type:交换机类型 direct、topic、fanout、headers
  3. Durability:是否持久化 true为持久化
  4. Auto Delect:当最后一个绑定到Exchange上的队列删除后,自动删除Exchange
  5. Internal:当前Exchange是否用于RabbitMQ内部使用,默认false
  6. Arguments:扩展参数,用于扩展AMQP协议自制定义化使用

三、Direct Exchange

所有发送到Direct Exchange的消息被转发到RouteKey 中指定的Queue,转发消息的Direct Exchange 的RouteKey必须和指定的Queue中的一致,否则不能发送到Queue。Direct模式可以使用RabbitMQ自带的Exchange:default Exchange,所以不需要讲Exchange进行任何的BinDing操作,消息传递时,RouteKey必须完全一致才会被队列接受,否则该消息会被抛弃。
在这里插入图片描述
生产者

 public static void main(String[] args) throws IOException, TimeoutException {
        //1、 创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        // RabbitMQ服务IP
        connectionFactory.setHost("127.0.0.1");
        // RabbitMQ服务端口
        connectionFactory.setPort(5672);
        // 选择VirtualHost
        connectionFactory.setVirtualHost("/");
        Connection connection;
        Channel channel;
        try {
            //2、 通过工厂创建连接
            connection = connectionFactory.newConnection();
            //3、通过连接创建Channel
            channel = connection.createChannel();

            //交换机名称
            String exchangeName = "test.direct";
            //路由键
            String routingKey = "te01_direct";
            AMQP.BasicProperties basicProperties = null;
            for (int i = 0; i < 5; i++) {
                String body = "Producer:"+i;
                //发送消息
                channel.basicPublish(exchangeName, routingKey, basicProperties, body.getBytes());
            }

            // 按顺序关闭连接
            channel.close();
            connection.close();


        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

    }

消费者

public static void main(String[] args) {
        //1、 创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        // RabbitMQ服务IP
        connectionFactory.setHost("127.0.0.1");
        // RabbitMQ服务端口
        connectionFactory.setPort(5672);
        // 选择VirtualHost
        connectionFactory.setVirtualHost("/");

        try {
            //2、 通过工厂创建连接
            Connection connection = connectionFactory.newConnection();

            //3、通过连接创建Channel
            Channel channel = connection.createChannel();
            //4、创建队列
            String queueName = "te01_direct";//队列名称

            //交换机类型:direct
            String exchangeType = "direct";
            String exchangeName = "test.direct";
            //路由键
            String routingKey = "te01_direct";

            //声明Exchange
            channel.exchangeDeclare(exchangeName,exchangeType,true,false,false,null);
            //声明队列
            channel.queueDeclare(queueName, true, false, false, null);
            //路由器绑定队列
            channel.queueBind(queueName,exchangeName,routingKey);
            //5、创建消费者
            QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
            //6、配置channel
            channel.basicConsume(queueName, true, queueingConsumer);

            while (true) {
                QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
                String body = new String(delivery.getBody());
                System.out.println("消费者:" + body);
            }


        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

源码下载

四、Topic Exchange

所有发送到Topic Exchange的消息被转发到所有关心的RoutingKey中指定的Topic的Queue上。
Exchange 将RoutingKey和某个Topic进行模糊匹配,此时队列需要绑定一个Topic。
匹配符号:

  1. #:匹配一个或多个词
  2. *:匹配一个词

例:
test.# 可以匹配 test.info、test.info.1、test.info.2
test.* 只可以匹配 test.info、test.error
在这里插入图片描述

生产者

 public static void main(String[] args) throws IOException, TimeoutException {
        //1、 创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        // RabbitMQ服务IP
        connectionFactory.setHost("127.0.0.1");
        // RabbitMQ服务端口
        connectionFactory.setPort(5672);
        // 选择VirtualHost
        connectionFactory.setVirtualHost("/");
        Connection connection;
        Channel channel;
        try {
            //2、 通过工厂创建连接
            connection = connectionFactory.newConnection();
            //3、通过连接创建Channel
            channel = connection.createChannel();

            //交换机名称
            String exchangeName = "test.topic";
            //路由键
            String routingKey1 = "te01.topic.01";
            String routingKey2 = "te01.topic.02";
            String routingKey3 = "te01.topic.03";
            String routingKey4 = "te01.topic.03.04";
            AMQP.BasicProperties basicProperties = null;

                String body = "Producer:";
                //发送消息
                channel.basicPublish(exchangeName, routingKey1, basicProperties, routingKey1.getBytes());
                channel.basicPublish(exchangeName, routingKey2, basicProperties, routingKey2.getBytes());
                channel.basicPublish(exchangeName, routingKey3, basicProperties, routingKey3.getBytes());
                channel.basicPublish(exchangeName, routingKey4, basicProperties, routingKey4.getBytes());


            // 按顺序关闭连接
            channel.close();
            connection.close();


        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

    }

消费者

 public static void main(String[] args) {
        //1、 创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        // RabbitMQ服务IP
        connectionFactory.setHost("127.0.0.1");
        // RabbitMQ服务端口
        connectionFactory.setPort(5672);
        // 选择VirtualHost
        connectionFactory.setVirtualHost("/");

        try {
            //2、 通过工厂创建连接
            Connection connection = connectionFactory.newConnection();

            //3、通过连接创建Channel
            Channel channel = connection.createChannel();
            //4、创建队列
            String queueName = "te01_topic";//队列名称

            //交换机类型:direct
            String exchangeType = "topic";

            String exchangeName = "test.topic";
            //路由键
            String routingKey = "te01.topic.*";

            //声明Exchange
            channel.exchangeDeclare(exchangeName,exchangeType,true,false,false,null);
            //声明队列
            channel.queueDeclare(queueName, true, false, false, null);
            //路由器绑定队列
            channel.queueBind(queueName,exchangeName,routingKey);
            //5、创建消费者
            QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
            //6、配置channel
            channel.basicConsume(queueName, true, queueingConsumer);

            while (true) {
                QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
                String body = new String(delivery.getBody());
                System.out.println("消费者:" + body);
            }


        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
       
    }

源码下载

五、Fanout Exchange

不处理任何路由键,直接将队列绑定到交换机上。发送到交换机的消息都会被绑定到与该交换机绑定的队列上。

生产者

public static void main(String[] args) throws IOException, TimeoutException {
        //1、 创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        // RabbitMQ服务IP
        connectionFactory.setHost("127.0.0.1");
        // RabbitMQ服务端口
        connectionFactory.setPort(5672);
        // 选择VirtualHost
        connectionFactory.setVirtualHost("/");
        Connection connection;
        Channel channel;
        try {
            //2、 通过工厂创建连接
            connection = connectionFactory.newConnection();
            //3、通过连接创建Channel
            channel = connection.createChannel();

            //交换机名称
            String exchangeName = "test.fanout";
            //路由键
            String routingKey = "";
            AMQP.BasicProperties basicProperties = null;
            for (int i = 0; i < 5; i++) {
                String body = "Producer:"+i;
                //发送消息
                channel.basicPublish(exchangeName, routingKey, basicProperties, body.getBytes());
            }

            // 按顺序关闭连接
            channel.close();
            connection.close();


        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

    }

消费者

public static void main(String[] args) {
        //1、 创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        // RabbitMQ服务IP
        connectionFactory.setHost("127.0.0.1");
        // RabbitMQ服务端口
        connectionFactory.setPort(5672);
        // 选择VirtualHost
        connectionFactory.setVirtualHost("/");

        try {
            //2、 通过工厂创建连接
            Connection connection = connectionFactory.newConnection();

            //3、通过连接创建Channel
            Channel channel = connection.createChannel();
            //4、创建队列
            String queueName = "te01_fanout";//队列名称

            //交换机类型:direct
            String exchangeType = "fanout";
            String exchangeName = "test.fanout";
            //路由键
            String routingKey = "";

            //声明Exchange
            channel.exchangeDeclare(exchangeName,exchangeType,true,false,false,null);
            //声明队列
            channel.queueDeclare(queueName, true, false, false, null);
            //路由器绑定队列
            channel.queueBind(queueName,exchangeName,routingKey);
            //5、创建消费者
            QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
            //6、配置channel
            channel.basicConsume(queueName, true, queueingConsumer);

            while (true) {
                QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
                String body = new String(delivery.getBody());
                System.out.println("消费者:" + body);
            }


        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

源码下载

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值