当前位置:网站首页>Simple use of directexchange switches.
Simple use of directexchange switches.
2022-07-19 05:01:00 【Ozawa can't Java】
stay Fanout In the pattern , A message , Will be consumed by all subscribed queues . however , In some cases , We want different messages to be consumed by different queues . This is the time to use Direct Type of Exchange.

stay Direct Model Next :
The binding of queues to switches , It can't be arbitrary binding , But to To specify a
RoutingKey( route key)The sender of the message is towards Exchange When sending a message , You must also specify the
RoutingKey.Exchange No longer give messages to each bound queue , It's based on the news
Routing KeyJudge , There's only a line ofRoutingkeyWith the newsRouting keyExactly the same , To receive messages
Specific code implementation
(1): Among consumers
Declare queues and switches based on annotations
be based on @Bean It is troublesome to declare queues and switches in a way ,Spring Annotation based declarations are also provided .
stay consumer Of SpringRabbitListener Add two consumers , Both queues and switches are declared based on annotations :
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "direct.queue1"),
exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
key = {"red", "blue"}
))
public void listenDirectQueue1(String msg){
System.out.println(" Consumers receive direct.queue1 The news of :【" + msg + "】");
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "direct.queue2"),
exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
key = {"red", "yellow"}
))
public void listenDirectQueue2(String msg){
System.out.println(" Consumers receive direct.queue2 The news of :【" + msg + "】");
}(2:) Among producers
@Test
public void testSendDirectExchange() {
// Switch name
String exchangeName = "itcast.direct";
// news
String message = " Red Alert ! Japan's indiscriminate discharge of nuclear waste water , Cause marine life to mutate , Surprise Godzilla !";
// Send a message
rabbitTemplate.convertAndSend(exchangeName, "red", message);
}Describe below Direct Switches and Fanout Switch differences ?
Fanout The switch routes messages to each queue bound to it
Direct Switch based on RoutingKey Determine which queue to route to
If multiple queues have the same RoutingKey, Then with Fanout The function is similar to
be based on @RabbitListener Annotations declare what are common annotations for queues and switches ?
@Queue
@Exchange
边栏推荐
- 高等数学笔记:复合函数的二阶导数与参数方程求解曲率
- Microservice high concurrency service governance
- POC——DVWA‘s XSS Reflected
- Tasking new aurix tc37x demo project
- redis 安装
- UE plug-in electronicnodes 5.0.0/4.23-4.27
- 免签名模板审核的短信验证测试
- MYSQL两个查询条件取并集然后进行查询
- POC——DVWA‘s File Upload
- Construction and application of knowledge map de (VII): large scale knowledge map pre training
猜你喜欢
随机推荐
Some concepts of ES
shardingsphere的核心概念和快速实战
POC——DVWA‘s XSS Reflected
Restclient operation document
一文了解Zipkin
Yiwen takes you to know about haproxy
DSL search results processing, including sorting, paging, highlighting
FanoutExchange交换机简单使用
免签名模板审核的短信验证测试
Pingcap clinic data acquisition instructions
渗透测试 10 --- 扫描 web目录 (dirb、wfuzz、wpscan、nikto)
一文带你了解HAProxy
3.RestClient查询文档
Load balancer ribbon practice
拥抱声明式UI
Service end interface test - test point of interface test [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
thinkphp 官网教程
CVE-2019-14234 Django JSONField SQL注入漏洞
shardingproxy分库分表实战及同类产品对比
脱敏字段举例








