java - Spring AMQP 的 "Cannot convert value of type"异常

标签 java spring rabbitmq spring-amqp

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

我有名为“test_queue”的队列和具有相同名称“test_queue”的直接交换,该交换绑定(bind)到队列“”test_queue”。

我使用 Spring AMQP 和 XML 配置来与 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>

关于java - Spring AMQP 的 "Cannot convert value of type"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37165307/

相关文章:

java - JTextArea 中的密码

java - JPA/EclipseLink 惰性集合未加载

java - Java中的客户端证书身份验证

java - 如何设置 Spring Cloud Zuul 服务器作为代理服务器

java - 如何通过java程序获取系统登录时间

c# - Rabbitmq,在发布者之前重启消费者

java - 使用音频指纹测量延迟 (Java)

java - Spring 启动 MVC : can't find JSP

python - 卡在 Celery 队列中的任务

nginx - 在 Azure Kubernetes 集群 (AKS) 上的 Nginx Ingress 上配置 TCP 端口