RabbitMQ - 如何覆盖 sendAndReceive 的 replyTimeout?

RabbitMQ - How to override the replyTimeout for sendAndReceive?

我正在使用 spring amqp 定义配置 amqp 模板,例如

<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" reply-timeout="45000" />

现在,在调用 amqpTemplate.sendAndReceive("COR.QUEUE", message) 时,我可以更改特定请求的 replyTimeout 吗?

您不能更改单个发送操作的超时;它是一个常数值。

如果您只有几个不同的值需要,您可以简单地声明多个模板,每个模板都有不同的超时。

您还可以创建一个包装器 class,它按需创建多个模板,每个请求超时一个。

private final Map<Long, RabbitTemplate> templates = new HashMap<>();

public Message sendAndReceive(String rk, Message message, long timeout) {
    // lookup a template for the requested timeout, or add one to the map
    return lookedupTemplate.sendAndReceive(rk, message);
}