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:

相关推荐
Allen Bright1 天前
RabbitMQ中的普通Confirm模式:深入解析与最佳实践
分布式·rabbitmq
S-X-S1 天前
集成RabbitMQ+MQ常用操作
rabbitmq
dzend1 天前
Kafka、RocketMQ、RabbitMQ 对比
kafka·rabbitmq·rocketmq
小林想被监督学习2 天前
RabbitMQ 的7种工作模式
分布式·rabbitmq
wy02_2 天前
Linux下载RabbitMQ,并解决Github拒绝访问443的问题
linux·rabbitmq·github
浩哲Zhe3 天前
RabbitMQ
java·分布式·rabbitmq
Allen Bright3 天前
RabbitMQ中的Topic模式
分布式·rabbitmq
Allen Bright3 天前
Spring Boot 整合 RabbitMQ:手动 ACK 与 QoS 配置详解
spring boot·rabbitmq·java-rabbitmq
一路狂飙的猪3 天前
RabbitMQ的工作模型
分布式·rabbitmq
来一杯龙舌兰4 天前
【RabbitMQ】RabbitMQ保证消息不丢失的N种策略的思想总结
分布式·rabbitmq·ruby·持久化·ack·消息确认