Code for delay queue SpringBoot RabbitMQ

Scenes

After a single user, 30min if not paid, then delete the order, this time you can delay queue

 

ready

Use rabbitmq_delayed_message_exchange plug;

First, download the plug-in: https://www.rabbitmq.com/community-plugins.html

The plug is then installed into the directory rabbitmq plugins;

Into sbin directory, execute "rabbitmq-plugins.bat enable rabbitmq_delayed_message_exchange";

 

Close RabbitMQ service, and then start (immediate restart of the plug-in might not take effect).

 

SpringBoot RabbitMQ Code

application.properties profile

spring.application.name=spring-boot-rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=nut
spring.rabbitmq.password=nut

 

Configuration class

Note that the " the X--DELAYED-of the type " and " the X--DELAYED-the Message "

/**
 * 延迟队列配置exchange
 */
@Configuration
public class DelayQueueConfig {

    public static final String DELAY_EXCHANGE = "DELAY_EXCHANGE";
    public static final String DELAY_QUEUE = "DELAY_QUEUE";
    public static final String DELAY_ROUTING_KEY = "DELAY_ROUTING_KEY";

    @Bean("delayExchange")
    public Exchange delayExchange() {
        Map<String, Object> args = new HashMap<>(1);
//       x-delayed-type delay declared type of queue Exchange 
        args.put ( " x-delayed-type ", "Direct" );
         return  new new CustomExchange (DELAY_EXCHANGE, "X-DELAYED-Message" , to true , to false , args); 
    } 

    @Bean ( "DelayQueue" )
     public queue DelayQueue () {
         return QueueBuilder.durable (DELAY_QUEUE) .build (); 
    } 

    / ** 
     * bound to the queue delay by the delay switch routingKey 
     * 
     * @return 
     * / 
    @Bean 
    public delayQueueBindExchange the Binding () {
         return new Binding(DELAY_QUEUE, Binding.DestinationType.QUEUE, DELAY_EXCHANGE, DELAY_ROUTING_KEY, null);
    }

}

 

Producers

When sending a message, to specify the delay in milliseconds

/ ** 
 * sender delay queue 
 * / 
@Component 
@ SLF4J 
public  class DelayQueueSender { 

    @Autowired 
    Private RabbitTemplate rabbitTemplate; 

    public  void sendDelayQueue ( int Number) { 
        log.warn ( "Send Delay Queue: {} milliseconds" , Number);
         / / Exchange herein may be Exchange service, in order to facilitate testing herein was directly delivered badmail Exchange message 
        rabbitTemplate.convertAndSend ( 
                DelayQueueConfig.DELAY_EXCHANGE, 
                DelayQueueConfig.DELAY_ROUTING_KEY, 
                Number, (message) -> { 
                    // set the delay in milliseconds
                    message.getMessageProperties().setDelay(number);
                    log.info("Now : {}", ZonedDateTime.now());
                    return message;
                });
    }
}

 

consumer

/**
 * 延迟队列消费者
 */
@Component
@Slf4j
@RabbitListener(queues = DelayQueueConfig.DELAY_QUEUE)
public class DelayQueueConsumer {

    @RabbitHandler
    public void receiveDelayMessage(Integer milliseconds){
        log.warn("DelayQueueConsumer Time : {}, and the millis : {}", ZonedDateTime.now(), milliseconds);

    }

}

 

test

First start the project;

Then sends a message in the test class;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqApplicationTests {

    @Autowired
    private DelayQueueSender delayQueueSender;


    @Test
    public void testDelayQueueSender(){
        delayQueueSender.sendDelayQueue(5000);
    }
}

 

Send Message window:

 

Consumers receive messages:

 

Showing that the delay interval transmission completion queue!

 

reference:

https://blog.csdn.net/linsongbin1/article/details/80178122

https://blog.csdn.net/youjin/article/details/82586888

https://docs.spring.io/spring-amqp/docs/2.0.0.M2/reference/htmlsingle/#delayed-message-exchange

https://www.rabbitmq.com/blog/2015/04/16/scheduling-messages-with-rabbitmq/

 

Guess you like

Origin www.cnblogs.com/theRhyme/p/10986409.html