【问题标题】:How to specify timeout for sending message to RabbitMQ using Spring Cloud Stream?如何使用 Spring Cloud Stream 指定向 RabbitMQ 发送消息的超时时间?
【发布时间】:2019-11-28 13:18:20
【问题描述】:

我们在发送消息的过程中遇到了网络问题,这导致所有线程都处于阻塞状态。我们使用org.springframework.cloud:spring-cloud-stream:2.0.1.RELEASEorg.springframework:spring-messaging:5.0.8.RELEASE 向RabbitMQ 代理发送消息。绑定接口:

interface MessagingSource {
    @Output("bindingTargetName")
    fun messageChannelOutput(): MessageChannel
}

用法:

 val isSent = messageSource.messageChannelOutput().send(message)

MessageChannel#send(Message, long) 方法的第二个参数也有超时时间(毫秒),但在 org.springframework.integration.channel.AbstractSubscribableChannel#doSend 方法:

@Override
protected boolean doSend(Message<?> message, long timeout) { // timeout is ignored in this method
    try {
        return getRequiredDispatcher().dispatch(message);
    }
    catch (MessageDispatchingException e) {
        String description = e.getMessage() + " for channel '" + this.getFullChannelName() + "'.";
        throw new MessageDeliveryException(message, description, e);
    }
}

您能解释一下为什么忽略超时参数以及如何配置它以避免长时间阻塞状态吗?

谢谢!

【问题讨论】:

    标签: java rabbitmq spring-integration amqp spring-cloud-stream


    【解决方案1】:

    频道sendTimeout 仅适用于频道本身可以阻塞的情况,例如带有当前已满的有界队列的QueueChannel;调用者将阻塞,直到队列中有空间可用,或者发生超时。

    在这种情况下,阻塞位于通道的下游,因此 sendTimeout 无关紧要(无论如何,它是一个 DirectChannel,无论如何都不能阻塞,订阅的处理程序直接在调用线程上调用)。

    您看到的实际阻塞很可能在rabbitmq客户端中的socket.write()中,它没有超时且不可中断;调用线程无法执行任何操作来“超时”写入。

    我知道的唯一可能的解决方案是通过在连接工厂上调用resetConnection() 来强制关闭兔子连接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-12
      • 2016-09-09
      • 2022-07-01
      • 2021-05-07
      • 2016-08-23
      • 2022-10-25
      相关资源
      最近更新 更多