RabbitMQ: Routing结构

生产者

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

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

public class EmitLog {
    public static final String EXCHANGE_NAME="emitlogs";


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

        //创建一个路由模式的交换机,默认创出来,不持久化,不自动删除,不是内部交换机
        channel.exchangeDeclare(EXCHANGE_NAME,"direct");

        String msg="hello routing!!";

        //准备routingKey
        String routingKey="info";

        //发送消息
        channel.basicPublish(EXCHANGE_NAME,routingKey,null,msg.getBytes("utf-8"));

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



    }








}

消费者1号

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

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 ReceiveError {
    private static final String EXCHANGE_NAME="emitlogs";

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

        channel.exchangeDeclare(EXCHANGE_NAME,"direct");

        //该消费者创建一个自己独占的队列,绑定到指定交换机接收消息。
        String queueName = channel.queueDeclare().getQueue();

        //准备号要绑定时使用的routingkey
        String routingKey = "error";

        //绑定该队列到交换机
        channel.queueBind(queueName,EXCHANGE_NAME,routingKey);

        //设置预留消息队列,也就是,RabbitMQ发过来,我可以存几个。当确认一个就会又发过来一个,
        // 但是这些相当于线程池里的线程,然后每个线程又去开辟一个新的线程去执行,回调方法,
        // 当回调方法确认完事,才会释放当前这个线程,然后去队列里在消费一个过来。
        channel.basicQos(1);

        //autoAck :false不自动确认,需要手动确认,如果手动不确认,就会按照 channel.basicQos(1);的数量,给多少就消费多少,不会再给你发了。
        channel.basicConsume(queueName, false, new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] body = message.getBody();
                String msg = new String(body, "utf-8");
                System.out.println(msg);

                //手动ACK
                channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
            }
        },consumerTag -> {});



    }

}

消费者2号

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

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 ReceiveIOther {
    private static final String EXCHANGE_NAME="emitlogs";

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

        channel.exchangeDeclare(EXCHANGE_NAME,"direct");

        //该消费者创建一个自己独占的队列,绑定到指定交换机接收消息。
        String queueName = channel.queueDeclare().getQueue();

        //准备号要绑定时使用的routingkey
        String routingKey1 = "error";
        String routingKey2 = "info";
        String routingKey3 = "warn";

        //绑定该队列到交换机
        channel.queueBind(queueName,EXCHANGE_NAME,routingKey1);
        channel.queueBind(queueName,EXCHANGE_NAME,routingKey2);
        channel.queueBind(queueName,EXCHANGE_NAME,routingKey3);

        channel.basicQos(1);

        channel.basicConsume(queueName, false, new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] body = message.getBody();
                //获取routingKey
                String routingKey = message.getEnvelope().getRoutingKey();
                String msg = new String(body, "utf-8");
                System.out.println(msg);

                //手动ACK
                channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
            }
        },consumerTag -> {});



    }

}
相关推荐
开开心心_Every6 小时前
界面干净的开源免费电视浏览器
人工智能·科技·智能手机·计算机外设·rabbitmq·语音识别·etcd
ExC1dNtqz6 小时前
Redis 分布式锁进阶第六篇讲解
数据库·redis·分布式
Survivor0017 小时前
分布式事务解决方案Seata源码分析
分布式·系统架构
我登哥MVP7 小时前
SpringCloud Alibaba 核心组件解析:分布式事务(Seata)
java·spring boot·分布式·spring·spring cloud·java-ee·intellij-idea
ACP广源盛139246256737 小时前
GSV6155@ACP#DP 1.4a 重定时器芯片,物理 AI 信号长距传输的稳定保障
大数据·人工智能·分布式·嵌入式硬件·spark
linux修理工1 天前
使用codebuddy学习kafka
分布式·学习·kafka
阿 才1 天前
跟文件系统(busybox)的构建
大数据·hadoop·分布式
老纪1 天前
Redis分布式锁进第九零篇
数据库·redis·分布式
Amy187021118231 天前
分布式光伏防孤岛保护:技术逻辑、标准演进与工程实践全解析
分布式
ACP广源盛139246256731 天前
IX7008 PCIe 交换芯片@ACP#RTX Spark 经济型 8 口扩展芯片(对比 ASM1806)
大数据·人工智能·分布式·嵌入式硬件·gpt·spark·电脑