Routing(exchange--direct)
引言
它是一种完全按照routing key(路由关键字)进行投递的:当消息中的routing key和队列中的binding key完全匹配时,才进行会将消息投递到该队列中
1.模型

2.创建生产者
package com.dwz.rabbitmq.exchange.direct; 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 {
private static final String EXCHANGE_NAME = "test_direct_exchange"; public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection(); Channel channel = connection.createChannel(); String routingKey_1 = "test.direct_1";
String routingKey_2 = "test.direct_2"; String msg_1 = "send rabbit direct message--1";
String msg_2 = "send rabbit direct message--2"; channel.basicPublish(EXCHANGE_NAME, routingKey_1, null, msg_1.getBytes());
channel.basicPublish(EXCHANGE_NAME, routingKey_2, null, msg_2.getBytes());
channel.close();
connection.close();
}
}
3.创建消费者1
package com.dwz.rabbitmq.exchange.direct; 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; public class Consumer {
private static final String QUEUE_NAME = "test_direct_queue_1";
private static final String EXCHANGE_NAME = "test_direct_exchange"; public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection(); Channel channel = connection.createChannel(); String routingKey = "test.direct_1"; channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, false, null);
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, 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 direct_1 message:" + msg);
}
}; channel.basicConsume(QUEUE_NAME, true, consumer);
}
}
4.创建消费者2
package com.dwz.rabbitmq.exchange.direct; 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; public class Consumer2 {
private static final String QUEUE_NAME = "test_direct_queue_2";
private static final String EXCHANGE_NAME = "test_direct_exchange"; public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection(); Channel channel = connection.createChannel(); String routingKey = "test.direct_2"; channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, false, null);
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, 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 direct_2 message:" + msg);
}
}; channel.basicConsume(QUEUE_NAME, true, consumer);
}
}
5.运行代码
通过路由键 routingKey 得到指定信息,success!
Routing(exchange--direct)的更多相关文章
- RabbitMQ --- Publish/Subscribe(发布/订阅)
目录 RabbitMQ --- Hello Mr.Tua RabbitMQ --- Work Queues(工作队列) 前言 在第二篇文章中介绍了 Work Queues(工作队列),它适用于把一个消 ...
- 8、RabbitMQ三种Exchange模式(fanout,direct,topic)的性能比较
RabbitMQ三种Exchange模式(fanout,direct,topic)的性能比较 RabbitMQ中,除了Simple Queue和Work Queue之外的所有生产者提交的消息都由Exc ...
- RabbitMQ --- Routing(路由)
目录 RabbitMQ --- Hello Mr.Tua RabbitMQ --- Work Queues(工作队列) RabbitMQ --- Publish/Subscribe(发布/订阅) 前言 ...
- POJ 1860 Currency Exchange (最短路)
Currency Exchange Time Limit : 2000/1000ms (Java/Other) Memory Limit : 60000/30000K (Java/Other) T ...
- Linux Direct 文件读写(文件DIO)
有时候,读写文件并不想要使用系统缓存(page cache),此时 direct 文件读写就派上了用场,使用方法: (1)打开文件时,添加O_DIRECT参数: 需要定义_GNU_SOURCE,否则找 ...
- 使用 EWS(Exchange Web Service)协议读取邮件、发送邮件
问题: 公司之前可以通过POP3协议收发邮件,因而在SoapUI中用JavaMail可以读取邮件,后来配置了Office 365,POP3协议端口不再开放,邮件全部读取失败,报login timeou ...
- 品友推广的投放原理 RTB:Real Time Bidding(实时竞价) DSP:Demand-Side Platform(需求方平台) 广告交易平台:AD Exchange
总结: 1.实时竞价 0.1秒出价各个广告主出价,投放价高者: RTB(Real Time Bidding)实时竞价,是一种利用第三方技术在数以百万计的网站或移动端针对每一个用户展示行为进行评估以及出 ...
- Currency Exchange(最短路)
poj—— 1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 29851 Ac ...
- RabbitMQ学习第四记:路由模式(direct)
1.什么是路由模式(direct) 路由模式是在使用交换机的同时,生产者指定路由发送数据,消费者绑定路由接受数据.与发布/订阅模式不同的是,发布/订阅模式只要是绑定了交换机的队列都会收到生产者向交换机 ...
随机推荐
- java开发环境构建
一. 基本工具安装 1. 配置终端命令别名 vim ~/.bash_profile *********************************************** # for colo ...
- Mysql(四)-2:多表查询
一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 准备表 #建表 create table department( id int, name varchar(20) ); create ta ...
- XML基础综合案例【三】
实现简单的学生管理系统 使用xml当做数据,存储学生信息 ** 创建一个xml文件,写一些学生信息 ** 增加操作1.创建解析器2.得到document 3.获取到根节点4.在根节点上面创建stu标签 ...
- (8)全志A64查看寄存器
1.查看寄存器0x01c20824寄存器的值: cd /sys/class/sunxi_dump echo 0x01c20824 > dump cat dump 例如: 2.都多个寄存器 ech ...
- MySQL查询数据库中所有数据表的数据条数
select table_name,table_rows from information_schema.tables where TABLE_SCHEMA = '数据库名称' order by ta ...
- SAP导出内表数据到excel
DATA: EXCEL TYPE OLE2_OBJECT, SHEET TYPE OLE2_OBJECT, CELL TYPE OLE2_OBJECT, ...
- SpringBoot 上传读取图片 巨坑
之前自己也做过文件上传,不过存储路径放在那个tomcat服务器路径下,就没遇到什么问题 但前几天在做图片的上传,想把文件放在项目下指定的一个文件夹下,就感觉有点麻烦 修改配置文件 在springboo ...
- PXE远程自动安装操作系统
一.PXE的工作原理 PXE:基于Client/Server的网络模式,支持远程主机通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统:PXE可以引导和安装Windows,linux等多种操 ...
- 什么是http协议(一)
http协议是大家在互联网中最为熟悉的协议,只要上网大家都会遇到,但是,很多人被问道什么是http协议,http协议的内容是什么就懵了.这里,我们随便聊聊http协议. 首先,我们说说协议.我一直觉得 ...
- 个人签发https证书
环境: jdk1.8 window7 cmder 1.生成证书库jks keytool.exe -genkeypair -alias www.bingco.com -keyalg RSA ^ -key ...