我很難找到Spring AMQP/Rabbit MQ中計劃/延遲消息的方式。
經過多次搜索後,我仍然無法在Spring AMQP中做到這一點。有人可以告訴我如何在Spring AMQP中使用x-delay。
我想延遲消息,如果消費者一方發生某種異常。 RabbitMQ的說要添加X-延遲和安裝的我已經有了這樣的插件,但還是消息不延時Spring AMQP中的計劃/延遲消息傳遞RabbitMq
我得到這消息
收到<(身體立刻正在添加:「[B @ 60a4ae5f (字節[26])'MessageProperties [頭= {X-延遲= 15000}
@Bean
ConnectionFactory connectionFactory(){
CachingConnectionFactory connectionFactory=new CachingConnectionFactory("127.0.0.1");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
connectionFactory.setPort(1500);
connectionFactory.setPublisherReturns(true);
return connectionFactory;
}
@Bean
Binding binding(@Qualifier("queue")Queue queue, DirectExchange exchange) {
return new Binding(queue.getName(), Binding.DestinationType.QUEUE, exchange.getName(), queue.getName(), null);
//return BindingBuilder.bind(queue).to(exchange).with(queueName);
}
@Bean
DirectExchange exchange() {
DirectExchange exchange=new DirectExchange("delay-exchange");
return exchange;
}
消費者---
@Override
public void onMessage(Message message, Channel channel) throws Exception {
System.out.println("Received <" + message+ ">" +rabbitTemplate);
if(i==1){
AMQP.BasicProperties.Builder props = new AMQP.BasicProperties.Builder();
Map<String,Object> headers = message.getMessageProperties().getHeaders();
headers.put("x-delay", 15000);
props.headers(headers);
i++;
channel.basicPublish(message.getMessageProperties().getReceivedExchange(), message.getMessageProperties().getReceivedRoutingKey(),
props.build(), message.getBody());
}
}
不確定是誰推薦你用'jms'標記標記這個問題,但這是不正確的。這只是關於RabbitMQ。是的,'spring-amqp'。正在修復... –