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




    }

}
相关推荐
艾希逐月6 小时前
分布式唯一 ID 生成方案
分布式
Monly216 小时前
RabbitMQ:生产者可靠性(生产者重连、生产者确认)
spring boot·rabbitmq·java-rabbitmq
齐木卡卡西在敲代码9 小时前
kafka的pull的依据
分布式·kafka
lllsure10 小时前
RabbitMQ 基础
分布式·rabbitmq
W|J10 小时前
Rabbit 实战指南-学习笔记
rabbitmq
DN金猿13 小时前
rabbitmq发送的延迟消息时间过长就立即消费了
分布式·rabbitmq
Monly2113 小时前
RabbitMQ:SpringAMQP Topic Exchange(主题交换机)
spring boot·rabbitmq·java-rabbitmq
程序员不迷路16 小时前
Kafka学习
分布式·kafka
北i16 小时前
ZooKeeper 一致性模型解析:线性一致性与顺序一致性的平衡
分布式·zookeeper·云原生
IT技术小密圈17 小时前
图解分布式锁: 5分钟搞懂分布式锁
分布式·后端·面试