1. 程式人生 > 實用技巧 >RabbitMQ-Spring整合RabbitMQ

RabbitMQ-Spring整合RabbitMQ

一、rabbitmq.properties

rabbitmq.host=127.0.0.1
rabbitmq.port=5672
rabbitmq.username=guest
rabbitmq.password=guest
rabbitmq.virtual-host=/

二、spring-rabbitmq-producer.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:context="http://www.springframework.org/schema/context"
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/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<!--載入配置檔案-->
<context:property-placeholder location="classpath:rabbitmq.properties"/>

<!-- 定義rabbitmq connectionFactory -->
<rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
port="${rabbitmq.port}"
username="${rabbitmq.username}"
password="${rabbitmq.password}"
virtual-host="${rabbitmq.virtual-host}"/>
<!--定義管理交換機、佇列-->
<rabbit:admin connection-factory="connectionFactory"/>

<!--定義持久化佇列,不存在則自動建立;不繫結到交換機則繫結到預設交換機
預設交換機型別為direct,名字為:"",路由鍵為佇列的名稱
-->
<rabbit:queue id="spring_queue" name="spring_queue" auto-declare="true"/>

<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~廣播;所有佇列都能收到訊息~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!--定義廣播交換機中的持久化佇列,不存在則自動建立-->
<rabbit:queue id="spring_fanout_queue_1" name="spring_fanout_queue_1" auto-declare="true"/>

<!--定義廣播交換機中的持久化佇列,不存在則自動建立-->
<rabbit:queue id="spring_fanout_queue_2" name="spring_fanout_queue_2" auto-declare="true"/>

<!--定義廣播型別交換機;並繫結上述兩個佇列-->
<rabbit:fanout-exchange id="spring_fanout_exchange" name="spring_fanout_exchange" auto-declare="true">
<rabbit:bindings>
<rabbit:binding queue="spring_fanout_queue_1"/>
<rabbit:binding queue="spring_fanout_queue_2"/>
</rabbit:bindings>
</rabbit:fanout-exchange>

<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~定向;指定routingKey的佇列收到訊息 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!--定義定向交換機中的持久化佇列,不存在則自動建立-->
<rabbit:queue id="spring_direct_queue_1" name="spring_direct_queue_1" auto-declare="true"/>
<!--定義定向交換機中的持久化佇列,不存在則自動建立-->
<rabbit:queue id="spring_direct_queue_2" name="spring_direct_queue_2" auto-declare="true"/>

<!--定義定向型別交換機;並繫結上述兩個佇列-->
<rabbit:direct-exchange id="spring_direct_exchange" name="spring_direct_exchange" auto-declare="true">
<rabbit:bindings>
<rabbit:binding queue="spring_direct_queue_1" key="direct_queue_1"/>
<rabbit:binding queue="spring_direct_queue_2" key="direct_queue_2"/>
</rabbit:bindings>
</rabbit:direct-exchange>

<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~萬用字元;*匹配一個單詞,#匹配多個單詞 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!--定義萬用字元交換機中的持久化佇列,不存在則自動建立-->
<rabbit:queue id="spring_topic_queue_star" name="spring_topic_queue_star" auto-declare="true"/>
<!--定義萬用字元交換機中的持久化佇列,不存在則自動建立-->
<rabbit:queue id="spring_topic_queue_well" name="spring_topic_queue_well" auto-declare="true"/>
<!--定義萬用字元交換機中的持久化佇列,不存在則自動建立-->
<rabbit:queue id="spring_topic_queue_well2" name="spring_topic_queue_well2" auto-declare="true"/>

<rabbit:topic-exchange id="spring_topic_exchange" name="spring_topic_exchange" auto-declare="true">
<rabbit:bindings>
<rabbit:binding pattern="hello.*" queue="spring_topic_queue_star"/>
<rabbit:binding pattern="hello.#" queue="spring_topic_queue_well"/>
<rabbit:binding pattern="hi.#" queue="spring_topic_queue_well2"/>
</rabbit:bindings>
</rabbit:topic-exchange>

<!--定義rabbitTemplate物件操作可以在程式碼中方便傳送訊息-->
<rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
</beans>

三、測試類中測試傳送訊息

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
public class SpringRabbitProducer {
@Autowired
private RabbitTemplate rabbitTemplate;

/**
* 簡單模式,傳送訊息
*/
@Test
public void helloWorldTest(){
rabbitTemplate.convertAndSend("spring_queue","hello world ...");
}

/**
* fanout,傳送訊息
*/
@Test
public void fanoutTest(){
rabbitTemplate.convertAndSend("spring_fanout_exchange","","hello fanout ...");
}

/**
* direct,傳送訊息
*/
@Test
public void directTest(){
rabbitTemplate.convertAndSend("spring_direct_exchange","direct_queue_2","hello direct ...");
}

/**
* topics,傳送訊息
*/
@Test
public void topicsTest(){
rabbitTemplate.convertAndSend("spring_topic_exchange","hi.hello","hello topics");
}
}