RabbitMQ Fanout交换器(广播)

需求

把消息传递到所有的队列中
在这里插入图片描述在这里插入图片描述

生产者

pom.xml  App.java  QueueTest.java 同(https://blog.csdn.net/u010667710/article/details/103292588)
application.yml
#rabbitmq 基础配置 链接 账号 密码 同(https://blog.csdn.net/u010667710/article/details/103292588)
.......
#设置交换器的名称
mq.config.exchange: order.fanout

Sender.java
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 消息发送者
* @author Administrator
*
*/
@Component
public class Sender {
       @Autowired
       private AmqpTemplate rabbitAmqpTemplate;
       
       //exchange 交换器名称
       @Value("${mq.config.exchange}")
       private String exchange;
       
       /*
        * 发送消息的方法
        */
       public void send(String msg){
             //向消息队列发送消息
             //参数一:交换器名称。
             //参数二:路由键
             //参数三:消息
             this.rabbitAmqpTemplate.convertAndSend(this.exchange,"", msg);
       }
}

消费者

pom.xml  App.java 同上
application.yml
#rabbitmq 基础配置 链接 账号 密码 同上
.......
#设置交换器的名称
mq.config.exchange: order.fanout
#短信 队列名称
mq.config.queue.sms: order.sms
#PUSH 队列名称
mq.config.queue.push: order.push

PushReceiver.java
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 消息接收者
* @author Administrator
* @RabbitListener bindings:绑定队列
* @QueueBinding  value:绑定队列的名称
*                exchange:配置交换器
*
* @Queue value:配置队列名称
*        autoDelete:是否是一个可删除的临时队列
*
* @Exchange value:为交换器起个名称
*           type:指定具体的交换器类型
*/
@Component
@RabbitListener(
             bindings=@QueueBinding(
                           value=@Queue(value="${mq.config.queue.push}",autoDelete="true"),
                           exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.FANOUT)
             )
       )
public class PushReceiver {
       /**
        * 接收消息的方法。采用消息队列监听机制
        * @param msg
        */
       
       @RabbitHandler
       public void process(String msg){
             System.out.println("PUSH........receiver: "+msg);
       }
}

SmsReceiver.java
@RabbitListener(
             bindings=@QueueBinding(
                           value=@Queue(value="${mq.config.queue.sms}",autoDelete="true"),
                           exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.FANOUT)
             )
       )

测试流程

先运行消费者的App.java
再运行生产者的测试代码
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值