當前位置:
首頁 >
Spring整合RabbitMQ(包含生产者和消费者)
發布時間:2024/1/8
35
豆豆
生活随笔
收集整理的這篇文章主要介紹了
Spring整合RabbitMQ(包含生产者和消费者)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
生產者
創建一個MAVEN項目spring-exchange-producer作為消息隊列的生產者
導入相關的依賴坐標
<dependencies><!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client --><dependency><groupId>com.rabbitmq</groupId><artifactId>amqp-client</artifactId><version>5.6.0</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework.amqp/spring-rabbit --><dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit</artifactId><version>2.1.4.RELEASE</version></dependency> </dependencies>創建spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"><!--設置連接工廠,配置基本參數--><rabbit:connection-factory id="connectionFactory" host="localhost" port="5672" username="guest" password="guest" virtual-host="/test"></rabbit:connection-factory><!--fanout-exchange,direct-exchange,topic-exchange聲明一個名為topicExchange的topic交換機,如果該交換機不存在,則自動創建--><rabbit:topic-exchange name="topicExchange" auto-declare="true"></rabbit:topic-exchange><!-- spring為我們分裝了RabbitTemplate對象來簡化生產者發送數據的過程,對常用的方法進行封裝 --><rabbit:template id="template" connection-factory="connectionFactory" exchange="topicExchange"></rabbit:template><!-- 在生產者中配置template對象,用于發送數據 --><bean id="newsProducer" class="com.kangswx.rabbitmq.exchange.NewsProducer"><property name="rabbitTemplate" ref="template"/></bean><!-- 所有產生的數據在rabbit可視化控制臺中展示 --><rabbit:admin connection-factory="connectionFactory"/> </beans>創建需要傳遞的實體類(必須要實現序列化接口)
package com.kangswx.rabbitmq.domain;import java.io.Serializable; import java.util.Date;public class News implements Serializable {private String source;private String title;private Date createTime;private String content;public News() {}public News(String source, String title, Date createTime, String content) {this.source = source;this.title = title;this.createTime = createTime;this.content = content;}public String getSource() {return source;}public void setSource(String source) {this.source = source;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}}創建生產者類
package com.kangswx.rabbitmq.exchange;import com.kangswx.rabbitmq.domain.News; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.Date;public class NewsProducer {private RabbitTemplate rabbitTemplate = null;public void sendNews(String routingKey, News news){//convertAndSend用于向exchange發送數據//第一個參數是routingKey,第二個參數是傳送的對象,可以是字符串,byte數組或者任何實現了序列化接口的對象rabbitTemplate.convertAndSend(routingKey, news);System.out.println("新聞發送成功");}public static void main(String[] args) {//手動初始化Spring的IOC容器ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");//IOC容器中獲取對象NewsProducer newsProducer = (NewsProducer) ctx.getBean("newsProducer");newsProducer.sendNews("us.20190101", new News("新華社", "特朗普又又又退群了", new Date(), "國際新聞內容"));newsProducer.sendNews("china.20190101", new News("鳳凰TV", "XXX企業榮登世界500強", new Date(), "國內新聞內容"));}public RabbitTemplate getRabbitTemplate() {return rabbitTemplate;}public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {this.rabbitTemplate = rabbitTemplate;}}消費者
創建一個MAVEN項目spring-exchange-consumer作為消息隊列的消費者
導入相關的依賴坐標(與生產者一致)
<dependencies><!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client --><dependency><groupId>com.rabbitmq</groupId><artifactId>amqp-client</artifactId><version>5.6.0</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework.amqp/spring-rabbit --><dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit</artifactId><version>2.1.4.RELEASE</version></dependency></dependencies>創建spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"><!--設置連接工廠,配置基本參數--><rabbit:connection-factory id="connectionFactory" host="localhost" port="5672" username="guest" password="guest" virtual-host="/test"></rabbit:connection-factory><!-- 所有產生的數據在rabbit可視化控制臺中展示 --><rabbit:admin connection-factory="connectionFactory"/><!-- 創建隊列 --><rabbit:queue name="topicQueue" auto-declare="true" auto-delete="false" durable="false" exclusive="false"></rabbit:queue><!-- 交換機與隊列綁定,并指定篩選條件 --><rabbit:topic-exchange name="topicExchange" auto-declare="true"><rabbit:bindings><rabbit:binding queue="topicQueue" pattern="us.*"></rabbit:binding></rabbit:bindings></rabbit:topic-exchange><!-- 啟動消費者后,Spring底層自動監聽對應的topicQueue數據,一旦有新的消息傳進來,自動傳入到consumer的recv的News參數中,之后程序對news做進一步的處理--><rabbit:listener-container connection-factory="connectionFactory"><rabbit:listener ref="consumer" method="recv" queue-names="topicQueue"/></rabbit:listener-container><!-- 消費者類 --><bean name="consumer" class="com.kangswx.rabbitmq.consumer.NewsConsumer"></bean> </beans>創建消息實體類(實體類的包名與類名必須與生產者一致,且必須實現序列化接口)
package com.kangswx.rabbitmq.domain;import java.io.Serializable; import java.util.Date;public class News implements Serializable {private String source;private String title;private Date createTime;private String content;public News() {}public News(String source, String title, Date createTime, String content) {this.source = source;this.title = title;this.createTime = createTime;this.content = content;}public String getSource() {return source;}public void setSource(String source) {this.source = source;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}}創建消費者類
package com.kangswx.rabbitmq.consumer;import com.kangswx.rabbitmq.domain.News; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class NewsConsumer {public void recv(News news){System.out.println("接收到最新消息: " + news.getTitle() + ":" + news.getSource());}public static void main(String[] args) {//手動初始化Spring的IOC容器ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");} }如果消費者需要解綁之前已經綁定的隊列,需要在RabbitMQ管理端頁面手動操作
spring整合RabbitMQ生產者代碼
spring整合RabbitMQ消費者代碼
總結
以上是生活随笔為你收集整理的Spring整合RabbitMQ(包含生产者和消费者)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 室内三维物体识别与姿态估计--背景
- 下一篇: web前端(HTML的CSS样式和Jav