spring amqp rabbitmq 学习(三) MessageConverter

本文介绍如何在Spring AMQP中使用Jackson2JsonMessageConverter将Java对象转换为JSON格式的消息发送到RabbitMQ,以及如何配置接收端解析这些JSON消息。

spring amqp默认使用的是SimpleMessageConverter,使用的是UTF-8编码,官网原文是这样说的

It handles text-based content, serialized Java objects,and simple byte arrays.

当contentType是以text开头的时候,它会使用UTF-8编码将消息转换为String类型

当contentType是application/x-java-serialized-object时,它会将消息进行解序列化

 

JsonMessageConverter、Jackson2JsonMessageConverter将对象转换为json传递给rabbitmq

 

首先发送信息的applicationContext-send-messageConverter.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
	xsi:schemaLocation="http://www.springframework.org/schema/beans    
      http://www.springframework.org/schema/beans/spring-beans.xsd   
      http://www.springframework.org/schema/context   
      http://www.springframework.org/schema/context/spring-context.xsd   
      http://www.springframework.org/schema/aop   
      http://www.springframework.org/schema/aop/spring-aop.xsd   
      http://www.springframework.org/schema/tx   
      http://www.springframework.org/schema/tx/spring-tx.xsd
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/rabbit
	  http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd">

	<rabbit:connection-factory id="connectionFactory" host="192.168.1.175" port="5672" channel-cache-size="25" />

	<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" routing-key="simpleSend" message-converter="jackson2JsonMessageConverter"/>

	<rabbit:admin connection-factory="connectionFactory" />
	
	<rabbit:queue name="simpleSend"/>
	
	<bean id="jackson2JsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter"/>

</beans>

 主要是增加了jackson2JsonMessageConverter

 

测试发送消息

@Test
	public void testSend() throws InterruptedException {
		
		rabbitTemplate.convertAndSend(new Order(1, "hello"));
	}

 打开rabbitmq管理界面,查看消息内容如下:



 证明发送成功,并且是以json的方式

 

接下来接收消息

applicationContext-receive-messageConverter.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
	xsi:schemaLocation="http://www.springframework.org/schema/beans    
      http://www.springframework.org/schema/beans/spring-beans.xsd   
      http://www.springframework.org/schema/context   
      http://www.springframework.org/schema/context/spring-context.xsd   
      http://www.springframework.org/schema/aop   
      http://www.springframework.org/schema/aop/spring-aop.xsd   
      http://www.springframework.org/schema/tx   
      http://www.springframework.org/schema/tx/spring-tx.xsd
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/rabbit
	  http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd">

	<rabbit:connection-factory id="connectionFactory" host="192.168.1.175" port="5672" channel-cache-size="25" />

	<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" queue="simpleSend" message-converter="jackson2JsonMessageConverter"/>

	<rabbit:admin connection-factory="connectionFactory" />
	
	<rabbit:queue name="simpleSend"/>
	
	<bean id="jackson2JsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter"/>

</beans>

 

测试代码:

@Test
	public void testReceive() throws InterruptedException {
		Object object=rabbitTemplate.receiveAndConvert();
		if(object!=null){
			log.info(object.toString());
			Order order=(Order)object;
			log.info(order.getName());
		}else{
			log.info("no msg!");
		}
	}

 debug一下,成功了

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值