Reputation: 111
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
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