Andremoniy
Andremoniy

Reputation: 34920

"Cannot convert value of type" exception with Spring AMQP

I'm trying to send message to my direct exchange on RabbitMQ server.

I have queue named as "test_queue" and direct exchange with same name "test_queue" which is bound to the queue ""test_queue".

I use Spring AMQP with XML configuration for interacting with Rabbit MQ, and here is my 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>

And I'm trying to send message this way:

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

Exception which I got:

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

Upvotes: 0

Views: 1834

Answers (2)

Gary Russell
Gary Russell

Reputation: 174749

It's not really good practice to name an exchange with _queue.

You generally need to give the elements unique ids otherwise the last bean definition wins...

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

Upvotes: 3

Andremoniy
Andremoniy

Reputation: 34920

The problem was with the bingings inside exchange configuration. So this block:

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

is superfluous, it just should be

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

Upvotes: 1

Related Questions