rabbitmq和spring框架集成

原文地址,转载请注明出处: http://blog.csdn.net/qq_34021712/article/details/72568179     ©王赛超

spring对amqp做了支持,目前只是做了Rabbitmq的实现。

集成后分为两个项目,一个是生产者,一个是消费者,完全模拟真实开发中应用场景,完整项目打包下载

rabbitmq-spring为生产者,rabbitmq-spring2为消费者。

1.生产者配置

rabbitmq.properties

rabbitmq.host=192.168.1.101
rabbitmq.port=5672
rabbitmq.vhost=/testrabbit
rabbitmq.username=test
rabbitmq.password=test
applicationContext-rabbitmq.xml

<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的web页面配置,
	不要写死在配置文件中,因为如果绑定的关系发生变化,就需要修改配置文件
	并且重启服务,在web页面配置不需要。
	 -->	
	
	<!-- 定义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"/>
	
	<!-- 定义一个交换机  name随便起名 ,auto-declare="true"预创建 , 持久化:durable="true" -->
	<rabbit:topic-exchange name="rabbitmq-exchange-order" auto-declare="true" durable="true">
	</rabbit:topic-exchange>
	
	<!-- 定义rabbitmq模板,指定工厂和交换机 -->
	<rabbit:template id="rabbitmqTemplate" connection-factory="connectionFactory" exchange="rabbitmq-exchange-order"/>
	
</beans>

OrderController类

package com.rabbitmq.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.rabbitmq.service.OrderService;

@Controller
@RequestMapping("order")
public class OrderController {
	
	@Autowired
	private OrderService orderService;

	/**
	 * 假如说有人在前台系统下订单,同时需要提醒物流系统 和库存系统
	 */
	@RequestMapping("create")
	public void createOrder(@RequestParam("msg")String msg){
		orderService.createOrder(msg);
	}
}
OrderServiceImpl类
package com.rabbitmq.service.impl;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rabbitmq.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService {
	
	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Override
	public void createOrder(String msg) {
		//这里业务代码就不写了,直接写几个注释得了
		
		//第一步 :订单系统生成订单
		
		//第二步:这里我们假装需要向库存系统和物流系统发送消息
		/*
		 * 参数1:routingKey(路由规则)
		 * 参数2:消息(发送的消息内容)
		 * 
		 */
		rabbitTemplate.convertAndSend("order.insert", msg);
	}

}

2.消费者配置

rabbitmq.properties

rabbitmq.host=192.168.1.101
rabbitmq.port=5672
rabbitmq.vhost=/testrabbit
rabbitmq.username=test
rabbitmq.password=test
applicationContext-rabbitmq.xml
<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的web页面配置,
	不要写死在配置文件中,因为如果绑定的关系发生变化,就需要修改配置文件
	并且重启服务,在web页面配置不需要。
	 -->	
	
	<!-- 定义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"/>
	
	<!-- 定义队列 -->
	<rabbit:queue name="rabbitmq-web-order" auto-declare="true" durable="true"/>
	
	<bean id="orderHandler" class="com.rabbitmq.service.OrderHandler"/>
	
	<!-- 定义监听 从rabbitmq-web-order队列中取消息,在orderHandler的execute方法中根据消息内容,执行业务逻辑 -->
	<rabbit:listener-container connection-factory="connectionFactory">
		<rabbit:listener ref="orderHandler" method="execute" queue-names="rabbitmq-web-order"/>
	</rabbit:listener-container>
	
</beans>
OrderHandler类

package com.rabbitmq.service;

public class OrderHandler {

	public void execute(String msg){
		//比如说这里是库存系统,下面调用接口,查询数量,然后修改库存,这里就直接打印一下消息得了
		System.out.println(msg);
	}
}

3.启动生产者和消费者



4.将交换机和队列在web控制台绑定



5.然后访问链接,查看效果



6.模拟拿到消息处理时,出现异常情况,下图是我发送了两条,所以会出现两条未处理信息




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值