引入依賴:
<!-- RabbitMQ --> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.4.0.RELEASE</version> </dependency>
配置文件:
1、生產者的配置和代碼
<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/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- 定義RabbitMQ的連接工廠 -->
<rabbit:connection-factory id="connectionFactory"
host="${rabbitmq.host}" port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}"
virtual-host="${rabbitmq.vhost}" />
<!-- 定義Rabbit模板,指定連接工廠以及定義exchange -->
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="MM_MANAGE_ITEM_EXCHANGE" />
<!-- MQ的管理,包括隊列、交換器等 -->
<rabbit:admin connection-factory="connectionFactory" />
<!-- 定義交換器,自動聲明 -->
<rabbit:topic-exchange name="MM_MANAGE_ITEM_EXCHANGE" auto-declare="true">
</rabbit:topic-exchange>
</beans>
rabbitmq.properties
rabbitmq.host=127.0.0.1
rabbitmq.port=5672
rabbitmq.username=beck
rabbitmq.password=123456
rabbitmq.vhost=/maomao
發送的代碼
// 更新成功,發送消息到mq中: 做什麼操作,對誰做了操作,什麼時候做了操作
try {
if (updateFlag){
Map<String, Object> msg = new HashMap<>();
msg.put("type", "update");
msg.put("id", item.getId());
msg.put("time", System.currentTimeMillis());
this.amqpTemplate.convertAndSend("item.update", PropertisService.MAPPER.writeValueAsString(msg));
}
} catch (Exception e) {
e.printStackTrace();
}
2、消費者的配置的代碼
<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/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- 定義RabbitMQ的連接工廠 -->
<rabbit:connection-factory id="connectionFactory"
host="${rabbitmq.host}" port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}"
virtual-host="${rabbitmq.vhost}" />
<!-- MQ的管理,包括隊列、交換器等 -->
<rabbit:admin connection-factory="connectionFactory" />
<!-- 定義隊列,自動聲明 durable:持久化 -->
<rabbit:queue name="MM_WEB_ITEM_QUEUE" auto-declare="true" durable="true"/>
<bean id="itemlistener" class="com.maomao.web.mq.ItemMqListener" />
<!-- 隊列監聽 -->
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener ref="itemlistener" method="getMessage" queue-names="MM_WEB_ITEM_QUEUE" />
</rabbit:listener-container>
</beans>
接收消息的bean
package com.maomao.web.mq;
public class ItemMqListener {
public void getMessage(String msg){
System.out.println("獲取到消息: " + msg);
}
}
3、這是訂閱者模式的消息,隊列和交換機的綁定,在rabbitmq的控制檯中操作。