rabbitMQ的Topic模式的生产者与消费者使用案例

topic模式 RoutingKey 按照英文单词点号多拼接规则填充。其中消费者匹配规则时候 * 代表一个单词,#表示多个单词
消费者C1的RoutingKey 规则按照*.orange.* 匹配 绑定队列Q1
java 复制代码
package com.esint.rabbitmq.work05;

import com.esint.rabbitmq.RabbitMQUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

public class ReceiveLogsTopic01 {

    public static final String EXCHANGE_NAME = "topic";
    public static void main(String[] args) throws Exception {

        Channel channel = RabbitMQUtils.getChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);

        String queueName = "Q1";

        channel.queueDeclare(queueName,false,false,false,null);

        channel.queueBind(queueName,EXCHANGE_NAME,"*.orange.*");
        System.out.println("Q1队列等待接受消息...");

        DeliverCallback deliverCallback = (deliverTag,mes)->{
            System.out.println("打印ReceiveLogsTopic01控制台消息:" + new String(mes.getBody(),"UTF-8"));
            System.out.println("接收队列:" + queueName + " 绑定键:" + mes.getEnvelope().getRoutingKey());
        };
        channel.basicConsume(queueName,true,deliverCallback,deliverTag->{});
    }

}
消费者C2的RoutingKey 规则按照*.rabbit.* 和 lazy.# 匹配 绑定队列Q2
java 复制代码
package com.esint.rabbitmq.work05;

import com.esint.rabbitmq.RabbitMQUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

public class ReceiveLogsTopic02 {

    public static final String EXCHANGE_NAME = "topic";
    public static void main(String[] args) throws Exception {

        Channel channel = RabbitMQUtils.getChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);

        String queueName = "Q2";

        channel.queueDeclare(queueName,false,false,false,null);

        channel.queueBind(queueName,EXCHANGE_NAME,"*.*.rabbit");
        channel.queueBind(queueName,EXCHANGE_NAME,"lazy.#");
        System.out.println("Q2队列等待接受消息...");

        DeliverCallback deliverCallback = (deliverTag,mes)->{
            System.out.println("打印ReceiveLogsTopic01控制台消息:" + new String(mes.getBody(),"UTF-8"));
            System.out.println("接收队列:" + queueName + " 绑定键:" + mes.getEnvelope().getRoutingKey());
        };
        channel.basicConsume(queueName,true,deliverCallback,deliverTag->{});
    }

}
生产者顺序发送
log 复制代码
        bindKeyMap.put("quick.orange.rabbit","Q1 Q2 catch【1】");
        bindKeyMap.put("lazy.orange.elep","Q1 Q2 【2】");
        bindKeyMap.put("quick.orange.fox","Q1【3】");
        bindKeyMap.put("lazy.brown.fox","Q2【4】");
        bindKeyMap.put("lazy.pink.rabbit","Q2 Q2【5】");
        bindKeyMap.put("quick.brown.fox","wu【6】");
        bindKeyMap.put("quick.orange.male.rabbit","wu【7】");
        bindKeyMap.put("lazy.orange.male.rabbit","Q2【8】");
java 复制代码
package com.esint.rabbitmq.work05;

import com.esint.rabbitmq.RabbitMQUtils;
import com.rabbitmq.client.Channel;

import java.util.HashMap;
import java.util.Map;

public class ProductLogs {

    public static final String EXCHANGE_NAME = "topic";

    public static void main(String[] args) throws Exception {

        Channel channel = RabbitMQUtils.getChannel();

        HashMap<String,String > bindKeyMap = new HashMap<>();
        bindKeyMap.put("quick.orange.rabbit","Q1 Q2 catch【1】");
        bindKeyMap.put("lazy.orange.elep","Q1 Q2 【2】");
        bindKeyMap.put("quick.orange.fox","Q1【3】");
        bindKeyMap.put("lazy.brown.fox","Q2【4】");
        bindKeyMap.put("lazy.pink.rabbit","Q2 Q2【5】");
        bindKeyMap.put("quick.brown.fox","wu【6】");
        bindKeyMap.put("quick.orange.male.rabbit","wu【7】");
        bindKeyMap.put("lazy.orange.male.rabbit","Q2【8】");

        for (Map.Entry<String, String> bindingKeyEntry : bindKeyMap.entrySet()) {
            String key = bindingKeyEntry.getKey();
            String value = bindingKeyEntry.getValue();
            channel.basicPublish(EXCHANGE_NAME,key,null,value.getBytes("UTF-8"));
            System.out.println("生产者发消息:"+ value);
        }
    }
}
运行消费者1 消费者2,执行生产者:

生产者:

消费者1:

消费者2:

相关推荐
P.H. Infinity5 小时前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
不能再留遗憾了10 小时前
RabbitMQ 高级特性——消息分发
分布式·rabbitmq·ruby
许苑向上14 小时前
【零基础小白】 window环境下安装RabbitMQ
rabbitmq
P.H. Infinity1 天前
【RabbitMQ】03-交换机
分布式·rabbitmq
孤蓬&听雨1 天前
RabbitMQ自动发送消息工具(自动化测试RabbitMQ)
分布式·测试工具·自动化·rabbitmq·自动发送消息
呼啦啦啦啦啦啦啦啦1 天前
RabbitMQ 七种工作模式介绍
分布式·rabbitmq
qq_203769491 天前
win11安装最新rabbitmq
分布式·rabbitmq·ruby
Amagi.1 天前
RabbitMQ的解耦、异步、削峰是什么?
分布式·rabbitmq
小码哥呀2 天前
RabbitMQ设置消息过期时间
分布式·rabbitmq
斑驳竹影2 天前
rabbitMQ RabbitTemplate 发送消息
java·rabbitmq·java-rabbitmq