For the latest stable version, please use Spring AMQP 3.2.0!spring-doc.cn

Delayed Message Exchange

Version 1.6 introduces support for the Delayed Message Exchange Pluginspring-doc.cn

The plugin is currently marked as experimental but has been available for over a year (at the time of writing). If changes to the plugin make it necessary, we plan to add support for such changes as soon as practical. For that reason, this support in Spring AMQP should be considered experimental, too. This functionality was tested with RabbitMQ 3.6.0 and version 0.0.1 of the plugin.

To use a RabbitAdmin to declare an exchange as delayed, you can set the delayed property on the exchange bean to true. The RabbitAdmin uses the exchange type (Direct, Fanout, and so on) to set the x-delayed-type argument and declare the exchange with type x-delayed-message.spring-doc.cn

The delayed property (default: false) is also available when configuring exchange beans using XML. The following example shows how to use it:spring-doc.cn

<rabbit:topic-exchange name="topic" delayed="true" />

To send a delayed message, you can set the x-delay header through MessageProperties, as the following examples show:spring-doc.cn

MessageProperties properties = new MessageProperties();
properties.setDelay(15000);
template.send(exchange, routingKey,
        MessageBuilder.withBody("foo".getBytes()).andProperties(properties).build());
rabbitTemplate.convertAndSend(exchange, routingKey, "foo", new MessagePostProcessor() {

    @Override
    public Message postProcessMessage(Message message) throws AmqpException {
        message.getMessageProperties().setDelay(15000);
        return message;
    }

});

To check if a message was delayed, use the getReceivedDelay() method on the MessageProperties. It is a separate property to avoid unintended propagation to an output message generated from an input message.spring-doc.cn