繁体   English   中英

Spring AMQP“无法转换类型的值”异常

[英]“Cannot convert value of type” exception with Spring AMQP

我正在尝试向RabbitMQ服务器上的直接交换发送消息。

我有一个名为“ test_queue”的队列,并与绑定到队列“ test_queue”的同名“ test_queue”直接交换。

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

<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

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

问题bingingsexchange配置内部。 所以这个块:

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

是多余的,应该是

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM