rabbitmq学习5:Topics
来源:互联网 发布:手机淘宝怎么进店铺 编辑:程序博客网 时间:2024/06/05 20:43
RabbitMQ学习笔记;Exchange;topics
在前面的《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学习5:Topics
- rabbitmq学习5:Topics
- rabbitmq学习5:Topics
- RabbitMQ Topics
- RabbitMQ学习笔记六:话题(Topics)
- RabbitMQ 学习笔记(五):Topics
- RabbitMQ学习(五).NET Client之Topics
- RabbitMQ学习小结(五)—— Topics[Python]
- RabbitMQ java Topics主题
- RabbitMq之Routing,Topics
- 七、rabbitMQ Topics
- RabbitMQ之Topics
- RabbitMQ 之五 Topics
- python系列之 RabbitMQ -- TOPICS
- Rabbitmq教程翻译(五)Topics主题
- RabbitMQ入门教程(七):主题交换机Topics
- Topics
- SpringBoot的RabbitMQ消息队列: 六、第五模式"Topics"
- 八皇后问题的回溯算法
- .NET 过滤HTML代码
- rabbitmq学习4:Routing
- UVA 4996 Scientific Experiment
- 聚集索引和非聚集索引
- rabbitmq学习5:Topics
- sqlserver拼音助记码
- RabbitMQ学习笔记6;RPC
- Java操作PDF
- mp4文件格式解析(一)
- sqlserver删除所有的用户表
- sqlserver BASE 64加密
- 记录点滴23(中秋、国庆篇)
- rabbitmq学习7:ConnectionFactory与Connection的认知