RabbitMQ(5)FanoutExchange
Fanout 就是我们熟悉的广播模式或者订阅模式,给Fanout交换机发送消息,绑定了这个交换机的所有队列都收到这个消息。
生产者工程:
package com.example.demo.rabbitMq.exchange.fanout; import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class FanoutRabbitConfig { @Bean
public Queue AMessage() {
return new Queue("fanout.A");
} @Bean
public Queue BMessage() {
return new Queue("fanout.B");
} @Bean
public Queue CMessage() {
return new Queue("fanout.C");
} @Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange("fanoutExchange");
} @Bean
Binding bindingExchangeA(Queue AMessage, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(AMessage).to(fanoutExchange);
} @Bean
Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(BMessage).to(fanoutExchange);
} @Bean
Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(CMessage).to(fanoutExchange);
} }
发送消息:
package com.example.demo.rabbitMq.exchange.fanout; import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class FanoutSender {
@Autowired
private AmqpTemplate rabbitTemplate; public void send() {
String context = "hi, fanout msg ";
System.out.println("Sender : " + context); //这里使用了A、B、C三个队列绑定到Fanout交换机上面,发送端的routing_key写任何字符都会被忽略
this.rabbitTemplate.convertAndSend("fanoutExchange","XCC", context);
}
}
消费者工程:
package com.example.demo.rabbitMq.exchange.fanout; import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component; @Component
public class FanoutReceiver { @RabbitHandler
@RabbitListener(queues = "fanout.A")
public void processA(String context) {
System.out.println("Receiver A : " + context);
} @RabbitHandler
@RabbitListener(queues = "fanout.B")
public void processB(String context) {
System.out.println("Receiver B : " + context);
} @RabbitHandler
@RabbitListener(queues = "fanout.C")
public void processC(String context) {
System.out.println("Receiver C : " + context);
}
}
测试:
启动消费工程,生产者工程发送消息:
package com.example.demo.rabbitMq; import com.example.demo.rabbitMq.exchange.fanout.FanoutSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMqFanoutTest {
@Autowired
private FanoutSender fanoutSender; @Test
public void send() throws Exception {
fanoutSender.send();
}
}
结果:

RabbitMQ(5)FanoutExchange的更多相关文章
- 消息中间件——RabbitMQ(九)RabbitMQ整合Spring AMQP实战!(全)
前言 1. AMQP 核心组件 RabbitAdmin SpringAMQP声明 RabbitTemplate SimpleMessageListenerContainer MessageListen ...
- RabbitMQ(六)远程连接
RabbitMQ(六)远程连接 默认情况下,rabbitmq使用`guest`来连接本地(localhost)的server,当需要远程连接时,就会失效. "guest" user ...
- RabbitMQ(五) -- topics
RabbitMQ(五) -- topics `rabbitmq`中的`topic exchange`将路由键和某模式进行匹配,从而类似于正则匹配的方式去接收喜欢的信息. topic exchange ...
- RabbitMQ(四) -- Routing
RabbitMQ(四) -- Routing `rabbitmq`可以通过路由选择订阅者来发布消息. Bindings 通过下面的函数绑定Exchange与消息队列: channel.queue_bi ...
- RabbitMQ(三) -- Publish/Subscribe
RabbitMQ(三) -- Publish/Subscribe `rabbitmq`支持一对多的模式,一般称为发布/订阅.也就是说,生产者产生一条消息后,`rabbitmq`会把该消息分发给所有的消 ...
- RabbitMQ(二) -- Work Queues
RabbitMQ(一) -- Work Queues RabbitMQ使用Work Queues的主要目的是为了避免资源使用密集的任务,它不同于定时任务处理的方式,而是把任务封装为消息添加到队列中.而 ...
- RabbitMQ (五)主题(Topic)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37706355 上一篇博客中,我们进步改良了我们的日志系统.我们使用direct类 ...
- RabbitMQ (四) 路由选择 (Routing)
上一篇博客我们建立了一个简单的日志系统,我们能够广播日志消息给所有你的接收者,如果你不了解,请查看:RabbitMQ (三) 发布/订阅.本篇博客我们准备给日志系统添加新的特性,让日志接收者能够订阅部 ...
- RabbitMQ (三) 发布/订阅
转发请标明出处:http://blog.csdn.net/lmj623565791/article/details/37657225 本系列教程主要来自于官网入门教程的翻译,然后自己进行了部分的修改与 ...
随机推荐
- The missing package manager for macOS (or Linux)
The missing package manager for macOS (or Linux) — Homebrew https://brew.sh/
- spring boot maven META-INF/MAINIFEST.MF
unzip -p charles.jar META-INF/MANIFEST.MF https://blog.csdn.net/isea533/article/details/50278205 htt ...
- C++11 std::call_once:保证函数在任何情况下只调用一次
std::call_once的作用是很简单的, 就是保证函数或者一些代码段在并发或者多线程的情况下,始终只会被执行一次.比如一些init函数,多次调用可能导致各种奇怪问题. 给个例子: #includ ...
- 41A
#include <stdio.h> #include <string.h> #define MAXSIZE 105 int main() { char Berlandish[ ...
- mysql 日期相关 CURRENT_TIMESTAMP, CURRENT_DATE, CURRENT_TIME
MySQL 时间函数 SELECT CURRENT_TIMESTAMP, CURRENT_DATE, CURRENT_TIME, CURRENT_TIME(), NOW(); https://dev. ...
- redis集群(jedis)批量删除同一前缀
public Set<String> getByPrefix(String key) { Set<String> setResult = new HashSet<> ...
- Git 在 windows 上面的安装
参考博客: https://blog.csdn.net/xiezhongyuan07/article/details/79411299 将该作者的文章搬过来, 大家可以直接看上面的原文章. 下面是拷贝 ...
- Java IO--BIO
一.java io 概述 1.1 相关概念 Java IO Java IO即Java 输入输出系统.不管我们编写何种应用,都难免和各种输入输出相关的媒介打交道,其实和媒介进行IO的过程是十分复杂的,这 ...
- uva12545
#include<iostream> using namespace std; +; char s[maxn],t[maxn]; int main(){ //freopen("1 ...
- mysql 事务锁超时时间 innodb_lock_wait_timeout
mysql 事务锁超时时间 innodb_lock_wait_timeout: # 查询全局等待事务锁超时时间 SHOW GLOBAL VARIABLES LIKE 'innodb_lock_wait ...