RabbitMQ: topic 结构

生产者

java 复制代码
package com.qf.mq2302.topic;

import com.qf.mq2302.utils.MQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class Pubisher {
    public static final String EXCHANGE_NAME="mypubilisher";

    public static void main(String[] args) throws Exception {
        Connection connection = MQUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_NAME,"topic");

        String msg="好好学习";
        String routingkey="lazy.orange.rabbit";

        channel.basicPublish(EXCHANGE_NAME,routingkey,null,msg.getBytes("utf-8"));

        channel.close();
        connection.close();

    }
}

消费者1

java 复制代码
package com.qf.mq2302.topic;

import com.qf.mq2302.utils.MQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;

import java.io.IOException;

public class MyConsumer01 {
    public static final String EXCHANGE_NAME="mypubilisher";

    public static void main(String[] args) throws Exception {
        Connection connection = MQUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_NAME,"topic");
        String queue = channel.queueDeclare().getQueue();

        channel.basicQos(1);

        //绑定队列和交换机
        String routingkey="*.orange.*";
        channel.queueBind(queue,EXCHANGE_NAME,routingkey);

        channel.basicConsume(queue, false, new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] body = message.getBody();
                String s = new String(body, "utf-8");
                System.out.println(s);

                channel.basicAck(message.getEnvelope().getDeliveryTag(),false);


            }
        },consumerTag -> {});




    }

}

消费者2

java 复制代码
package com.qf.mq2302.topic;

import com.qf.mq2302.utils.MQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;

import java.io.IOException;

public class MyConsumer02 {
    public static final String EXCHANGE_NAME="mypubilisher";

    public static void main(String[] args) throws Exception {
        Connection connection = MQUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_NAME,"topic");
        String queue = channel.queueDeclare().getQueue();

        channel.basicQos(1);

        //绑定队列和交换机

        String routingkey2="*.*.rabbit";
        String routingkey3="lazy.#";
        channel.queueBind(queue,EXCHANGE_NAME,routingkey3);
        channel.queueBind(queue,EXCHANGE_NAME,routingkey2);

        channel.basicConsume(queue, false, new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] body = message.getBody();
                String s = new String(body, "utf-8");
                System.out.println(s);

                channel.basicAck(message.getEnvelope().getDeliveryTag(),false);


            }
        },consumerTag -> {});




    }

}
相关推荐
Frank_refuel4 小时前
分布式RPC框架实战(二):服务端模块划分与底层设计
分布式·网络协议·rpc
mftang1 天前
EtherCAT协议:从“飞读飞写”机制到分布式时钟同步的实时以太网架构深度解析
分布式·架构·ethercat·分布式时钟·从站控制器
ShineWinsu1 天前
对于Redis:Hash类型的解析
c++·redis·分布式·缓存·面试·hash·哈希表
东方芷兰1 天前
Agent 技术摘要 03 —— 基座模型、推理模型、Flash、联邦学习、分布式机器学习
人工智能·分布式·机器学习
程序猿乐锅1 天前
【黑马点评 | 第十一篇】关注 Feed 流实现
java·数据库·redis·分布式·后端·缓存·maven
j7~1 天前
【C++标准项目】C++基于protobuf的发布订阅式消息队列(篇一):项目概述+环境搭建
分布式·消息队列·rabbitmq·gtest·protobuf·muduo·sqlites
今年下半年2 天前
【Spring Boot】多种存储文件(FastDFS / MinIO / 阿里云 OSS)接入设计说明(附源码)
spring boot·分布式·中间件·简单工厂模式·策略模式
ShineWinsu2 天前
对于Redis:string类型的解析
java·c++·redis·分布式·缓存·面试·string
仍然.2 天前
RabbitMQ---可靠性传输
网络·分布式·rabbitmq
灯澜忆梦2 天前
【RabbitMQ #13】 | 延迟消息
分布式·rabbitmq·ruby