spring boot整合RabbitMQ(Fanout模式)
1.Fanout Exchange介绍
Fanout Exchange 消息广播的模式,不管路由键或者是路由模式,会把消息发给绑定给它的全部队列,如果配置了routing_key会被忽略。

如上图所示,即当使用fanout交换器时,他会将消息广播到与该交换器绑定的所有队列上,这有利于你对单条消息做不同的反应。
例如存在以下场景:一个web服务要在用户完善信息时,获得积分奖励,这样你就可以创建两个对列,一个用来处理用户信息的请求,另一个对列获取这条消息是来完成积分奖励的任务。
2.代码示例
1).Queue配置类
FanoutRabbitConfig.java类:
package com.example.rabbitmqfanout;
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");
}
//创建Fanout交换器
@Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange("fanoutExchange");
}
//将对列绑定到Fanout交换器
@Bean
Binding bindingExchangeA(Queue AMessage,FanoutExchange fanoutExchange) {
return BindingBuilder.bind(AMessage).to(fanoutExchange);
}
//将对列绑定到Fanout交换器
@Bean
Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(BMessage).to(fanoutExchange);
}
//将对列绑定到Fanout交换器
@Bean
Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(CMessage).to(fanoutExchange);
}
}
2).消息生产者
FanoutSender.java类:
package com.example.rabbitmqfanout.rabbitmq;
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);
this.rabbitTemplate.convertAndSend("fanoutExchange","", context);
}
}
3).消息消费者
FanoutReceiverA.java类:
package com.example.rabbitmqfanout.rabbitmq;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "fanout.A")
public class FanoutReceiverA {
@RabbitHandler
public void process(String message) {
System.out.println("fanout Receiver A : " + message);
}
}
FanoutReceiverB.java类:
package com.example.rabbitmqfanout.rabbitmq;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "fanout.B")
public class FanoutReceiverB {
@RabbitHandler
public void process(String message) {
System.out.println("fanout Receiver B: " + message);
}
}
FanoutReceiverC.java类:
package com.example.rabbitmqfanout.rabbitmq;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "fanout.C")
public class FanoutReceiverC {
@RabbitHandler
public void process(String message) {
System.out.println("fanout Receiver C: " + message);
}
}
4).测试
FanoutTest.java类:
package com.example.rabbitmqfanout.rabbitmq;
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 FanoutTest {
@Autowired
private FanoutSender sender;
@Test
public void fanoutSender() throws Exception {
sender.send();
}
}
spring boot整合RabbitMQ(Fanout模式)的更多相关文章
- Spring Boot (十三): Spring Boot 整合 RabbitMQ
1. 前言 RabbitMQ 是一个消息队列,说到消息队列,大家可能多多少少有听过,它主要的功能是用来实现应用服务的异步与解耦,同时也能起到削峰填谷.消息分发的作用. 消息队列在比较主要的一个作用是用 ...
- spring boot整合RabbitMQ(Direct模式)
springboot集成RabbitMQ非常简单,如果只是简单的使用配置非常少,springboot提供了spring-boot-starter-amqp项目对消息各种支持. Direct Excha ...
- Spring Boot 整合 rabbitmq
一.消息中间件的应用场景 异步处理 场景:用户注册,信息写入数据库后,需要给用户发送注册成功的邮件,再发送注册成功的邮件. 1.同步调用:注册成功后,顺序执行发送邮件方法,发送短信方法,最后响应用户 ...
- Spring Boot整合Rabbitmq
Spring Boot应用中整合RabbitMQ,并实现一个简单的发送.接收消息的例子来对RabbitMQ有一个直观的感受和理解. 在Spring Boot中整合RabbitMQ是一件非常容易的事,因 ...
- spring boot 2.x 系列 —— spring boot 整合 RabbitMQ
文章目录 一. 项目结构说明 二.关键依赖 三.公共模块(rabbitmq-common) 四.服务消费者(rabbitmq-consumer) 4.1 消息消费者配置 4.2 使用注解@Rabbit ...
- spring boot 整合 RabbitMQ 错误
1.错误 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.spr ...
- spring boot整合RabbitMQ(Topic模式)
1.Topic交换器介绍 Topic Exchange 转发消息主要是根据通配符. 在这种交换机下,队列和交换机的绑定会定义一种路由模式,那么,通配符就要在这种路由模式和路由键之间匹配后交换机才能转发 ...
- Spring Boot整合RabbitMQ详细教程
原文:https://blog.csdn.net/qq_38455201/article/details/80308771 1.首先我们简单了解一下消息中间件的应用场景 异步处理 场景说明:用户注册后 ...
- spring boot 整合 RabbitMq (注解)
1.增加rabbitmq的依赖包 <!-- ampq 依赖包 --> <dependency> <groupId>org.springframework.boot& ...
随机推荐
- java中的类、对象、方法
类=一个种类(class)东西 对象=属于该种类的一个对象/物件(object,台湾翻译为‘物件’)方法=对这个种类的东西都可以进行的操作 比如我有一辆汽车-类 public class car {. ...
- ZOJ2417 Lowest Bit 2017-04-18 20:53 38人阅读 评论(0) 收藏
Lowest Bit Time Limit: 2 Seconds Memory Limit: 65536 KB Given an positive integer A (1 <= A ...
- mac与win7(台式电脑)共享文件
人生处处又都坑,自己走过了,所以记下来. mac共享文件,win7访问: 1.系统偏好设置-共享-都选中就行.一般都会这样说. 2.系统偏好设置-用户与群组-解锁-客人用户-允许客人用户连接到共享文件 ...
- 一起学习MVC(1)初步了解MVC
MVC 即模型视图控制器(Model View Controller) 利于团队开发.便于管理与维护.代码易读性强.未来的主流开发框架结构. 当然,缺点也显而易见,与传统开发框架相比有很大的不 ...
- 微软发布TFS 2018!
也许你还没来得及使用TFS 2017,今天,微软已经发布了TFS 2018的第一个版本(RC1). 与之前所有的候选版本一样,这是一个正式上线(微软成称为go-live)的TFS版本.如果你计划采纳T ...
- Flash(as3) 调整显示对象颜色
在游戏开发中改变显示对象的颜色是比较常见的操作,那么除了在FlashCS中调整,AS3又提供了怎样的方式呢? ColorTransform 这个类是轻量级的应用,其构造参数如下: ColorTrans ...
- [转载]未能从程序集“System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”中加载类型“System.Web.WebPages.DisplayModeProvider”
转载于http://bkcoding.cn/post_1210.html 使用vs2010开发asp.net mvc程序时,出现 未能从程序集“System.Web.WebPages, Version ...
- 蚂蚁男孩.缓存组件(Framework.Mayiboy.Caching)
它能做什么? 主要是用来方便使用缓存而诞生,该组件封装了RunTimeCache.Memcached.Redis的使用,通过简单配置就能高效快速使用起来. 使用说明 一. 下载源码,自己手动编译 ...
- pageadmin CMS网站建设教程: 附属表数据列表调用语法
列表调用语法 Html.SubDataList(参数1,参数2,参数3,参数4)参数说明: 注:第一个参数对象中必须定义Table或ParentTable/ParentField属性.参数1常用属性: ...
- Android与js交互
本文转载自:http://blog.csdn.net/it1039871366/article/details/46372207 一文. Android 中可以通过webview来实现和js的交互,在 ...