rabbitmq05 Topics通配符模式

rabbitmq05 Topics通配符模式,第1张

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;


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. 监听队列
        

        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将消息发送到对应的队列

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5117621.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存