当前位置:网站首页>Direct exchange
Direct exchange
2022-07-23 16:46:00 【A light wind and light clouds】
Preface
We built a simple logging system . We can broadcast log messages to many recipients . In this section, we will add some special features - For example, we only let a consumer subscribe to some of the published news . For example, we only store serious error messages in the log file ( To save disk space ), At the same time, all log messages can still be printed on the console .
Binding is the bridge between switch and queue . You can understand that :
The queue is only interested in the messages of the switch to which it is bound . Binding parameters :routingKey It can also be called binding key, To create a binding, we use code :channel.queueBind(queueName, EXCHANGE_NAME, "routingKey"); The meaning after binding is determined by its exchange type .
Direct exchange Introduce
We want programs that write log messages to disk to receive only serious errors (errros), Instead of storing warnings (warning) Or information (info) Log messages avoid wasting disk space .Fanout This type of exchange does not give us much flexibility - It can only broadcast unconsciously , Here we will use direct This type to replace , This type of work is , The message only goes to its bound routingKey The queue to .
In the picture above , We can see X Bound to two queues , The binding type is direct. queue Q1 The binding key is orange, queue Q2 The binding key has two : A binding key is black, The other binding key is green.
In this case of binding , The producer publishes a message to exchange On , The binding key is orange The message will be published to the queue
Q1. The binding key is blackgreen And messages will be published to the queue Q2, Messages of other message types will be discarded .
Multiple bindings
Of course, if exchange The binding type of is direct, But it binds to multiple queues key If they are the same , In this case, although the binding type is direct But it behaves like fanout It's kind of like , It's like broadcasting , As shown in the figure below .
Combat code
Tool class :
public class untils {
public static Channel getChannel() throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.231.132");
factory.setUsername("admin");
factory.setPassword("123");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
return channel;
}
}consumer 1:
public class ReceiveLogsDirect01 {
private static final String EXCHANG_NAME="direct_logs";
public static void main(String[] args) throws Exception{
Channel channel = untils.getChannel();
/**
* Declaration switch
*/
channel.exchangeDeclare(EXCHANG_NAME, BuiltinExchangeType.DIRECT);
String queueName="disk";
channel.queueDeclare(queueName,false,false,false,null);
channel.queueBind(queueName,EXCHANG_NAME,"error");
System.out.println(" Preparing to receive messages ....");
DeliverCallback deliverCallback=(Consumer,delivert)->
{
String s = new String(delivert.getBody());
System.out.println(" Error message --->:"+s);
};
channel.basicConsume(queueName,true,deliverCallback,consumerTag ->{} );
}
}consumer 2:
public class ReceiveLogsDirect02 {
private static final String EXCHANG_NAME="direct_logs";
public static void main(String[] args) throws Exception{
Channel channel = untils.getChannel();
/**
* Declaration switch
*/
channel.exchangeDeclare(EXCHANG_NAME, BuiltinExchangeType.DIRECT);
String queueName="console";
channel.queueDeclare(queueName,false,false,false,null);
channel.queueBind(queueName,EXCHANG_NAME,"info");
channel.queueBind(queueName,EXCHANG_NAME,"warning");
System.out.println(" Preparing to receive messages ....");
DeliverCallback deliverCallback=(Consumer, delivert)->
{
String s = new String(delivert.getBody());
System.out.println("info warning news --->:"+s);
};
channel.basicConsume(queueName,true,deliverCallback,consumerTag ->{} );
}
}producer :
public class EmitLogDirect {
private static final String EXCHANGE_NAME = "direct_logs";
public static void main(String[] args)throws Exception {
Channel channel = untils.getChannel();
// Create multiple bindingKeyMap
Map<String,String> bindingKeyMap=new HashMap<>();
bindingKeyMap.put("info"," Ordinary info news ");
bindingKeyMap.put("warning"," Warning warning news ");
bindingKeyMap.put("error"," error error news ");
//debug No consumers receive this message , So I lost
bindingKeyMap.put("debug"," debugging debug news ");
for (Map.Entry<String,String> bindingKeyEntry:bindingKeyMap.entrySet())
{
String bindingKey=bindingKeyEntry.getKey();
String message=bindingKeyEntry.getValue();
channel.basicPublish(EXCHANGE_NAME,bindingKey,null,message.getBytes(StandardCharsets.UTF_8));
System.out.println(" The producer sends a message "+message);
}
}
}result :



边栏推荐
- Oralce中实现将指定列的指定内容替换为想要的内容
- Flutter | 给 ListView 添加表头表尾最简单的方式
- 三方支付公司有哪些?
- Ali Er Mian: when does MySQL use table locks and row locks?
- Frequently asked questions about MySQL
- 动态规划背包问题之01背包详解
- [taro] applet picker dynamically obtains data
- YOLOv4: Optimal Speed and Accuracy of Object Detection
- 网络协议与攻击模拟:wireshark使用、ARP协议
- [C language] structure, enumeration and union
猜你喜欢

946. Verify stack sequence ●● & sword finger offer 31. stack push in and pop-up sequence ●●

Unity Metaverse(一)、Ready Player Me & Blender 自定义你的Avatar虚拟人

国内生产总值(GDP)数据可视化

ESP8266-NodeMCU——从苏宁API获取实时天气

腾讯云获国际专业流媒体测评肯定:三大场景下视频编码性能全部最优

32位单片机GPIO端口电路结构以及输出模式

小米集团副总裁崔宝秋:开源是人类技术进步的最佳平台和模式

Navicat15下载安装

UiPath Studio Enterprise 22.4 Crack

学习笔记7--交通环境行为预测
随机推荐
Stm32f103+rfid-rc522 module realizes simple card reading and writing demo "recommended collection"
Do you know why PCBA circuit board is warped?
Go 接口:深入内部原理
[2022 freshmen learning] key points of the second week
Why is apple x charging slowly_ IPhone 12 supports 15W MagSafe wireless charging. What will happen to iPhone charging in the future_ Charger
The new business form of smart civil aviation has emerged, and Tupo digital twin has entered the civil aviation flight network of the Bureau
20220721挨揍内容
2022-7-22 面经复习+简单题目整理
Nifi 1.16.3 集群搭建+kerberos+用户认证
go语言多返回值以及返回错误类型
Go interface: go deep into internal principles
小米集团副总裁崔宝秋:开源是人类技术进步的最佳平台和模式
How to buy financial products with a return of more than 6%?
Transparent proxy server architecture of squid proxy service
20220721 beaten content
AC automata and fail tree
15001.系统设计方案
Frequently asked questions about MySQL
TS encapsulates the localstorage class to store information
anchor free yolov1