在前面的《rabbitmq学习4:Routing 》中使用一般的名字的路由,现在想通过一些路由规则让消费者来接受符合规则的消息?那应当怎么样呢?那就要用到类型为topic的Exchange了。
Topics的工作示意图如下:
我们可能从图中看到有*和#两个通配符。*表示通配一个词;#表示通配0个或多个词。
下面让我们来看看Topics的程序如何实现的吧!
P端的程序如下 :
- package com.abin.rabbitmq;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- public class EmitLogTopic {
- private static final String EXCHANGE_NAME = "topic_logs";
- public static void main(String[] argv) throws Exception {
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- Connection connection = factory.newConnection();
- Channel channel = connection.createChannel();
- channel.exchangeDeclare(EXCHANGE_NAME, "topic");//声明topic类型的Exchange
- String routingKeyOne = "logs.error.one";// 定义一个路由名为“error”
- for (int i = 0; i <= 1; i++) {
- String messageOne = "this is one error logs:" + i;
- channel.basicPublish(EXCHANGE_NAME, routingKeyOne, null, messageOne
- .getBytes());
- System.out.println(" [x] Sent '" + routingKeyOne + "':'"
- + messageOne + "'");
- }
- System.out.println("################################");
- String routingKeyTwo = "logs.error.two";
- for (int i = 0; i <= 2; i++) {
- String messageTwo = "this is two error logs:" + i;
- channel.basicPublish(EXCHANGE_NAME, routingKeyTwo, null, messageTwo
- .getBytes());
- System.out.println(" [x] Sent '" + routingKeyTwo + "':'"
- + messageTwo + "'");
- }
- System.out.println("################################");
- String routingKeyThree = "logs.info.one";
- for (int i = 0; i <= 3; i++) {
- String messageThree = "this is one info logs:" + i;
- channel.basicPublish(EXCHANGE_NAME, routingKeyThree, null,
- messageThree.getBytes());
- System.out.println(" [x] Sent '" + routingKeyThree + "':'"
- + messageThree + "'");
- }
- channel.close();
- connection.close();
- }
- }
运行结果可能如下:
- [x] Sent 'logs.error.one':'this is one error logs:0'
- [x] Sent 'logs.error.one':'this is one error logs:1'
- ################################
- [x] Sent 'logs.error.two':'this is two error logs:0'
- [x] Sent 'logs.error.two':'this is two error logs:1'
- [x] Sent 'logs.error.two':'this is two error logs:2'
- ################################
- [x] Sent 'logs.info.one':'this is one info logs:0'
- [x] Sent 'logs.info.one':'this is one info logs:1'
- [x] Sent 'logs.info.one':'this is one info logs:2'
- [x] Sent 'logs.info.one':'this is one info logs:3'
第一个C端的代码如下:
- package com.abin.rabbitmq;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- import com.rabbitmq.client.QueueingConsumer;
- public class ReceiveLogsTopic {
- private static final String EXCHANGE_NAME = "topic_logs";// 定义Exchange名称
- public static void main(String[] argv) throws Exception {
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- Connection connection = factory.newConnection();
- Channel channel = connection.createChannel();
- channel.exchangeDeclare(EXCHANGE_NAME, "topic");// 声明topic类型的Exchange
- String queueName = "queue_topic_logs1";// 定义队列名为“queue_topic_logs1”的Queue
- channel.queueDeclare(queueName, false, false, false, null);
- // String routingKeyOne = "*.error.two";// "error"路由规则
- // channel.queueBind(queueName, EXCHANGE_NAME, routingKeyOne);// 把Queue、Exchange及路由绑定
- String routingKeyTwo = "logs.*.one";//通配所有logs下第三词(最后一个)词为one的消息
- channel.queueBind(queueName, EXCHANGE_NAME, routingKeyTwo);
- System.out.println(" [*] Waiting for messages.");
- QueueingConsumer consumer = new QueueingConsumer(channel);
- channel.basicConsume(queueName, true, consumer);
- while (true) {
- QueueingConsumer.Delivery delivery = consumer.nextDelivery();
- String message = new String(delivery.getBody());
- String routingKey = delivery.getEnvelope().getRoutingKey();
- System.out.println(" [x] Received '" + routingKey + "':'" + message
- + "'");
- }
- }
- }
第一个C端的运行结果如下:
- [*] Waiting for messages.
- [x] Received 'logs.error.one':'this is one error logs:0'
- [x] Received 'logs.error.one':'this is one error logs:1'
- [x] Received 'logs.info.one':'this is one info logs:0'
- [x] Received 'logs.info.one':'this is one info logs:1'
- [x] Received 'logs.info.one':'this is one info logs:2'
- [x] Received 'logs.info.one':'this is one info logs:3'
第二个C端的程序如下:
- package com.abin.rabbitmq;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- import com.rabbitmq.client.QueueingConsumer;
- public class ReceiveLogsTopicTwo {
- private static final String EXCHANGE_NAME = "topic_logs";//定义Exchange名称
- public static void main(String[] argv) throws Exception {
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- Connection connection = factory.newConnection();
- Channel channel = connection.createChannel();
- channel.exchangeDeclare(EXCHANGE_NAME, "topic");//声明topic类型的Exchange
- String queueName = "queue_topic_logs2";//定义队列名为“queue_topic_logs2”的Queue
- channel.queueDeclare(queueName, false, false, false, null);
- String routingKeyOne = "logs.#";//通配所有logs下的消息
- channel.queueBind(queueName, EXCHANGE_NAME, routingKeyOne);//把Queue、Exchange及路由绑定
- System.out.println(" [*] Waiting for messages.");
- QueueingConsumer consumer = new QueueingConsumer(channel);
- channel.basicConsume(queueName, true, consumer);
- while (true) {
- QueueingConsumer.Delivery delivery = consumer.nextDelivery();
- String message = new String(delivery.getBody());
- String routingKey = delivery.getEnvelope().getRoutingKey();
- System.out.println(" [x] Received '" + routingKey + "':'" + message
- + "'");
- }
- }
- }
第二个C端的运行结果如下:
- [*] Waiting for messages.
- [x] Received 'logs.error.one':'this is one error logs:0'
- [x] Received 'logs.error.one':'this is one error logs:1'
- [x] Received 'logs.error.two':'this is two error logs:0'
- [x] Received 'logs.error.two':'this is two error logs:1'
- [x] Received 'logs.error.two':'this is two error logs:2'
- [x] Received 'logs.info.one':'this is one info logs:0'
- [x] Received 'logs.info.one':'this is one info logs:1'
- [x] Received 'logs.info.one':'this is one info logs:2'
- [x] Received 'logs.info.one':'this is one info logs:3'
相关推荐
在实际应用中,RabbitMQ能够支持多种消息模式和交换类型,包括工作队列(Work queues)、发布/订阅(Publish/Subscribe)、路由(Routing)、Topics类型交换机和远程过程调用(RPC)等,这些模式在RabbitMQ中通过...
通过这个RabbitMQ客户端工程,开发者不仅可以学习到如何使用Qt和QAMQP库与RabbitMQ进行通信,还能了解如何设计和组织一个完整的Qt项目,这对于构建其他基于Qt的消息传递应用具有很高的参考价值。此外,由于工程已经...
**RabbitMQ实战指南** RabbitMQ是一种广泛使用的开源消息代理和...通过深入学习并实践本书中的内容,读者将能够熟练掌握RabbitMQ的使用,将其应用于各种分布式系统中,解决异步通信、解耦系统、提高系统可靠性的挑战。
本学习资料旨在帮助你深入理解RabbitMQ的核心概念、安装与配置、工作模式以及如何在实际项目中应用。 首先,我们需要了解RabbitMQ的基本概念。消息队列(Message Queue)是一种解耦组件的方式,允许生产者发送消息...
5. **Topics(主题)**:类似于发布/订阅,但更灵活,使用通配符进行路由,例如“*.stock.*”可以匹配所有股票相关的消息。 6. **手动和自动确认消息**:生产者发送消息后,RabbitMQ默认会等待消费者确认收到消息。...
5. RabbitMQ的文档:RabbitMQ提供了详细的文档,包括AMQP队列的使用模型、配置选项等。 6. Transport配置:我们可以通过transport配置来创建Routing模型和Topics模型,这两种模型是fanout类型模型的进阶。 7. AMQP...
5. **05Topics(主题)** 主题模式是路由模式的增强版,支持使用通配符(`*`和`#`)作为路由键。这允许消费者使用更灵活的规则订阅消息,比如`weather.*`可以匹配`weather.us.east`和`weather.eu.west`等。这种方式...
通过这个"RabbitMQ-demo.zip"压缩包,我们可以学习并实践RabbitMQ的基本操作和使用场景。 首先,让我们了解一下RabbitMQ的核心概念: 1. **Broker**:RabbitMQ服务器就是一个broker,它接收、存储和转发消息。 2. ...
【RabbitMQ入门操作手册】提供了全面的RabbitMQ学习指南,从基础概念到实际操作,帮助初学者快速掌握这个强大的消息队列系统。RabbitMQ是一个基于AMQP(Advanced Message Queuing Protocol)的开源消息代理,其核心...
### MQ学习文档知识点详解 #### 一、MQ概念与特性 **1.1 同步与异步通讯** - **同步通讯**: - 特征:即时响应。 - 例子:电话通话。 - 优点:及时反馈,效率高。 - 缺点:资源消耗大,耦合度高。 - **异步...
**RabbitMQ游乐场**,也称为`rabbitmq-playground`,是一个专为学习和实践RabbitMQ消息队列技术而设计的环境。RabbitMQ是业界广泛应用的开源消息代理和队列服务器,它允许应用程序之间通过消息进行异步通信。在`...
通过学习这些示例,你可以更好地理解如何将RabbitMQ和MQTT.js集成到Web应用中,从而实现实时通讯功能。在实际项目中,你可以根据需求调整代码,例如,添加错误处理、心跳保持、以及更复杂的订阅策略等,以构建稳定且...
2. **Kafka与RabbitMQ对比**:Kafka更适合大数据实时处理,RabbitMQ适合简单消息传递。 3. **Kafka概念**:分区和副本提供容错,消费组允许多个消费者并行消费。 4. **消息存储策略**:Kafka将消息持久化到磁盘,...
消息中间件的实现有很多,有新贵Kafka、RocketMq,也有老牌劲旅RabbitMq和ActiveMq,不过我最后选择了Nsq来讲解,因为它极简、清爽,用起来舒服,讲起来也好理解,更重要的是,通过对Nsq的学习,我们很容易扩展到...
发布者发送消息到特定的主题(Topics),订阅者则根据感兴趣的主題接收消息,而代理负责路由消息,确保消息正确地从发布者传递到订阅者。 在Android平台上,为了实现MQTT消息推送,首先需要集成一个MQTT客户端库。...