Skip to content

Instantly share code, notes, and snippets.

@garyrussell
Created April 7, 2020 14:06
Show Gist options
  • Save garyrussell/b5ceb24d7fa985e684e5c923b5e4ef6d to your computer and use it in GitHub Desktop.
Save garyrussell/b5ceb24d7fa985e684e5c923b5e4ef6d to your computer and use it in GitHub Desktop.
Delayed Exchange
package com.example.demo;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class Rabbit1Application {
public static void main(String[] args) {
SpringApplication.run(Rabbit1Application.class, args);
}
@Bean
public DirectExchange exchange() {
return ExchangeBuilder.directExchange("delayed")
.delayed()
.build();
}
@Bean
public Queue queue() {
return QueueBuilder.durable("queue").build();
}
@Bean
public Binding binding() {
return BindingBuilder.bind(queue()).to(exchange()).with("routing.key");
}
@RabbitListener(queues = "queue")
public void listen(String in) {
System.out.println(in);
}
@Bean
public ApplicationRunner runner(RabbitTemplate template) {
return args -> template.convertAndSend("delayed", "routing.key", "foo", msg -> {
msg.getMessageProperties().setDelay(8000);
System.out.println("Sending");
return msg;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment