首先需要声明消息队列以及交换机绑定 还有死信交换机,如果不需要定时任务 则不需要死信交换机
    创建配置类, 加上注解@Configuration

    1. @Configuration
    2. public class RabbitMQConfig {
    3. public static final String EX_A = "exchangeA";
    4. public static final String EX_B = "exchangeB";
    5. public static final String Q_A = "queueA";
    6. public static final String Q_B = "queueB";
    7. public static final String ROUTING_KEY_A = "exa-qa";
    8. public static final String ROUTING_KEY_B = "exb-qb";
    9. @Bean(EX_A)
    10. public Exchange EX_A(){
    11. return ExchangeBuilder.fanoutExchange(EX_A).durable(true).build();
    12. }
    13. @Bean(EX_B)
    14. public Exchange EX_B(){
    15. return ExchangeBuilder.fanoutExchange(EX_B).durable(true).build();
    16. }
    17. @Bean(Q_A)
    18. public Queue Q_A(){
    19. Queue queue = QueueBuilder.durable(Q_A).build();
    20. queue.addArgument("x-dead-letter-exchange",EX_B);
    21. return queue;
    22. }
    23. @Bean(Q_B)
    24. public Queue Q_B(){
    25. Queue queue = QueueBuilder.durable(Q_B).build();
    26. return queue;
    27. }
    28. @Bean
    29. public Binding binding_A(){
    30. return BindingBuilder.bind(Q_A()).to(EX_A()).with(ROUTING_KEY_A).noargs();
    31. }
    32. @Bean
    33. public Binding binding_B(){
    34. return BindingBuilder.bind(Q_B()).to(EX_B()).with(ROUTING_KEY_B).noargs();
    35. }
    36. }

    发送消息使用RabbitTemplate 延迟消息需要设置死信交换机.

    1. import com.heima.taskdemo.config.RabbitMQConfig;
    2. import org.springframework.amqp.core.Message;
    3. import org.springframework.amqp.core.MessageProperties;
    4. import org.springframework.amqp.rabbit.core.RabbitTemplate;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.web.bind.annotation.GetMapping;
    7. import org.springframework.web.bind.annotation.RestController;
    8. import java.text.SimpleDateFormat;
    9. import java.util.Date;
    10. @RestController
    11. public class WmNewsPublishProducer {
    12. @Autowired
    13. private RabbitTemplate rabbitTemplate;
    14. @GetMapping("/publish")
    15. public String publish(){
    16. MessageProperties messageProperties = new MessageProperties();
    17. // 设置过期时间,单位:毫秒
    18. messageProperties.setExpiration("5000");
    19. //将需要发送的转为byts 放入Message 如果是对象可以 byte[] bytes = JSONObject.toJSONBytes(wmNews);
    20. Message message = new Message("hello world".getBytes(),messageProperties);
    21. //发送时选择交换机以及队列 还有message
    22. rabbitTemplate.convertAndSend(RabbitMQConfig.EX_A,RabbitMQConfig.ROUTING_KEY_A,message);
    23. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    24. System.out.println("消息发送时间【"+simpleDateFormat.format(new Date())+"】");
    25. return "OK";
    26. }
    27. }

    使用 @RabbitListener(queues = RabbitMQConfig.Q_B) 接收消息,需要声明消息队列

    1. @Component
    2. public class PublishListener {
    3. @RabbitListener(queues = RabbitMQConfig.Q_B)
    4. public void publish(Message message){
    5. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    6. System.out.println("接收到消息【"+simpleDateFormat.format(new Date())+"】"+new String(message.getBody()));
    7. }
    8. }