spring amqp rabbitmq 学习(三) MessageConverter

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管理界面,查看消息内容如下:


spring amqp rabbitmq 学习(三) MessageConverter
 证明发送成功,并且是以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一下,成功了

你可能感兴趣的:(spring,rabbitmq,AMQP)