首页 文章

具有空队列名称的Spring AMQP入站适配器

提问于
浏览
0

我正在使用Spring AMQP开发一个消费者应用程序,它接收来自RabbitMQ的消息 . 宣布了一个主题交换 . 要连接到Rabbit,我创建一个空名称的队列,因为 the broker will provide an automatic queue name ,请参阅the specs

@Bean
public TopicExchange exchange() {
    TopicExchange topicExchange = new TopicExchange(topicExchangeName);
    topicExchange.setShouldDeclare(false);
    return topicExchange;
}

@Bean
public Queue queue() {
  return new Queue("", queueDurable, queueExclusive, queueAutoDelete, queueParameters);
}

@Bean
public Binding binding(Queue queue, TopicExchange exchange) {
    return BindingBuilder.bind(queue).to(exchange).with(routingKey);
}

但是当我尝试使用Spring Integration Java DSL配置AMQP Inbound Channel Adapter时:

@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' cannot be null or empty

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]

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

2 回答

  • 4

    这不是一个好的解决方案 .

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

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

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

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

    @Bean
    public Queue queue() {
      return new Queue(new AnonymousQueue.Base64UrlNamingStrategy().generateName(),
          queueDurable, queueExclusive, queueAutoDelete, queueParameters);
    }
    

    这样,如果连接丢失并重新 Build ,队列将获得相同的名称 .

  • 1

    AMQP-816问题已得到修复,现在可以在Spring Boot 2.1.0中找到 .

    更新项目的父项可以解决问题:

    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.0.RELEASE</version>
    </parent>
    

    空队列名称

    spring:
        rabbitmq:
            queue:
                name:
                durable: false
                exclusive: true
                autoDelete: true
    

    创建一个自动队列名称 amq.gen-U1vKiSfIvy8bO11jLD29Sw

    非空队列名称

    spring:
        rabbitmq:
            queue:
                name: abc
                durable: false
                exclusive: true
                autoDelete: true
    

    创建一个名为 abc 的队列:

相关问题