Fanout交换器-编写生产者
生活随笔
收集整理的這篇文章主要介紹了
Fanout交换器-编写生产者
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我們再來編寫生產者,Provider,這里我們已經把消費者給寫好了,一個SmsReceiver,一個PushReceiver,先改一下,這是push receiver@RabbitHandler
public void process(String msg){System.out.println("Push..........receiver: "+msg);
}這樣筆記里要重新替換一下,Consumer寫好以后呢,我們回過來看Provider的寫法,Provider的配置文件已經改好了,我們把交換器的名稱交換一下就可以了,然后我們再看sender這個類,在這個類當中呢,我們取交換器的名稱,然后這里沒有路由鍵了,就可以把這個刪掉,那么沒有路由鍵了,在發送消息的時候,對于convertAndSend方法的第二個參數,我們該怎么去辦呢,注意,這里我們只要給空串就可以了,沒有路由鍵的,我們只要給空串就可以了,這樣我們的消息發送者,就會交給我們的fanout交換器public void send(String msg){//向消息隊列發送消息//參數一:交換器名稱。//參數二:路由鍵//參數三:消息this.rabbitAmqpTemplate.convertAndSend(this.exchange,"", msg);
}fanout交換器再把消息廣播到其他的隊列當中,所以這塊需要注意,把路由鍵去掉,換乘空串就可以了,接下來我們就來測試一下,如果我們的服務端,Sender,發送一條數據,這條數據能不能同時發送到PushReceiver和SmsReceiver當中,我們先把消費者啟動,觀察控制臺,我們再去運行我們的測試代碼,Provider的測試代碼,這塊我們也不用死循環了,直接發送Hello RabbitMQ,我們來運行,觀察控制臺,我們可以看到,現在SMS Receiver和Push Receiver同時收到消息了Push..........receiver: Hello RabbitMQ
Sms........receiver: Hello RabbitMQ也就是說我現在這個消息,廣播到了兩個隊列當中,這個不就是訂閱兩個隊列對象嗎,我們就直接輸出兩個相同的東西,是不是Hello Rabbit其實最后你會發現,Fanout交換器,最大的區別,跟我們之前講的Direct,和Topic,他們最大的區別是什么呢,他并不會通過路由鍵的特點,去發送的一個過程,因為他沒有路由鍵,沒有路由鍵的是通過廣播的方式發送到所有的隊列當中,那么其他的消息訂閱者這一塊,也不需要有路由鍵,因為一旦有路由鍵,就是從某個特定的隊列了,所以這塊需要注意,我們把Provider的代碼copy一下,還是比較簡單的,只要改動一個地方,把路由鍵換成空串就可以了,把它整理到筆記當中,測試代碼什么都不用動,那我們的fanout交換器就講解完了,希望能對這三種交換器,通過這三個案例呢,有一個本質上的一個認識,區別,然后我們在開發的過程當中呢,可以根據自己的開發情況,需求,來決定,用哪種交換器來傳遞消息
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>rabbitmq-fanout-provider</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.12.RELEASE</version><relativePath/> </parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><thymeleaf.version>3.0.9.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><!-- 這個插件,可以將應用打包成一個可執行的jar包 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
spring.application.name=rabbitmq-fanout-providerspring.rabbitmq.host=59.110.158.145
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guestmq.config.exchange=order.fanout
package com.learn;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class RabbitFanoutProviderApplication {public static void main(String[] args) {SpringApplication.run(RabbitFanoutProviderApplication.class, args);}
}
package com.learn.test;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;import com.learn.RabbitFanoutProviderApplication;
import com.learn.Sender;/*** 消息隊列測試類* @author Administrator**/
@RunWith(SpringRunner.class)
@SpringBootTest(classes=RabbitFanoutProviderApplication.class)
public class QueueTest {@Autowiredprivate Sender sender;/** 測試消息隊列*/@Testpublic void test1(){this.sender.send("Hello RabbitMQ");}
}
?
總結
以上是生活随笔為你收集整理的Fanout交换器-编写生产者的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Fanout交换器-编写消费者
- 下一篇: CentOS查看 占用 内存 最多的 进