rabbitmq05 Topics通配符模式

一、

在这里插入图片描述

” # :匹配一个或多个词
* :匹配不多不少恰好1个词

举例:
item.# :能够匹配 item.insert.abc 或者 item.insert
item.* :只能匹配 item.insert

二、案例需求

一个频道
一个交换机(类型为:topic)
两个队列
两个消费者(一个消费者 路由key为 info.insert,另一个为 infor.# 和 *.delet.#)

三、代码

package com.xiaoxu.topics;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.xiaoxu.ConnectionUtis;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * topic 通配符模式
 */
public class Producter {

    //定义两个消息队列
    static  final  String QUEUE_TOPICS_1 = "queue_topics_1";
    static  final  String QUEUE_TOPICS_2 = "queue_topics_2";
    //defining a exchange
    static final  String EXCHANGE_NAME = "exchange_topics";

    public static void main(String[] args) throws IOException, TimeoutException {
        //1、创建连接工厂
        //2. 创建连接;(抽取一个获取连接的工具类)
        Connection connection = ConnectionUtis.getConnection();
        // 3. 创建频道;
        Channel channel = connection.createChannel();

        //statement exchange
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);

        // 6. 监听队列
        /**
         * 参数1:交换机名称,如果没有指定则使用默认Default Exchage
         * 参数2:路由key,简单模式可以传递队列名称
         * 参数3:消息其它属性
         * 参数4:消息内容
         */

        String message = "我是一个消息。。infor";
        channel.basicPublish(EXCHANGE_NAME, "infor.inset", null, message.getBytes());
        System.out.println(message);

        message = "我是一个消息。。error.delet";
        channel.basicPublish(EXCHANGE_NAME,"error.delet",null,message.getBytes());
        System.out.println(message);

        message = "我是一个消息。。error.delet.insert";
        channel.basicPublish(EXCHANGE_NAME,"error.delet.insert",null,message.getBytes());
        System.out.println(message);
        //7、关闭资源
        channel.close();
        connection.close();
    }
}

package com.xiaoxu.topics;


import com.rabbitmq.client.*;
import com.xiaoxu.ConnectionUtis;
import com.xiaoxu.topics.Producter;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer1 {

    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = ConnectionUtis.getConnection();
        //创建频道
        Channel channel = connection.createChannel();
        //statement exchange
        channel.exchangeDeclare(Producter.EXCHANGE_NAME,BuiltinExchangeType.TOPIC);
        // create queue
        channel.queueDeclare(Producter.QUEUE_TOPICS_1,true,false,false,null);
        //bainding  queue and exchange
        channel.queueBind(Producter.QUEUE_TOPICS_1,Producter.EXCHANGE_NAME,"infor.inset");
        //creat consumer and set information
        DefaultConsumer consumer1 = new DefaultConsumer(channel) {
            @Override
          public void handleDelivery(String consumerTag, Envelope envelope,
                                     AMQP.BasicProperties properties, byte[] body) throws IOException{

                //路由key
                System.out.println("路由key为:" + envelope.getRoutingKey());
                //交换机
                System.out.println("交换机为:" + envelope.getExchange());
                //消息id
                System.out.println("消息id为:" + envelope.getDeliveryTag());
                //收到的消息
                System.out.println("接收到的消息为:" + new String(body, "utf-8"));

                System.out.println("我是消费者一号。。。");
            }
        };

        //listen to the message
        channel.basicConsume(Producter.QUEUE_TOPICS_1,true,consumer1);

    }
}
package com.xiaoxu.topics;


import com.rabbitmq.client.*;
import com.xiaoxu.ConnectionUtis;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer2 {

    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = ConnectionUtis.getConnection();
        //创建频道
        Channel channel = connection.createChannel();
        //statement exchange
        channel.exchangeDeclare(Producter.EXCHANGE_NAME,BuiltinExchangeType.TOPIC);
        // create queue
        channel.queueDeclare(Producter.QUEUE_TOPICS_2,true,false,false,null);
        //bainding  queue and exchange
        channel.queueBind(Producter.QUEUE_TOPICS_2,Producter.EXCHANGE_NAME,"infor.#");
        channel.queueBind(Producter.QUEUE_TOPICS_2,Producter.EXCHANGE_NAME,"*.delet.#");


        //creat consumer and set information
        DefaultConsumer consumer1 = new DefaultConsumer(channel) {
            @Override
          public void handleDelivery(String consumerTag, Envelope envelope,
                                     AMQP.BasicProperties properties, byte[] body) throws IOException{

                //路由key
                System.out.println("路由key为:" + envelope.getRoutingKey());
                //交换机
                System.out.println("交换机为:" + envelope.getExchange());
                //消息id
                System.out.println("消息id为:" + envelope.getDeliveryTag());
                //收到的消息
                System.out.println("接收到的消息为:" + new String(body, "utf-8"));

                System.out.println("我是消费者二号。。。");
            }
        };



        //listen to the message
        channel.basicConsume(Producter.QUEUE_TOPICS_2,true,consumer1);


    }
}

四、测试

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

五、模式总结

RabbitMQ工作模式:
1、简单模式 HelloWorld 一个生产者、一个消费者,不需要设置交换机(使用默认的交换
机)
2、工作队列模式 Work Queue 一个生产者、多个消费者(竞争关系),不需要设置交换机(使用默认的交换机)
3、发布订阅模式 Publish/subscribe 需要设置类型为fanout的交换机,并且交换机和队列进行绑定,当发送消息到
交换机后,交换机会将消息发送到绑定的队列
4、路由模式 Routing 需要设置类型为direct的交换机,交换机和队列进行绑定,并且指定routing key,当发送消息
到交换机后,交换机会根据routing key将消息发送到对应的队列
5、通配符模式 Topic 需要设置类型为topic的交换机,交换机和队列进行绑定,并且指定通配符方式的routing key,
当发送消息到交换机后,交换机会根据routing key将消息发送到对应的队列

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐睡睡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值