當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring整合RabbitMQ(包含生产者和消费者)
生活随笔
收集整理的這篇文章主要介紹了
Spring整合RabbitMQ(包含生产者和消费者)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
生產(chǎn)者
創(chuàng)建一個MAVEN項目spring-exchange-producer作為消息隊列的生產(chǎn)者
導(dǎo)入相關(guān)的依賴坐標
<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>創(chuàng)建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"><!--設(shè)置連接工廠,配置基本參數(shù)--><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交換機,如果該交換機不存在,則自動創(chuàng)建--><rabbit:topic-exchange name="topicExchange" auto-declare="true"></rabbit:topic-exchange><!-- spring為我們分裝了RabbitTemplate對象來簡化生產(chǎn)者發(fā)送數(shù)據(jù)的過程,對常用的方法進行封裝 --><rabbit:template id="template" connection-factory="connectionFactory" exchange="topicExchange"></rabbit:template><!-- 在生產(chǎn)者中配置template對象,用于發(fā)送數(shù)據(jù) --><bean id="newsProducer" class="com.kangswx.rabbitmq.exchange.NewsProducer"><property name="rabbitTemplate" ref="template"/></bean><!-- 所有產(chǎn)生的數(shù)據(jù)在rabbit可視化控制臺中展示 --><rabbit:admin connection-factory="connectionFactory"/> </beans>創(chuàng)建需要傳遞的實體類(必須要實現(xiàn)序列化接口)
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;}}創(chuàng)建生產(chǎn)者類
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發(fā)送數(shù)據(jù)//第一個參數(shù)是routingKey,第二個參數(shù)是傳送的對象,可以是字符串,byte數(shù)組或者任何實現(xiàn)了序列化接口的對象rabbitTemplate.convertAndSend(routingKey, news);System.out.println("新聞發(fā)送成功");}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(), "國際新聞內(nèi)容"));newsProducer.sendNews("china.20190101", new News("鳳凰TV", "XXX企業(yè)榮登世界500強", new Date(), "國內(nèi)新聞內(nèi)容"));}public RabbitTemplate getRabbitTemplate() {return rabbitTemplate;}public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {this.rabbitTemplate = rabbitTemplate;}}消費者
創(chuàng)建一個MAVEN項目spring-exchange-consumer作為消息隊列的消費者
導(dǎo)入相關(guān)的依賴坐標(與生產(chǎn)者一致)
<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>創(chuàng)建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"><!--設(shè)置連接工廠,配置基本參數(shù)--><rabbit:connection-factory id="connectionFactory" host="localhost" port="5672" username="guest" password="guest" virtual-host="/test"></rabbit:connection-factory><!-- 所有產(chǎn)生的數(shù)據(jù)在rabbit可視化控制臺中展示 --><rabbit:admin connection-factory="connectionFactory"/><!-- 創(chuàng)建隊列 --><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底層自動監(jiān)聽對應(yīng)的topicQueue數(shù)據(jù),一旦有新的消息傳進來,自動傳入到consumer的recv的News參數(shù)中,之后程序?qū)ews做進一步的處理--><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>創(chuàng)建消息實體類(實體類的包名與類名必須與生產(chǎn)者一致,且必須實現(xiàn)序列化接口)
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;}}創(chuàng)建消費者類
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");} }如果消費者需要解綁之前已經(jīng)綁定的隊列,需要在RabbitMQ管理端頁面手動操作
spring整合RabbitMQ生產(chǎn)者代碼
spring整合RabbitMQ消費者代碼
總結(jié)
以上是生活随笔為你收集整理的Spring整合RabbitMQ(包含生产者和消费者)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 室内三维物体识别与姿态估计--背景
- 下一篇: gradle idea java ssm