Demo

<?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:context="http://www.springframework.org/schema/context"
	xmlns:task="http://www.springframework.org/schema/task" xmlns:aop="http://www.springframework.org/schema/aop"
	   xmlns:rabbit="http://www.springframework.org/schema/rabbit"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
			http://www.springframework.org/schema/rabbit
                           http://www.springframework.org/schema/rabbit/spring-rabbit-1.1.xsd
			http://www.springframework.org/schema/beans
			http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
			http://www.springframework.org/schema/context
			http://www.springframework.org/schema/context/spring-context-3.2.xsd
			http://www.springframework.org/schema/task
			http://www.springframework.org/schema/task/spring-task-3.2.xsd
			http://www.springframework.org/schema/aop
			http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
			http://www.springframework.org/schema/tx
			http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
			http://www.springframework.org/schema/mvc
			http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

	<bean id="rabbitConnectionFactory"
		  class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
		<constructor-arg value="10.0.0.11"/>
		<property name="username" value="guest"/>
		<property name="password" value="guest"/>
	</bean>

	<bean id="converter"
		  class="org.springframework.amqp.support.converter.JsonMessageConverter">
		<property name="defaultCharset" value="UTF-8"/>
	</bean>

	<rabbit:template id="rabbitTemplate"
					 connection-factory="rabbitConnectionFactory" message-converter="converter" />

	<rabbit:admin connection-factory="rabbitConnectionFactory"/>

	<rabbit:queue name="ttt.ttt.tt.tt"/>

</beans>

Java文件

package yourcompany;

import com.yourcompany.pojo.ParamsPojo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by yang on 15-11-9.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-test.xml")

public class HelloTest {

    @Autowired
    private RabbitTemplate template;

    @Test
    public void run() {
        ParamsPojo pp = new ParamsPojo();
        pp.setCuid(222222222);
        template.convertAndSend("ttt.ttt.tt.tt", pp);
    }

    @Test
    public void run2() {
        ParamsPojo pp = (ParamsPojo) template.receiveAndConvert("ttt.ttt.tt.tt");
        System.out.println(pp.getCuid());
    }
}

关于序列化的选择问题

<rabbit:template id="rabbitTemplate" connection-factory="rabbitConnectionFactory" message-converter="converter" />

这一行的配置,会创建一个org.springframework.amqp.rabbit.core.RabbitTemplate对象,默认情况下的MessageConverter,通过源码可知就是private volatile MessageConverter messageConverter = new SimpleMessageConverter();

关于MessageConveter的类型:

org.springframework.amqp.support.converter.SimpleMessageConverter

这个是使用JDK默认的序列化来序列化Message的.(默认就是它)

org.springframework.amqp.support.converter.JsonMessageConverter

这个是使用Jacson工具来进行Message对象的JSON化的.

org.springframework.amqp.support.converter.Jackson2JsonMessageConverter

这个是使用Jacson 2 工具来进行message对象的json化的.

org.springframework.amqp.support.converter.MarshallingMessageConverter

这个是XML与对象互转的converter

org.springframework.amqp.support.converter.SerializerMessageConverter

这个是使用Spring框架内的序列化接口来做的.

序列化: org.springframework.core.serializer.DefaultSerializer,使用的是ObjectOutputStream来写入OutputStream 反序列化:org.springframework.core.serializer.DefaultDeserializer,使用的是ObjectInputStream来读取InputStream.

使用默认的这两个Serializer时,本质上与JDK自带的序列化行为是相同的。通过源码可看到:

	public void serialize(Object object, OutputStream outputStream) throws IOException {
		if (!(object instanceof Serializable)) {
			throw new IllegalArgumentException(getClass().getSimpleName() + " requires a Serializable payload " +
					"but received an object of type [" + object.getClass().getName() + "]");
		}
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
		objectOutputStream.writeObject(object);
		objectOutputStream.flush();
	}

它必须要实现Serializable接口.

建议

使用JSON的方式来进行序列化Message,这样子通过RabbitMQ管理后台,也可以了解队列的内容。如果用JDK自带的方式来序列化,这样子可查找问题的时候,难以定位数据.