spring boot整合RabbitMQ实例详解(Fanout模式)

所属分类: 软件编程 / java 阅读数: 21
收藏 0 赞 0 分享

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模式),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

Collections工具类_动力节点Java学院整理

Collections工具类提供了大量针对Collection/Map的操作。这篇文章主要介绍了Collections工具类_动力节点Java学院整理,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringMVC集成Swagger实例代码

本篇文章主要介绍了SpringMVC集成Swagger实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

十大常见Java String问题_动力节点Java学院整理

本文介绍Java中关于String最常见的10个问题,需要的朋友参考下吧
收藏 0 赞 0 分享

Java微信公众平台开发(13) 微信JSSDK中Config配置

这篇文章主要为大家详细介绍了Java微信公众平台开发第十三步,微信JSSDK中Config配置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java实现一个达达租车系统的步骤详解

这篇文章主要给大家介绍了利用Java实现一个达达租车系统的步骤,文中给出了详细的实现思路和示例代码,并在文末给出了完整的源码供大家学习下载,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

Java微信公众平台开发(14) 微信web开发者工具使用

这篇文章主要为大家详细介绍了Java微信公众平台开发第十四步,微信web开发者工具的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Spring Boot整合RabbitMQ实例(Topic模式)

Topic Exchange 转发消息主要是根据通配符。接下来通过本文给大家分享Spring Boot整合RabbitMQ实例(Topic模式),需要的朋友参考下吧
收藏 0 赞 0 分享

Java微信公众平台开发(15) 微信JSSDK的使用

这篇文章主要为大家详细介绍了Java微信公众平台开发第十五步,微信JSSDK的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java多线程的同步方法实例代码

这篇文章主要介绍了 java多线程的同步方法实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

spring boot整合RabbitMQ实例详解(Fanout模式)

这篇文章主要介绍了spring boot整合RabbitMQ的实例讲解(Fanout模式),非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多