javascript
rabittmq java spring_消息队列 RabbitMQ 与 Spring 整合使用的实例代码
一、什么是 RabbitMQ
RabbitMQ 是實現 AMQP(高級消息隊列協議)的消息中間件的一種,最初起源于金融系統,用于在分布式系統中存儲轉發消息,在易用性、擴展性、高可用性等方面表現不俗。消息中間件主要用于組件之間的解耦,消息的發送者無需知道消息使用者的存在,反之亦然。
RabbitMQ 是由 Erlang 語言開發,安裝 RabbitMQ 服務需要先安裝 Erlang 語言包。
二、如何與 Spring 集成
1. 我們都需要哪些 Jar 包?
拋開單獨使用 Spring 的包不說,引入 RabbitMQ 我們還需要兩個:
com.rabbitmq
amqp-client
3.5.1
org.springframework.amqp
spring-rabbit
1.4.5.RELEASE
2. 使用外部參數文件 application.properties:
mq.host=127.0.0.1
mq.username=queue
mq.password=1234
mq.port=8001
# 統一XML配置中易變部分的命名
mq.queue=test_mq
易變指的是在實際項目中,如果測試與生產環境使用的同一個 RabbitMQ 服務器。那我們在部署時直接修改 properties 文件的參數即可,防止測試與生產環境混淆。
修改 applicationContext.xml 文件,引入我們創建的 properties 文件
3. 連接 RabbitMQ 服務器
password="${mq.password}" port="${mq.port}" />
4. 聲明一個 RabbitMQ Template
5. 在 applicationContext.xml 中聲明一個交換機,name 屬性需配置到 RabbitMQ 服務器。
交換機的四種模式:
direct:轉發消息到 routigKey 指定的隊列。
topic:按規則轉發消息(最靈活)。
headers:(這個還沒有接觸到)
fanout:轉發消息到所有綁定隊列
交換器的屬性:
持久性:如果啟用,交換器將會在server重啟前都有效。
自動刪除:如果啟用,那么交換器將會在其綁定的隊列都被刪除掉之后自動刪除掉自身。
惰性:如果沒有聲明交換器,那么在執行到使用的時候會導致異常,并不會主動聲明。
如果沒有隊列綁定在交換機上,則發送到該交換機上的消息會丟失。
一個交換機可以綁定多個隊列,一個隊列可以被多個交換機綁定。
topic 類型交換器通過模式匹配分析消息的 routing-key 屬性。它將 routing-key 和 binding-key 的字符串切分成單詞。這些單詞之間用點隔開。它同樣也會識別兩個通配符:#匹配0個或者多個單詞,*匹配一個單詞。例如,binding key:*.stock.#匹配 routing key:usd.stcok 和 eur.stock.db,但是不匹配 stock.nana。
因為交換器是命名實體,聲明一個已經存在的交換器,但是試圖賦予不同類型是會導致錯誤??蛻舳诵枰獎h除這個已經存在的交換器,然后重新聲明并且賦予新的類型。
6. 在 applicationContext.xml 中聲明一個隊列,name 屬性是需要配置到 RabbitMQ 服務器的。
durable:是否持久化
exclusive:僅創建者可以使用的私有隊列,斷開后自動刪除
auto-delete:當所有消費端連接斷開后,是否自動刪除隊列
7. 創建生產者端
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @Description: 消息隊列發送者
* @Author:
* @CreateTime:
*/
@Service
public class Producer {
@Autowired
private AmqpTemplate amqpTemplate;
public void sendQueue(String exchange_key, String queue_key, Object object) {
// convertAndSend 將Java對象轉換為消息發送至匹配key的交換機中Exchange
amqpTemplate.convertAndSend(exchange_key, queue_key, object);
}
}
8. 在 applicationContext.xml 中配置監聽及消費者端
消費者 Java 代碼:
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
public class RabbitmqService implements MessageListener {
public void onMessage(Message message) {
System.out.println("消息消費者 = " + message.toString());
}
}
至此,我們的所有配置文件就寫完了,最終如下:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
password="${mq.password}" port="${mq.port}" />
9. 如何使用 RabbitMQ 發送一個消息
@Autowired
private Producer producer;
@Value("#{appConfig['mq.queue']}")
private String queueId;
/**
* @Description: 消息隊列
* @Author:
* @CreateTime:
*/
@ResponseBody
@RequestMapping("/sendQueue")
public String testQueue() {
try {
Map map = new HashMap();
map.put("data", "hello rabbitmq");
producer.sendQueue(queueId + "_exchange", queueId + "_patt", map);
} catch (Exception e) {
e.printStackTrace();
}
return "發送完畢";
}
嗯。這個測試是 SpringMVC 框架。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
總結
以上是生活随笔為你收集整理的rabittmq java spring_消息队列 RabbitMQ 与 Spring 整合使用的实例代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 装机吧u盘装系统怎么分区 U盘装系统如何
- 下一篇: java中对象的生存期_JSP中Java