栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Rabbitmq 主题模型

Rabbitmq 主题模型

topic模型
动态订阅模型,可以使用通配符进行绑定
这种模型的RoutingKey一般是由一个或多个单词组成,多个单词之间用"."分割,例如 item.insert
通配符:
- * 匹配不多不少恰好一个词
- # 匹配一个或多个词(采用hash策略)
生产者
package com.huixiang.rabbitmq.topics;

import com.huixiang.utils.RabbitmqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

// 主题模型(topic quene)   生产者    动态路由
public class Productor {

    public static void main(String[] args) {
        Connection connection = null;
        Channel channel = null;
        try {
            // 获取连接
            connection = RabbitmqUtils.setConnection();
            // 获取通道
            channel = connection.createChannel();
            // 指定key名
            String keyName = "logs.topic.save";
            
            channel.exchangeDeclare("logs_topic","topic");
            //要多条。所以用循环
            for (int i =0;i<10;i++){
                //生产消息
                channel.basicPublish("logs_topic",keyName,null,"nihao".getBytes());
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            RabbitmqUtils.closeConnectionChanel(channel,connection);
        }
    }
}

消费者1
package com.huixiang.rabbitmq.topics;

import com.huixiang.utils.RabbitmqUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer1 {
    public static void main(String[] args) {
        Connection connection = null;
        Channel channel = null;
        try {
            connection = RabbitmqUtils.setConnection();
            channel = connection.createChannel();
            // 创建临时对列
            String queue = channel.queueDeclare().getQueue();
            // 指定key名
            String keyName1 = "logs.#";
            
            channel.exchangeDeclare("logs_topic","topic");
            // 交换机绑定对列(临时对列)
            channel.queueBind(queue,"logs_topic",keyName1);
            
            channel.basicConsume(queue, true, new DeliverCallback() {
                @Override
                public void handle(String consumerTag, Delivery message) throws IOException {
                    System.out.println("消费者1:收到消息" + new String(message.getBody(), "UTF-8"));
                }
            }, new CancelCallback() {
                @Override
                public void handle(String consumerTag) throws IOException {
                    System.out.println("失败");
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

消费者2
package com.huixiang.rabbitmq.topics;

import com.huixiang.utils.RabbitmqUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer2 {
    public static void main(String[] args) {
        Connection connection = null;
        Channel channel = null;
        try {
            connection = RabbitmqUtils.setConnection();
            channel = connection.createChannel();
            // 创建临时对列
            String queue = channel.queueDeclare().getQueue();
            // 指定key名
            String keyName1 = "logs.topic.*";
            
            channel.exchangeDeclare("logs_topic","topic");
            // 交换机绑定对列(临时对列)
            channel.queueBind(queue,"logs_topic",keyName1);
            
            channel.basicConsume(queue, true, new DeliverCallback() {
                @Override
                public void handle(String consumerTag, Delivery message) throws IOException {
                    System.out.println("消费者1:收到消息" + new String(message.getBody(), "UTF-8"));
                }
            }, new CancelCallback() {
                @Override
                public void handle(String consumerTag) throws IOException {
                    System.out.println("失败");
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/439146.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号