簡體   English   中英

具有空隊列名稱的Spring AMQP入站適配器

[英]Spring AMQP Inbound Adapter with empty queue name

我正在使用從 RabbitMQ 接收消息的Spring AMQP開發消費者應用程序。 聲明了一個主題交換。 要連接到 Rabbit,我創建了一個空名稱隊列,因為代理將提供一個自動隊列名稱,請參閱InterCor M4 升級規范混合規范

@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 入站通道適配器時

@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();
}

我收到錯誤“隊列名稱”不能為空或為空

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);
}

這樣,如果連接丟失並重新建立,隊列將獲得相同的名稱。

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的隊列:

非空隊列名稱

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM