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) 路由模式是在使用交换机的同时,生产者指定路由发送数据,消费者绑定路由接受数据.与发布/订阅模式不同的是,发布/订阅模式只要是绑定了交换机的队列都会收到生产者向交换机 ...
随机推荐
- lua加载DLL
.cpp //若没有在项目属性--库文件.依赖文件.包含添加.则添加一下路径 #pragma comment (lib,"lua5.1.lib") #include " ...
- Sublime Text3 插件收录
收录常用的Sublime Text3 插件, 方便安装使用,免得每次一个个的搜, 欢迎补充 安装方法直接打开install package 搜索安装 1. Babel 支持react jsx语法 2. ...
- unity 3D循环滚动效果
https://blog.csdn.net/qinyuanpei/article/details/52765356 https://blog.csdn.net/chongzi_daima/articl ...
- 【weixin】微信支付简介
一.微信支付模式 1.付款码支付 付款码支付是用户展示微信钱包内的“刷卡条码/二维码”给商户系统扫描后直接完成支付的模式.主要应用线下面对面收银的场景. 2.Native支付 Native支付是商户系 ...
- MySQL之常用查询
1) 查询字符集 show variables like 'character%'; 2)查看版本 select version(); 3) 共享表空间的数据文件存储路径 show variables ...
- 【Git的基本操作七】创建远程库、在本地创建远程库别名
1. 创建远程库
- 99乘法表(js)
//九九乘法表 let i,j,str; for(i=1;i<=9;i++) { str = ""; for(j=1;j<=i;j++) { str = str+i+' ...
- 退居三线iOS开发的自主开发历程
忙前忙后,一切终将步入正轨,在忙也要抽出时间思考自己的事情 推荐一篇简书(https://www.jianshu.com/u/8367278ff6cf)讲解很官方 Metal体验 学习了一些基础的视频 ...
- PL/SQL Developer -> 下载 -> 安装 ->执行SQL -> 设置本地/远程连接
一 下载 点击进入 https://www.allroundautomations.com/bodyplsqldevreg.html 二 安装 4wkf7lzcb8amvke2rzeuclnk5emc ...
- JAVA 分布式
什么是分布式系统? 要理解分布式系统,主要需要明白一下2个方面: 1.分布式系统一定是由多个节点组成的系统. 其中,节点指的是计算机服务器,而且这些节点一般不是孤立的,而是互通的. 2.这些连通的节点 ...