rabbitMQ的direct模式的生产者与消费者使用案例

消费者C1的RoutingKey 规则按照info warn 两种RoutingKey匹配 绑定队列console

java 复制代码
package com.esint.rabbitmq.work03;


import com.esint.rabbitmq.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * 消费者01的消息接受
 */
public class ReceiveLog01 {

    public static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws Exception {

        //获取信道
        Channel channel = RabbitMQUtils.getChannel();
        //声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");

        /**
         *声明一个队列  队列名字随机
         * 消费者断开与队列的链接后 队列自动删除
         */
        String queue = channel.queueDeclare().getQueue();

        /**
         * 绑定交换机与队列
         * 1.队列名字
         */
        channel.queueBind(queue,EXCHANGE_NAME,"");
        System.out.println("wait print message...");

        DeliverCallback deliverCallback = (var,var2)->{
            System.out.println("01:"+new String(var2.getBody()));
        };
        channel.basicConsume(queue,true,deliverCallback, var->{});

    }
}

消费者C2的RoutingKey 规则按照error RoutingKey匹配 绑定队列disk

java 复制代码
package com.esint.rabbitmq.work03;


import com.esint.rabbitmq.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * 消费者02的消息接受
 */
public class ReceiveLog02 {

    public static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws Exception {

        //获取信道
        Channel channel = RabbitMQUtils.getChannel();
        //声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");

        /**
         *声明一个队列  队列名字随机
         * 消费者断开与队列的链接后 队列自动删除
         */
        String queue = channel.queueDeclare().getQueue();

        /**
         * 绑定交换机与队列
         * 1.队列名字
         */
        channel.queueBind(queue,EXCHANGE_NAME,"");
        System.out.println("wait print message...");

        DeliverCallback deliverCallback = (var,var2)->{
            System.out.println("02:"+new String(var2.getBody(),"UTF-8"));
        };
        channel.basicConsume(queue,true,deliverCallback, var->{});

    }
}

生产者发送 基于下面调试程序改变参数RoutingKey 切换info warn error 可以观察两个生产者的不同数据接收

java 复制代码
package com.esint.rabbitmq.work03;

import com.esint.rabbitmq.RabbitMQUtils;
import com.rabbitmq.client.Channel;

import java.util.Scanner;

public class EmitLog {


    public static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws Exception {

        Channel channel = RabbitMQUtils.getChannel();

        //声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");

        Scanner scanner = new Scanner(System.in);

        while(scanner.hasNext()){
            String mes = scanner.next();
            channel.basicPublish(EXCHANGE_NAME,"",null,mes.getBytes("UTF-8"));
            System.out.println("生产者发出消息:"+mes.toString());
        }
    }
}
相关推荐
小林想被监督学习3 小时前
RabbitMQ 的7种工作模式
分布式·rabbitmq
wy02_10 小时前
Linux下载RabbitMQ,并解决Github拒绝访问443的问题
linux·rabbitmq·github
浩哲Zhe1 天前
RabbitMQ
java·分布式·rabbitmq
Allen Bright1 天前
RabbitMQ中的Topic模式
分布式·rabbitmq
Allen Bright2 天前
Spring Boot 整合 RabbitMQ:手动 ACK 与 QoS 配置详解
spring boot·rabbitmq·java-rabbitmq
一路狂飙的猪2 天前
RabbitMQ的工作模型
分布式·rabbitmq
来一杯龙舌兰2 天前
【RabbitMQ】RabbitMQ保证消息不丢失的N种策略的思想总结
分布式·rabbitmq·ruby·持久化·ack·消息确认
Allen Bright3 天前
Spring Boot 整合 RabbitMQ:从入门到实践
spring boot·rabbitmq·java-rabbitmq
bug_null3 天前
RabbitMQ消息可靠性保证机制7--可靠性分析-rabbitmq_tracing插件
分布式·rabbitmq
kingbal3 天前
RabbitMQ:添加virtualHost
分布式·rabbitmq