Вопрос

I am new to RabbitMQ and I am trying to "receiveAndConvert" to a custom type of mine: Person.java.

Here is my producer:

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);
    }

}

and here is my consumer:

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());
    }

}

My Person.java is just a POJO with a name and an age instance variables.

I get a ClassCastException as follows:

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)

Note that the Producer and Consumer classes are located in two different projects and I have copied/pasted the Person.java class over to the Consumer project.

My config is as follows:

<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>
Это было полезно?

Решение

It just occurred to me that my Person.java class needs to implement Serializable. It now works.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top