"无法转换类型" Spring AMQP的异常

时间:2016-05-11 14:13:28

标签: java spring rabbitmq spring-amqp

我试图将消息发送到RabbitMQ服务器上的直接交换。

我的队列命名为" test_queue"和同名的直接交换" test_queue"它绑定到队列"" test_queue"。

我使用带有XML配置的Spring AMQP与Rabbit MQ进行交互,这是我的conf:

<rabbit:template id="amqpTemplate"
                 connection-factory="rabbitConnectionFactory"
                 exchange="test_queue"/>

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

<rabbit:queue name="test_queue"/>

<rabbit:connection-factory id="rabbitConnectionFactory" (...)/>

<rabbit:direct-exchange name="test_queue">
    <rabbit:bindings>
        <rabbit:binding queue="test_queue"/>
    </rabbit:bindings>
</rabbit:direct-exchange>

我试图以这种方式发送消息:

AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
RabbitTemplate template = (RabbitTemplate) ctx.getBean("amqpTemplate");
template.convertAndSend("Hello, world!");

我得到的例外:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.amqp.rabbit.config.BindingFactoryBean#0': Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.springframework.amqp.core.DirectExchange' to required type 'org.springframework.amqp.core.Queue' for property 'destinationQueue'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.amqp.core.DirectExchange] to required type [org.springframework.amqp.core.Queue] for property 'destinationQueue': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:753)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:835)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at rabbitmq.TestRabbitMq.main(TestRabbitMq.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.springframework.amqp.core.DirectExchange' to required type 'org.springframework.amqp.core.Queue' for property 'destinationQueue'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.amqp.core.DirectExchange] to required type [org.springframework.amqp.core.Queue] for property 'destinationQueue': no matching editors or conversion strategy found
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:591)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:603)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:204)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1527)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1486)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    ... 16 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [org.springframework.amqp.core.DirectExchange] to required type [org.springframework.amqp.core.Queue] for property 'destinationQueue': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:302)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:576)
    ... 22 more

2 个答案:

答案 0 :(得分:3)

_queue命名交换并不是一个好习惯。

您通常需要为元素提供唯一ID,否则最后一个bean定义会赢...

<rabbit:queue id="queue" name="test_queue"/>

<rabbit:connection-factory id="rabbitConnectionFactory" (...)/>

<rabbit:direct-exchange id="exchange" name="test_queue">
    <rabbit:bindings>
        <rabbit:binding queue="queue"/>
    </rabbit:bindings>
</rabbit:direct-exchange>

答案 1 :(得分:1)

问题在于Databingings配置。所以这个块:

exchange

是多余的,它应该是

<rabbit:bindings>
    <rabbit:binding queue="test_queue"/>
</rabbit:bindings>