Rabbitmq 使用Spring AMQP向侦听器发送延迟消息

Rabbitmq 使用Spring AMQP向侦听器发送延迟消息,rabbitmq,spring-amqp,spring-rabbit,Rabbitmq,Spring Amqp,Spring Rabbit,我需要在一定的持续时间后将消息发送给MessageListener,所以有没有任何方法可以使用SpringAMQP实现 例如。 Producer生成消息并将消息发送到RabbitMQ Q,该消息会立即被侦听器接收到,我想延迟在用户端接收到的消息,比如说在一些配置参数之后,比如说1000ms,RabbitMQ为此提供的功能 从1.6版开始,Spring AMQP还提供了关于此问题的高级API: 更新 在Spring AMQP1.6之前,您应该这样做: @Bean CustomExchange de

我需要在一定的持续时间后将消息发送给MessageListener,所以有没有任何方法可以使用SpringAMQP实现

例如。
Producer生成消息并将消息发送到RabbitMQ Q,该消息会立即被侦听器接收到,我想延迟在用户端接收到的消息,比如说在一些配置参数之后,比如说1000ms,RabbitMQ为此提供的功能

从1.6版开始,Spring AMQP还提供了关于此问题的高级API:

更新

在Spring AMQP
1.6
之前,您应该这样做:

@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);
}

...

MessageProperties properties = new MessageProperties();
properties.setHeader("x-delay", 15000);
template.send(exchange, routingKey,
        MessageBuilder.withBody("foo".getBytes()).andProperties(properties).build());
@Bean
Queue queue() {
    return QueueBuilder.durable(queueName)
            .withArgument("x-dead-letter-exchange", dlx)
            .withArgument("x-dead-letter-routing-key", dlq)
            .build();
}


@Bean
TopicExchange exchange() {
    return (TopicExchange) ExchangeBuilder.topicExchange(topicExchangeName)
            .delayed()
            .build();

@Bean
Binding binding() {
    return BindingBuilder.bind(queue()).to(exchange()).with(queueName);
}
@Bean
CustomExchange延迟交换(){
Map args=new HashMap();
参数put(“x延迟类型”、“直接”);
返回新的CustomExchange(“我的exchange”、“x-delayed-message”、真、假、args);
}
...
MessageProperties=newmessageproperties();
setHeader(“x-delay”,15000);
发送模板(交换、路由密钥、,
MessageBuilder.withBody(“foo.getBytes()).andProperties(properties.build());

另请参见此问题及其答案:

如果使用spring boot,则可能是这样的:

@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);
}

...

MessageProperties properties = new MessageProperties();
properties.setHeader("x-delay", 15000);
template.send(exchange, routingKey,
        MessageBuilder.withBody("foo".getBytes()).andProperties(properties).build());
@Bean
Queue queue() {
    return QueueBuilder.durable(queueName)
            .withArgument("x-dead-letter-exchange", dlx)
            .withArgument("x-dead-letter-routing-key", dlq)
            .build();
}


@Bean
TopicExchange exchange() {
    return (TopicExchange) ExchangeBuilder.topicExchange(topicExchangeName)
            .delayed()
            .build();

@Bean
Binding binding() {
    return BindingBuilder.bind(queue()).to(exchange()).with(queueName);
}

这在1.4版本中可用吗?问题是什么?我说:从1.6开始。你能解释一下为什么坚持使用这样一个旧的EOL版本吗?我们肯定会升级到最新版本,但过了一段时间(2个月),因为我们有不同的产品推出策略,所以回到这个问题上来,因为我们正在开发此功能,我们有amqp 1.4.3,那么,我们在这个版本中有什么方法可以实现吗?否则,我们将不得不等待产品的升级。明天我将与您分享一些东西。是的,即使没有Spring高级API,也肯定有一个解决延迟交换的技巧。请在我的答案中查找更新