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




    }

}
相关推荐
zquwei2 小时前
SpringCloudGateway+Nacos注册与转发Netty+WebSocket
java·网络·分布式·后端·websocket·网络协议·spring
道一云黑板报6 小时前
Flink集群批作业实践:七析BI批作业执行
大数据·分布式·数据分析·flink·kubernetes
飞来又飞去7 小时前
kafka sasl和acl之间的关系
分布式·kafka
MZWeiei9 小时前
Zookeeper的监听机制
分布式·zookeeper
莹雨潇潇9 小时前
Hadoop完全分布式环境部署
大数据·hadoop·分布式
浩哲Zhe9 小时前
RabbitMQ
java·分布式·rabbitmq
明达技术10 小时前
分布式 IO 模块:赋能造纸业,革新高速纸机主传动
分布式
Allen Bright11 小时前
RabbitMQ中的Topic模式
分布式·rabbitmq
李洋-蛟龙腾飞公司12 小时前
HarmonyOS Next 应用元服务开发-分布式数据对象迁移数据权限与基础数据
分布式·华为·harmonyos
rainoway12 小时前
CRDT宝典 - Multi-Value-Register
前端·分布式·算法