Search code examples
rabbitmqamqpspring-amqp

RabbitMQ - How to override the replyTimeout for sendAndReceive?


I am configuring the amqp template using the spring amqp definition like

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

Now, while calling the amqpTemplate.sendAndReceive("COR.QUEUE", message), can I change the replyTimeout for specific requests?


Solution

  • You cannot change the timeout for individual send operations; it is a constant value.

    If you only have a few different values you need, you can simply declare multiple templates, each with a different timeout.

    You could also create a wrapper class that creates multiple templates on-demand, one for each requested timeout.

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