当前位置:网站首页>Switch exchanges
Switch exchanges
2022-07-23 10:49:00 【A light wind and light clouds】
Exchanges Concept
RabbitMQ The core idea of the messaging model is : Messages produced by producers are never sent directly to the queue . actually , Often producers don't even know which queues these messages are delivered to .
contrary , Producers can only send messages to switches (exchange), The work of the switch is very simple , On the one hand, it receives messages from producers , On the other hand, push them into the queue . The switch must know exactly how to handle the received message . Should these messages be put in a specific queue, or should they be put in many queues, or should they be discarded . This depends on the type of switch .
Exchanges The type of
There are the following types :
direct (direct), The theme (topic) , title (headers) , Fan out (fanout)
unknown exchange
In the previous part, we are right exchange Know nothing about , But still able to send messages to the queue . The reason why it can be implemented before is that we use the default exchange , We pass an empty string (“”) Are identified .

The first parameter is the name of the switch . An empty string indicates a default or nameless switch : The message can be routed to the queue by routingKey(bindingkey) binding key designated , If it exists
Temporary queues
The name of the queue is very important to us - We need to specify which queue of messages our consumers consume .
Whenever we connect to Rabbit when , We all need a new empty queue , To do this, we can create a queue with random names , Or let the server choose a random queue name for us . Second, once we disconnect the consumer , The queue will be automatically deleted .
The way to create a temporary queue is as follows :
String queueName = channel.queueDeclare().getQueue();
When it's created, it grows like this :
binding (bindings)
What is? bingding Well ,binding It's actually exchange and queue The bridge between , It tells us exchange Bound to that queue . For example, what the following picture tells us is X And Q1 and Q2 bound
Fanout
Fanout Introduce
Fanout This type is very simple . As you can guess from the name , It broadcasts all received messages to all queues it knows . The system defaults to some exchange type

Fanout actual combat
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;
}
}producer :
public class EmitLog {
private static final String EXCHNGE_NAME="logs";
public static void main(String[] args) throws Exception{
/**
* Declare a switch
* 1. Type of switch
* 2.exchang The type of
*/
Channel channel = untils.getChannel();
channel.exchangeDeclare(EXCHNGE_NAME,"fanout");
Scanner sc=new Scanner(System.in);
System.out.println(" Please enter a message ");
while (sc.hasNext()){
String message=sc.nextLine();
channel.basicPublish(EXCHNGE_NAME,"",null,message.getBytes(StandardCharsets.UTF_8));
System.out.println(" The message sent was "+message);
}
}
}consumer :( Start two threads )
public class ReceiveLogs {
private static final String EXCHANGE_NAME="logs";
public static void main(String[] args) throws Exception {
Channel channel = untils.getChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
/**
* Generate a temporary queue , The name of the queue is random
* When the consumer disconnects from the queue , Automatic queue deletion
*/
String queue = channel.queueDeclare().getQueue();
// Bind the temporary queue to our exchange, among routingkey( Also known as bindingkey)
channel.queueBind(queue, EXCHANGE_NAME, "");
System.out.println(" Waiting to receive the message ....");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" The console prints the received message "+message); };
channel.basicConsume(queue,true,deliverCallback,consumerTag -> {});
}
}effect :


Two consumers

边栏推荐
- Li Hongyi machine learning 2022-hw1
- Response对象
- [unity] avpro uses stepping pits, and the editor mode uses video playback. The video cannot be played after packaging
- Recommend a shell installation force artifact, which has been open source! Netizen: really fragrant...
- CloudCompare&PCL 点云点匹配(基于点到面的距离)
- How to protect the copyright of NFT digital collections?
- SQLZOO——SELECT from WORLD Tutorial
- toco生成tflite模型
- Operator usage and scheduling process of 31 spark
- Solve the Chinese garbled code of post request and get request in servlet
猜你喜欢
![Hololens third perspective development [nanny level tutorial] [stepping on the pit record]](/img/09/53d2578e10b6324514c45ec1f50e7e.png)
Hololens third perspective development [nanny level tutorial] [stepping on the pit record]

300 题 第六讲 二次型

SQLZOO——SELECT from WORLD Tutorial

kex_ exchange_ Identification: read: connection reset by peer imperfect solution (one)

SPR:SUPERVISED PERSONALIZED RANKING BASED ON PRIOR KNOWLEDGE FOR RECOMMENDATION

SQLZOO——SELECT from WORLD Tutorial

Jump statement and debugger

第一章概述-------第一节--1.2互联网概述

组件中的自定义事件

CloudCompare&PCL 点云点匹配(基于点到面的距离)
随机推荐
Error in na.fail.default(list(Purchase = c(“CH“, “CH“, “CH“, “MM“, “CH“, : missing values in obj
理解ASP.NET Core - 基于Cookie的身份认证(Authentication)
Seektiger's okaleido has a big move. Will the STI of ecological pass break out?
Linux: database connection
Li Nan, CTO of Yunqian Technology: technology and technical people, coevolution with digitalization
Redis源码与设计剖析 -- 12.集合对象
openvino_datawhale
Introduction to partition operators, broadcast variables and accumulators of 32 spark
300 题 第六讲 二次型
Anaconda虚拟环境下安装opencv报错的问题
Chapter 3 Standard Input
linux:数据库连接
Flutter 运行flutter pub get 报错“客户端没有所需特权“
TS类型体操 之 中级类型体操挑战收官之战
解决servlet中post请求和get请求中文乱码现象
Leetcode skimming -- bit by bit record 022
R语言使用DALEX包对h2o包构建的机器学习模型进行解释分析:总结及实战
【Warning】YOLOV5训练时的ignoring corrupt image/label: [Errno 2].....,无法全部训练数据集,快速带你解决它
12 个适合做外包项目的开源后台管理系统
20. Valid brackets