SpringMVC整合RabbitMQ

一、SpringMVC框架搭建请参照,SpringMVC框架搭建

二、SpringMVC整合RabbitMQ

  1. 添加RabbitMQ的jar包,在pom.xml中添加:

     <dependency>
     	<groupId>org.springframework.amqp</groupId>
     	<artifactId>spring-rabbit</artifactId>
     	<version>2.1.2.RELEASE</version>
     </dependency>
    
  2. src/main/resource 新建rabbit.properties,配置内容如下:

     # rabbit ip
     rabbit.host=127.0.0.1
     # rabbit 端口
     rabbit.port=5672
     # rabbit 用户名
     rabbit.username=admin
     # rabbit 密码
     rabbit.password=admin
    
  3. src/main/resource下新建spring-rabbit-send.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"
         xmlns:context="http://www.springframework.org/schema/context"
     	xsi:schemaLocation="http://www.springframework.org/schema/rabbit
     	http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
         http://www.springframework.org/schema/context      
         http://www.springframework.org/schema/context/spring-context-4.0.xsd 
     	http://www.springframework.org/schema/beans
     	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
      
         <context:property-placeholder location="classpath:rabbit.properties" ignore-unresolvable="true" />
     	<!-- 配置连接工厂 -->
     	<rabbit:connection-factory id="connectionFactory"
     		host="${rabbit.host}" port="${rabbit.port}" username="${rabbit.username}" password="${rabbit.password}" />
      
     	<!-- 定义mq管理 -->
     	<rabbit:admin connection-factory="connectionFactory" />
      
     	<!-- 声明队列 -->
     	<rabbit:queue name="queue" auto-declare="true" durable="true" />
      
     	<!-- 定义交换机绑定队列(路由模式) -->
     	<rabbit:direct-exchange name="IExchange" id="IExchange">
     		<rabbit:bindings>
     			<rabbit:binding queue="queue" key="queuekey" />
     		</rabbit:bindings>
     	</rabbit:direct-exchange>
      
     	<!-- 消息对象json转换类 -->
     	<bean id="jsonMessageConverter"
     		class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />
      
     	<!-- 定义模版 -->
     	<rabbit:template id="rabbitTemplate"
     		connection-factory="connectionFactory" exchange="IExchange"
     		message-converter="jsonMessageConverter" />
      
     </beans>
    
  4. src/main/resource下新建spring-rabbit-recv.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"
         xmlns:context="http://www.springframework.org/schema/context"
     	xsi:schemaLocation="http://www.springframework.org/schema/rabbit
     	http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
         http://www.springframework.org/schema/context      
         http://www.springframework.org/schema/context/spring-context-4.0.xsd 
     	http://www.springframework.org/schema/beans
     	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
      
      
         <context:property-placeholder location="classpath:rabbit.properties" ignore-unresolvable="true" />
         
     	<!-- 配置连接工厂 -->
     	<rabbit:connection-factory id="connectionFactory"
     		host="${rabbit.host}" port="${rabbit.port}" username="${rabbit.username}" password="${rabbit.password}" />
      
     	<!-- 定义mq管理 -->
     	<rabbit:admin connection-factory="connectionFactory" />
      
     	<!-- 声明队列 -->
     	<rabbit:queue name="queue" auto-declare="true" durable="true" />
      
     	<!-- 定义消费者 -->
     	<bean name="queuehandler" class="com.climber.rabbitmq.RecvHandler" />
      
     	<!-- 定义消费者监听队列 -->
     	<rabbit:listener-container
     		connection-factory="connectionFactory">
     		<rabbit:listener ref="queuehandler" queues="queue" />
     	</rabbit:listener-container>
      
     </beans>
    
  5. 定义RecvHandler类,具体代码如下:

     public class RecvHandler implements MessageListener {
     
     	private static final ObjectMapper MAPPER = new ObjectMapper();
     
     	public void onMessage(Message msg) {
     		try {
     			// msg就是rabbitmq传来的消息,需要的同学自己打印看一眼
     			// 使用jackson解析
     			JsonNode jsonData = MAPPER.readTree(msg.getBody());
     			System.out.println("我是可爱的小猪,我的id是" + jsonData.get("id").asText() + ",我的名字是" + jsonData.get("name").asText());
     
     		} catch (IOException e) {
     			e.printStackTrace();
     		}
     	}
     
     }
    
  6. 定义Controller层:

     @Controller
     public class RabbitController {
     
     	@Autowired
     	private RabbitTemplate rabbitTemplate;
     	
     	@RequestMapping(value="rabbit",method=RequestMethod.GET)
     	public void rabbit(){
     		
     		HashMap<String, String> map = new HashMap<String, String>();
     		map.put("id", "1");
     		map.put("name", "pig");
     		//根据key发送到对应的队列
     		rabbitTemplate.convertAndSend("queuekey", map);
     		
     		map.put("id", "2");
     		map.put("name", "cat");
     		//根据key发送到对应的队列
     		rabbitTemplate.convertAndSend("queuekey", map);
     		
     	}
     	
     }
    
  7. 进入浏览器,输入:http://localhost:8080/rabbitmq/rabbit.do,可看到控制台打印从RabbitMQ获取的信息

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值