RuaTre
RuaTre

Reputation: 111

How to public message with delay time rabbitmq implement in spring boot

My case. I have queue container element with exactly time to deliver. I use rabbitmq implement by spring boot to support. Now i should send message to queue and after delay time queue will deliver message. Rabbitmq support rabbitmq-delayed-message-exchange plugin to schedule message. But i do not implement . What wrong in my code. (I enabled plugin delay)

@Bean
DirectExchange directExchange() {
    Map<String, Object> args = new HashMap<String, Object>();
    args.put("x-delayed-type", "x-delayed-message");
    return new DirectExchange("my-exchange", true, false, args);
}

@Bean
Binding binding(Queue queue, DirectExchange directExchange) {
    return BindingBuilder.bind(queue).to(directExchange).with(queueName);
}

The Post Answer button should be used only for complete answers to the question.

Upvotes: 8

Views: 8535

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121560

See the similar question with an appropriate answer.

Scheduled/Delay messaging in Spring AMQP RabbitMq

Your problem is here:

@Bean
CustomExchange delayExchange() {
    Map<String, Object> args = new HashMap<String, Object>();
    args.put("x-delayed-type", "direct");
    return new CustomExchange("my-exchange", "x-delayed-message", true, false, args);
}

From other side we have introduced the Delayed Exchange in the Spring AMQP 1.6: https://spring.io/blog/2016/02/16/spring-amqp-1-6-0-milestone-1-and-1-5-4-available.

UPDATE

The Binding should be declared as :

@Bean
Binding binding(Queue queue, Exchange delayExchange) {
    return BindingBuilder.bind(queue).to(delayExchange).with("foo").noargs();
}

To send message with the delay you should do almost the same what you have tried:

rabbitTemplate.convertAndSend("my-exchange", "spring-boot", new DaoDoa(), new MessagePostProcessor() {
        @Override
        public Message postProcessMessage(Message message) throws AmqpException {
            message.getMessageProperties().setHeader("x-delay, 15000);
            return message;
        }
    });

Upvotes: 20

Related Questions