引入依赖:
<!-- 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的控制台中操作。