2012-12-12 112 views
1

我是RabbitMQ的新手,我試圖將「receiveAndConvert」轉換爲自定義類型:Person.javaRabbitMQ的「receiveAndConvert」會出現ClassCastException

這裏是我的製片

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) 

注意,生產者和消費者類位於兩個不同的項目,我已經複製/粘貼的Person.java類轉移到消費項目。

配置如下:

<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> 

回答

1

它只是發生在我身上,我的Person.java類需要實現Serializable。現在起作用了。

相關問題