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 本系列教程主要来自于官网入门教程的翻译,然后自己进行了部分的修改与 ...
随机推荐
- Git服务器配置和基本使用
#git服务器搭建 1. 在系统中增加git用户 useradd -s /usr/bin/git-shell git 2. 在git用户的home目录下新建.ssh目录,做好相关配置 1)生成公私匙: ...
- typescript interface 泛型
interface interface Obj { [index: string]: any; } class Person { name: string; } let obj: obj = { na ...
- SpringBoot2.0整合mybatis、shiro、redis实现基于数据库权限管理系统
转自https://blog.csdn.net/poorcoder_/article/details/71374002 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管 ...
- spring BeanFactory VS FactoryBean
一.FactoryBean示例 public class DateStringFactoryBean implements FactoryBean<Object> { private bo ...
- Jmeter学习之-获取登录的oken值(1)
ps: 这里只着重讲述如何实时获取其他接口返回的值,作为此次接口的参数传递,添加接口请求的相关不再详述,可查看上一篇文章 为了方便管理,此处将:登录接口单独放在一个线程组下面,需要使用登录接口返回的t ...
- sql server创建windows账户
--不要干坏事 sql server中使用xp_cmdshell --1.允许配置高级选项 GO RECONFIGURE GO --2.开启xp_cmdshell服务 RECONFIGURE GO - ...
- cocos creator 动态创建精灵
var node = new cc.Node();var sprite = node.addComponent(cc.Sprite);sprite.spriteFrame = new cc.Sprit ...
- Unable to register MBean [HikariDataSource (HikariPool-0)] with key 'dataSou rce'; nested exception is javax.management.InstanceAlreadyExistsException: com.z axxer.hikari:name=dataSource,type=HikariDa
今天启动项目看到已经启动起来,但是看到控制台有红色,没注意是什么问题,具体在细看下,发现是一个Tomcat中发布了两个实例. 解决办法:去发布路径下,全部删掉或者删掉不用的即可.
- c#之如何操作excel
可使用EPPlus类库,下载地址如下: http://epplus.codeplex.com/ 也可以在这里下载: https://files.cnblogs.com/files/jietian331 ...
- Python之模块导入
import sys #import module (.py)import functools #名词空间 functoolsprint(functools) print("-------- ...