首页 文章

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

提问于
浏览
0

我正在尝试向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 回答

  • 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

    问题出在 bingings 里面 exchange 配置 . 所以这块:

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

    是多余的,它应该是

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

相关问题