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




    }

}
相关推荐
半桶水专家1 小时前
Kafka 4.0.1 KRaft 模式完整部署指南
分布式·kafka·linq
huohuopro5 小时前
HBase 伪分布式环境安装指南
数据库·分布式·hbase
一只大袋鼠6 小时前
高并发系统架构优化(下):突破带宽瓶颈,迈向分布式集群
分布式·系统架构
路小雨~6 小时前
RabbitMQ 全面学习资料
分布式·学习·rabbitmq
heimeiyingwang6 小时前
【架构实战】分布式事务解决方案
分布式·架构
2401_840192276 小时前
监控的作用
分布式·kubernetes
hf2000127 小时前
零成本迁移,原地加速,成本降低60%:火花思维基于云器Lakehouse升级实践
大数据·分布式·spark·lakehouse
拦路雨g7 小时前
Duboo配置zookeeper账号密码认证链接
分布式·zookeeper·云原生
chaofan9807 小时前
拒绝单体模型依赖:从 GPT-5.4 与 Claude 生产力之争看分布式 AI 网关的必要性
人工智能·分布式·gpt
爱吃苹果的梨叔8 小时前
清虹分布式坐席管理系统落地江西省送变电实业有限公司坐席调度系统
分布式