1

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

}

My 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クラスは2つの異なるプロジェクトにあり、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>
4

1 に答える 1

0

私のPerson.javaクラスがを実装する必要があることに気づきSerializableました。これで動作します。

于 2012-12-12T09:01:49.270 に答える