【问题标题】:How to configure Spring's Rabbit Template to throw exception on timeout如何配置 Spring 的 Rabbit 模板以在超时时抛出异常
【发布时间】:2021-09-25 16:13:07
【问题描述】:

我已经在谷歌上搜索了一段时间,但找不到解决问题的方法。 问题在于 RabbitTemplate 方法的默认行为,即convertSendAndReceive() 和convertSendAndReceiveAsType()。

当您调用这些方法并且它们没有被处理和回复(默认直接回复 queue=amq.rabbitmq.reply-to)时,RabbitTemplate 只会返回 null 响应,而不是指示该消息未得到回复。
当您在队列中发送几乎空的正文并期望收到类似用户的书籍或类似内容时,这一点非常重要,null 响应您无法判断用户是否没有书籍或消息是否没有及时处理。

示例调用

final List<String> messages = rabbitTemplate.convertSendAndReceiveAsType("getMessagesQueue", 0, new ParameterizedTypeReference<>() {});

我找到了一个解决方法 - 使用 AsyncRabbitTemplate,因为它的 RabbitConverterFuture 在方法 .get(timeout) 上抛出异常,但这不是我的首选。我不想使用 AsyncRabbitTemplate 来获取未处理消息的通知。

例子

final AsyncRabbitTemplate.RabbitConverterFuture<List<String>> messages = asyncRabbitTemplate.convertSendAndReceiveAsType("getMessagesQueue", 0, new ParameterizedTypeReference<>() {});

try {
    messages.get(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
    // message not processed
}

我的问题是如何配置 RabbitTemplate(配置模板本身,而不是用方面、装饰器、代理或类似的方式包装模板调用)实际上抛出一些异常而不是返回空值。

【问题讨论】:

    标签: spring spring-boot rabbitmq spring-amqp spring-rabbit


    【解决方案1】:

    目前没有这样的功能;随时在 GitHub 上打开新功能请求。 https://github.com/spring-projects/spring-amqp/issues

    【讨论】:

      猜你喜欢
      • 2014-04-25
      • 2020-03-28
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-16
      • 2011-04-11
      • 1970-01-01
      相关资源
      最近更新 更多