Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 436
RuaTre Avatar asked Feb 24 '16 03:02

RuaTre


People also ask

How do you delay a message on RabbitMQ?

To delay a message, the user must publish it with the x-delay header, which accepts an integer representing the number of milliseconds the message should be delayed by RabbitMQ. It's worth noting that delay in this context means delaying message routing to queues or other exchanges.

What is delayed exchange in RabbitMQ?

The RabbitMQ delayed exchange plugin is used to implement a wait time between when a message reaches the exchange and when it is delivered to a queue. Every time a message is published, an offset in milliseconds can be specified.

Does RabbitMQ automatically create queues?

What do you mean "auto-creating" queues? RabbitMQ doesn't create them, the application code does.


1 Answers

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;
        }
    });
like image 111
Artem Bilan Avatar answered Jan 11 '23 15:01

Artem Bilan