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:

相关推荐
2501_941870563 分钟前
分布式AI训练框架设计与多语言实现实践指南
rabbitmq
q***656927 分钟前
使用 Docker 部署 RabbitMQ 的详细指南
docker·容器·rabbitmq
i***586728 分钟前
【RabbitMQ】超详细Windows系统下RabbitMQ的安装配置
windows·分布式·rabbitmq
2501_9411481534 分钟前
跨语言分布式AI平台建设与实践:Python、Java、C++、Go深度探索
kafka·rabbitmq
JSON_L8 小时前
Fastadmin中使用rabbitmq实现延迟队列
rabbitmq·php·fastadmin
q***69779 小时前
集成RabbitMQ+MQ常用操作
分布式·rabbitmq
2501_9418008812 小时前
高性能区块链架构设计与多语言实现
rabbitmq
n***840714 小时前
Linux安装RabbitMQ
linux·运维·rabbitmq
i***719514 小时前
RabbitMQ 集群部署方案
分布式·rabbitmq·ruby
k***216014 小时前
RabbitMQ 客户端 连接、发送、接收处理消息
分布式·rabbitmq·ruby