RabbitMQ - 如何覆盖 sendAndReceive 的回复超时?

Mah*_*ath 4 amqp rabbitmq spring-amqp

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

<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" reply-timeout="45000" />
Run Code Online (Sandbox Code Playgroud)

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

Gar*_*ell 5

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

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

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

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);
}
Run Code Online (Sandbox Code Playgroud)