欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring boot整合RabbitMQ實例詳解(Fanout模式)

 更新時間:2017年04月26日 14:24:46   作者:牛頭人  
這篇文章主要介紹了spring boot整合RabbitMQ的實例講解(Fanout模式),非常不錯,具有參考借鑒價值,需要的朋友可以參考下

1.Fanout Exchange介紹

Fanout Exchange 消息廣播的模式,不管路由鍵或者是路由模式,會把消息發(fā)給綁定給它的全部隊列,如果配置了routing_key會被忽略。

如上圖所示,即當(dāng)使用fanout交換器時,他會將消息廣播到與該交換器綁定的所有隊列上,這有利于你對單條消息做不同的反應(yīng)。

例如存在以下場景:一個web服務(wù)要在用戶完善信息時,獲得積分獎勵,這樣你就可以創(chuàng)建兩個對列,一個用來處理用戶信息的請求,另一個對列獲取這條消息是來完成積分獎勵的任務(wù)。

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 {
  //創(chuàng)建隊列
  @Bean
  public Queue AMessage() {
    return new Queue("fanout.A");
  }
  //創(chuàng)建隊列
  @Bean
  public Queue BMessage() {
    return new Queue("fanout.B");
  }
  //創(chuàng)建隊列
  @Bean
  public Queue CMessage() {
    return new Queue("fanout.C");
  }
  //創(chuàng)建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).消息生產(chǎn)者

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模式),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 深入理解Java中的弱引用

    深入理解Java中的弱引用

    這篇文章主要介紹了深入理解Java中的弱引用,本文講解了強引用、弱引用、引用隊列、四種引用、軟引用、虛引用等內(nèi)容,需要的朋友可以參考下
    2015-01-01
  • 詳解 Java HashMap 實現(xiàn)原理

    詳解 Java HashMap 實現(xiàn)原理

    這篇文章主要介紹了詳解 Java HashMap 實現(xiàn)原理的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-03-03
  • Java將Date日期類型字段轉(zhuǎn)換成json字符串的方法

    Java將Date日期類型字段轉(zhuǎn)換成json字符串的方法

    這篇文章主要給大家介紹了關(guān)于Java將Date日期類型字段轉(zhuǎn)換成json字符串的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • JAVA實現(xiàn)將磁盤中所有空文件夾進行刪除的代碼

    JAVA實現(xiàn)將磁盤中所有空文件夾進行刪除的代碼

    這篇文章主要介紹了JAVA實現(xiàn)將磁盤中所有空文件夾進行刪除的代碼,需要的朋友可以參考下
    2017-06-06
  • vue+ java 實現(xiàn)多級菜單遞歸效果

    vue+ java 實現(xiàn)多級菜單遞歸效果

    這篇文章主要介紹了vue+ java 實現(xiàn)多級菜單遞歸效果,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • 詳解SpringBoot健康檢查的實現(xiàn)原理

    詳解SpringBoot健康檢查的實現(xiàn)原理

    這篇文章主要介紹了詳解SpringBoot健康檢查的實現(xiàn)原理,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot框架,感興趣的朋友可以了解下
    2021-03-03
  • Java求質(zhì)數(shù)的幾種常用算法分析

    Java求質(zhì)數(shù)的幾種常用算法分析

    這篇文章主要介紹了Java求質(zhì)數(shù)的幾種常用算法,結(jié)合實例形式分析了三種比較常見的求質(zhì)數(shù)算法原理及相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2018-12-12
  • Java中的SynchronousQueue阻塞隊列及使用場景解析

    Java中的SynchronousQueue阻塞隊列及使用場景解析

    這篇文章主要介紹了Java中的SynchronousQueue阻塞隊列及使用場景解析,SynchronousQueue 是 Java 中的一個特殊的阻塞隊列,它的主要特點是它的容量為0,這意味著 SynchronousQueue不會存儲任何元素,需要的朋友可以參考下
    2023-12-12
  • 一文搞懂Spring中的JavaConfig

    一文搞懂Spring中的JavaConfig

    這篇文章主要介紹了Spring中的JavaConfig知識,包括事務(wù)注解驅(qū)動,properties配置文件加載方法,本文給大家介紹的非常詳細,需要的朋友可以參考下
    2021-09-09
  • 使用Spring?Security搭建極簡的安全網(wǎng)站教程

    使用Spring?Security搭建極簡的安全網(wǎng)站教程

    這篇文章主要為大家介紹了使用Spring?Security搭建極簡的安全網(wǎng)站教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06

最新評論