Spring整合RabbitMQ

引入依赖:

<!-- 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的控制台中操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值