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 -> {});




    }

}
相关推荐
device_Any12 分钟前
分库分表之后,分布式 ID 怎么生成
分布式·分库分表
skywoodsky1 小时前
rabbitmq联邦模式
rabbitmq
nice_lcj5206 小时前
在 Spring Boot 里学会使用 RabbitMQ:从入门到实战
java·spring boot·spring·rabbitmq·java-rabbitmq
Francek Chen7 小时前
【大数据处理与分析】Spark:03 Spark运行架构
大数据·hadoop·分布式·架构·spark
ps酷教程7 小时前
分布式事务
分布式
我不会起名字3227 小时前
RabbitMQ 落地边界:交换机、延迟队列与死信风暴的 4 个坑
消息队列·rabbitmq·死信队列·延迟队列·后端架构
真上帝的左手7 小时前
12. 消息队列-RabbitMQ-Spring Boot 集成 RabbitMQ/RocketMQ实战——消息可靠投递、幂等消费、顺序保证
分布式·rabbitmq·java-rocketmq·java-rabbitmq·架构设计·应用架构
kyrie_sakura20 小时前
大模型学习32 --大模型微调3 -- 分布式训练算法
分布式·学习·算法
qq_410960351 天前
大模型 Prompt 工程在文件上传与分布式存储中的高级用
分布式·prompt
霸道流氓气质1 天前
分布式序列号生成:基于 Redis 原子自增的编号方案
数据库·redis·分布式