Topics(主题模式)
引言
topic exchange和direct exchange类似,都是通过routing key和binding key进行匹配,不同的是topic exchange可以为routing key设置多重标准。
direct路由器类似于sql语句中的精确查询;topic 路由器有点类似于sql语句中的模糊查询。
topic 使用通配符“*”和“#”进行routingkey的模糊匹配:
*:精确匹配一个; #:任意匹配多个

1.模型

2.创建生产者
package com.dwz.rabbitmq.exchange.topic; import java.io.IOException;
import java.util.concurrent.TimeoutException; import com.dwz.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection; public class Producer {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel(); String exchangeName = "test_topic_exchange";
String routingKey_1 = "user.save";
String routingKey_2 = "user.update";
String routingKey_3 = "user.delete.abc"; String msg = "hello rabbitmq topic message successs!--";
channel.basicPublish(exchangeName, routingKey_1, null, (msg + routingKey_1).getBytes());
channel.basicPublish(exchangeName, routingKey_2, null, (msg + routingKey_2).getBytes());
channel.basicPublish(exchangeName, routingKey_3, null, (msg + routingKey_3).getBytes()); channel.close();
connection.close();
}
}
3.创建消费者1
package com.dwz.rabbitmq.exchange.topic; import java.io.IOException;
import java.util.concurrent.TimeoutException; import com.dwz.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
/**
* topic:模糊匹配
* @author dangwangzhen
*
*/
public class Consumer {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel(); String exchangeName = "test_topic_exchange";
String exchangeType = "topic";
String queueName = "test_topic_queue_1";
String routingKey = "user.#";
channel.exchangeDeclare(exchangeName, exchangeType, true, false, null);
channel.queueDeclare(queueName, false, false, false, null);
channel.queueBind(queueName, exchangeName, routingKey); DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String msg = new String(body, "utf-8");
System.out.println("rec topic 1--message:" + msg);
}
};
channel.basicConsume(queueName, true, consumer);
}
}
4.创建消费者2
package com.dwz.rabbitmq.exchange.topic; import java.io.IOException;
import java.util.concurrent.TimeoutException; import com.dwz.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
/**
* topic:模糊匹配
* @author dangwangzhen
*
*/
public class Consumer2 {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel(); String exchangeName = "test_topic_exchange";
String exchangeType = "topic";
String queueName = "test_topic_queue_2";
String routingKey = "user.*.*";
channel.exchangeDeclare(exchangeName, exchangeType, true, false, null);
channel.queueDeclare(queueName, false, false, false, null);
channel.queueBind(queueName, exchangeName, routingKey); DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String msg = new String(body, "utf-8");
System.out.println("rec topic2--message:" + msg);
}
};
channel.basicConsume(queueName, true, consumer);
}
}
5.运行代码
success!
Topics(主题模式)的更多相关文章
- RabbitMQ六种队列模式-主题模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主题模式 [ ...
- ActiveMQ队列、主题模式区别
1.ActiveMQ队列模式如下图,生产者创建消息到消息中间件,再“均分给消费者”. 2.ActiveMQ主题模式如下图,生产者创建消息到消息中间件,消费者会接受到订阅的主题中所有的消息.在主题模式下 ...
- RabbitMQ消息队列(八)-通过Topic主题模式分发消息(.Net Core版)
前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过routingkey来匹配消息的模式已经有一定了解那fanout也很好 ...
- 队列模式&主题模式
# RabbitMQ 消息中间件 **Advanced Message Queuing Protocol (高级消息队列协议** The Advanced Message Queuing Protoc ...
- RabbitMQ (七) 订阅者模式之主题模式 ( topic )
主题模式和路由模式很像 路由模式是精确匹配 主题模式是模糊匹配 依然先通过管理后台添加一个交换机. 生产者 public class Producer { private const string E ...
- activeMQ队列模式和主题模式的Java实现
一.队列模式 生产者 import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destina ...
- (八)RabbitMQ消息队列-通过Topic主题模式分发消息
原文:(八)RabbitMQ消息队列-通过Topic主题模式分发消息 前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过 ...
- ActiveMQ--模式(队列模式/主题模式)
两种模式:队列模式/主题模式 pom.xml <dependency> <groupId>org.apache.activemq</groupId> <art ...
- RabbitMQ主题模式
Send类 package topics; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; imp ...
随机推荐
- 六一广告页H5全屏滚动效果实现
明天就六一儿童了(放假了),在这里提前祝大家周末快乐,每逢节假日公司必然会推出h5活动页的需求,这次六一儿童节也不例外,产品这次倒是没提什么互动效果需求,只不过根据UI妹子给的设计图,图片与图片中颜色 ...
- mssql 数据库“查询处理器用尽了内部资源,无法生成查询计划。”问题的处理
在项目中动态拼接sql语句,使用union all连接结果集,每个查询语句都使用了in(几百个数值).语句如: ,,,..............................) UNION ALL ...
- 手把手教你搭建FastDFS集群(中)
手把手教你搭建FastDFS集群(中) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/u0 ...
- 【ES6 】声明变量的方式
var function let const import class
- 题解 P2879 【[USACO07JAN]区间统计Tallest Cow】
题目链接: https://www.luogu.org/problemnew/show/P2879 思路: 先不管最大高度,我们读入一对x,y.说明,x+1~y-1之间牛的身高都小于x,y. 然后不妨 ...
- 第十章、typing模块
目录 第十章.typing模块 一.说明 二.typing模块的作用 三.使用typing模块 四.typing常用类型 第十章.typing模块 一.说明 二.typing模块的作用 类型检查,防止 ...
- mysql 使用zip包进行安装以及服务启动后立即关闭问题
本实例使用的mysql版本为 mysql-8.0.15-winx64 1.下载zip包 官网地址:https://dev.mysql.com/downloads/mysql/ 2.安 ...
- Linux:rm可不可以实现删除所有文件,除了demo文件
方法1: shopt -s extglob #开启扩展通配符 rm -rf !(demo) #删除除了demo的文件 方法2: find /test -not -name "demo&quo ...
- 【原创】马哥 文本三剑客之awk
命令 awk 全称: man搜索: 简述 基本用法 选项 用法与实验 print 打印 (1)(2)(3) 变量 1.内建变量 FS与OFS RS与ORS NR与FNR NF ARGC与ARGC 2. ...
- kill命令和killall命令
kill命令用于终止指定的进程(terminate a process),是Unix/Linux下进程管理的常用命令.通常,我们在需要终止某个或某些进程时,先使用ps/pidof/pstree/top ...