我是 RabbitMQ 的新手,我正在尝试“ receiveAndConvert
”到我的自定义类型:Person.java
.
这是我的制片人:
public class Producer {
public static void main(String[] args) {
ApplicationContext context = new GenericXmlApplicationContext("classpath:/applicationContext.xml");
AmqpTemplate template = context.getBean(AmqpTemplate.class);
Person person = new Person();
person.setAge(37);
person.setName("Julien");
template.convertAndSend("myqueue", person);
}
}
这是我的消费者:
public class Consumer {
public static void main(String[] args) {
ApplicationContext context = new GenericXmlApplicationContext("classpath:/applicationContext.xml");
AmqpTemplate template = context.getBean(AmqpTemplate.class);
Person me = (Person) template.receiveAndConvert("myqueue");
System.out.println("Me: " + me.getName() + ":" + me.getAge());
}
}
我的Person.java 只是一个带有名称和年龄实例变量的 POJO。
我得到一个ClassCastException如下:
Exception in thread "main" java.lang.ClassCastException: [B cannot be cast to trc.suivi.amqp.Person
at trc.suivi.amqp.Consumer.main(Consumer.java:14)
请注意,Producer 和 Consumer 类位于两个不同的项目中,我已将 Person.java 类复制/粘贴到 Consumer 项目中。
我的配置如下:
<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.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<rabbit:connection-factory id="connectionFactory" />
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" />
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="myqueue" />
</beans>