引言

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!

最新文章

  1. 【效率】专为Win7系统设计的极简番茄计时器 - MiniPomodoro (附源码)
  2. -webkit-appearance、sselect
  3. django复习笔记3:urls/views/templates三板斧
  4. warning: #870-D: invalid multibyte character sequence
  5. M5: 使用StorageFile
  6. sql语句操作集锦
  7. 整齐地输出n的平方,立方
  8. mysql 重命名表名
  9. maven自动下载jar包
  10. CSS十问
  11. jQuery 事件——关于select选中
  12. 轻松搞定表白女朋友:Android版APP (零基础也可直接下载软件)
  13. 3ds max学习笔记-- 复合对象运算
  14. 20135323符运锦----第七周:Linux内核如何装载和启动一个可执行程序
  15. 两种解决方法 PHP Warning: File upload error - unable to create a temporary file in Unknown
  16. MIT Molecular Biology 笔记2 DNA的突变和修复
  17. 14nm或于6月量产,中芯首次披露12nm及第二代FinFET "N+1"计划(详细数据)
  18. linux 查看文件夹大小 du -h --max-depth=1 ./
  19. SparkMLlib分类算法之决策树学习
  20. 20135320赵瀚青LINUX第一章读书笔记

热门文章

  1. mysql45讲
  2. 超链接hover切换效果
  3. 基于S7协议实现与西门子PLC通信
  4. 【ES6 】var/let/const的区别
  5. redis的数据结构及操作命令
  6. Redis-Set常用命令
  7. Python之for循环与while循环
  8. asyncio:python3未来并发编程主流、充满野心的模块
  9. 树莓派3B+一个外接显示器影响有线网卡无法启动的BUG
  10. 关于STM32运行时程序卡在B.处的解决方法