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 ...
随机推荐
- Tomcat 的部署器
要使用一个Web应用程序,必须要将表示该应用程序的Context实例部署到一个Host实例中,在Tomcat中,Context实例可以用WAR文件的形式来部署,也可以将整个WEB应用程序复制到Tomc ...
- docker基础知识
兴起于2010年,2013年docker开源. 什么是docker? built ship run 官方定位: Docker is a world's leading software contain ...
- python版本
一般在Linux下,默认会安装一个python2.*的版本,但是我们自己开发有时候需要python3.*的版本 1. 安装python3 .安装依赖包 )首先安装gcc编译器,gcc有些系统版本已经默 ...
- 封装一些简单的 dom 操作
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- js div模拟水平滚动条
这个也是我百度到的,但是忘记保存连接了,现在把代码贴上来,有需要的可以参考一下: <!DOCTYPE html> <html> <head> <meta ch ...
- Line 算法与deepwalk的对比 和个人理解
用户的关注关系本身就是一个图结构,要从用户关注关系生成用户的embedding表示,其实就是做graph的emebding表示. deepwalk+word2vec 比较简单,效果也还可以.这种方法再 ...
- 【异常】Caused by: java.lang.IllegalStateException: Zip64 archives are not supported
1 自己打包Spring boot项目依赖了第三方的Phoenix jar包过大,导致启动后报错 参考了这篇博客:https://cloud.tencent.com/developer/ask/135 ...
- apache启动错误 AH00072: make_sock: could not bind to address [::]:443 windows系统端口/进程查看
1. netstat -ano|findstr " 2. tasklist|findstr "
- 并发编程:协程TCP、非阻塞IO、多路复用、
一.线程池实现阻塞IO 二.非阻塞IO模型 三.多路复用,降低CPU占用 四.模拟异步IO 一.线程池实现阻塞IO 线程阻塞IO 客户端 import socket c = socket.socket ...
- Hadoop_08_客户端向HDFS读写(上传)数据流程
1.HDFS的工作机制: HDFS集群分为两大角色:NameNode.DataNode (Secondary Namenode) NameNode负责管理整个文件系统的元数据 DataNode 负责管 ...