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.
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.
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.
What do you mean "auto-creating" queues? RabbitMQ doesn't create them, the application code does.
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;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With