Rabbitmq 队列名称为空的Spring AMQP入站适配器

Rabbitmq 队列名称为空的Spring AMQP入站适配器,rabbitmq,spring-integration,spring-amqp,Rabbitmq,Spring Integration,Spring Amqp,我正在开发一个消费者应用程序,使用它从RabbitMQ接收消息。已声明主题交换。要连接到Rabbit,我创建了一个空名称的队列,因为代理将提供一个自动队列名称,请参见: 但当我尝试使用Spring Integration Java DSL配置时: @Autowired private Queue queue; @Bean public IntegrationFlow amqpInbound(ConnectionFactory connectionFactory) { return Inte

我正在开发一个消费者应用程序,使用它从RabbitMQ接收消息。已声明主题交换。要连接到Rabbit,我创建了一个空名称的队列,因为代理将提供一个自动队列名称,请参见:

但当我尝试使用Spring Integration Java DSL配置时:

@Autowired
private Queue queue;

@Bean
public IntegrationFlow amqpInbound(ConnectionFactory connectionFactory) {
  return IntegrationFlows.from(Amqp.inboundAdapter(connectionFactory, queue))
      .handle(m -> System.out.println(m.getPayload()))
      .get();
}
我收到一个错误,“queueName”不能为null或空

2018-05-25 13:39:15.080 ERROR 14636 --- [erContainer#0-1] o.s.a.r.l.SimpleMessageListenerContainer : Failed to check/redeclare auto-delete queue(s).

java.lang.IllegalArgumentException: 'queueName' cannot be null or empty
    at org.springframework.util.Assert.hasText(Assert.java:276) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:337) ~[spring-rabbit-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.redeclareElementsIfNecessary(AbstractMessageListenerContainer.java:1604) ~[spring-rabbit-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:963) [spring-rabbit-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_162]

如何将消息队列名称的值设置为空字符串?

这不是一个好的解决方案

问题是,对于代理生成的队列名称,如果连接丢失并重新建立,队列名称将更改,但容器将不知道新队列,并将尝试使用旧队列

AnonymousQueue
通过框架生成随机名称来解决这个问题

但是,匿名队列不是持久的,是独占的,并且是自动删除的

如果您想要一个具有不同属性的队列,但仍然需要一个随机名称,请使用

@Bean
public Queue queue() {
  return new Queue(new AnonymousQueue.Base64UrlNamingStrategy().generateName(),
      queueDurable, queueExclusive, queueAutoDelete, queueParameters);
}
这样,如果连接丢失并重新建立,队列将获得相同的名称。

问题已经解决,现在可以在Spring Boot中使用

更新项目的父项可修复此问题:


org.springframework.boot

非空队列名称 创建名为abc的队列:


谢谢,我同意你的意见,但我必须遵守规定使用空名称的规范。它已被更广泛的社区采用,我们无法改变它。我试图用a替换
Amqp.inboundAdapter
,并将其设置为消息侦听器,但仍然得到完全相同的异常。看起来很不幸,我将不得不使用Java客户端;请随意打开“改进”。提出了改进Jira问题。
@Bean
public Queue queue() {
  return new Queue(new AnonymousQueue.Base64UrlNamingStrategy().generateName(),
      queueDurable, queueExclusive, queueAutoDelete, queueParameters);
}
spring:
    rabbitmq:
        queue:
            name:
            durable: false
            exclusive: true
            autoDelete: true
spring:
    rabbitmq:
        queue:
            name: abc
            durable: false
            exclusive: true
            autoDelete: true