The average queue

RabbitMqConfig

@Slf4j
@Configuration
public class RabbitMqConfig {

    final static String HELLO_QUEUE = "hello_queue";
    final static String HELLO_EXCHANGE = "hello_exchange";

    /** * switch */
    @Bean
    public DirectExchange helloExchange(a) {
        return ExchangeBuilder
                // The simplest switch type is direct
                .directExchange(HELLO_EXCHANGE)
                // Persistence (optional, default persistence)
                .durable(true)
                .build();
    }


    /** * queue */
    @Bean
    public Queue helloQueue(a) {
        return new Queue(HELLO_QUEUE);
    }

    
    /** * bind **@param helloQueue
     * @param helloExchange
     * @return* /
    @Bean
    public Binding helloBinding(Queue helloQueue, DirectExchange helloExchange) {
        returnBindingBuilder.bind(helloQueue).to(helloExchange).with(HELLO_QUEUE); }}Copy the code

MqSender

@Component
@RequiredArgsConstructor
public class MqSender {

    private final RabbitTemplate rabbitTemplate;

    /** * Send message */
    public void sendHello(String msg) { rabbitTemplate.convertAndSend( RabbitMqConfig.HELLO_EXCHANGE, RabbitMqConfig.HELLO_QUEUE, msg ); }}Copy the code

MqReceiver

@Component
@RequiredArgsConstructor
public class MqReceiver {

   @RabbitListener(queues = RabbitMqConfig.HELLO_QUEUE)
    public void helloHandle(String msg)  {
    	// TODO message processing}}Copy the code

TTL delay queue

RabbitMqConfig

@Slf4j
@Configuration
public class RabbitMqConfig {

    final static String HELLO_QUEUE = "hello_queue";
    final static String HELLO_EXCHANGE = "hello_exchange";
    final static String HELLO_TTL_QUEUE = "hello_ttl_queue";
    final static String HELLO_TTL_EXCHANGE = "hello_ttl_exchange";

	/** * TTL switch */
    @Bean
    public DirectExchange helloTtlExchange(a) {
        return ExchangeBuilder
                // The simplest switch type is direct
                .directExchange(HELLO_TTL_EXCHANGE)
                // Persistence (optional, default persistence)
                .durable(true)
                .build();
    }


    /** * TTL queue */
    @Bean
    public Queue helloTtlQueue(a) {
          return QueueBuilder.durable(HELLO_TTL_QUEUE)
                // Bind to a dead letter queue. The message will be sent to the dead letter queue when it expires
                .withArgument("x-dead-letter-exchange", HELLO_EXCHANGE)
                .withArgument("x-dead-letter-routing-key", HELLO_QUEUE)
                .build();
    }

    
    /** * TTL binding **@param helloTtlQueue
     * @param helloTtlExchange
     * @return* /
    @Bean
    public Binding helloBinding(Queue helloTtlQueue, DirectExchange helloTtlExchange) {
        return BindingBuilder.bind(helloTtlQueue).to(helloTtlExchange).with(HELLO_TTL_QUEUE);
    }



    /** * switch */
    @Bean
    public DirectExchange helloExchange(a) {
        return ExchangeBuilder
                // The simplest switch type is direct
                .directExchange(HELLO_EXCHANGE)
                // Persistence (optional, default persistence)
                .durable(true)
                .build();
    }


    /** * queue */
    @Bean
    public Queue helloQueue(a) {
        return new Queue(HELLO_QUEUE);
    }

    
    /** * bind **@param helloQueue
     * @param helloExchange
     * @return* /
    @Bean
    public Binding helloBinding(Queue helloQueue, DirectExchange helloExchange) {
        returnBindingBuilder.bind(helloQueue).to(helloExchange).with(HELLO_QUEUE); }}Copy the code

MqSender

@Component
@RequiredArgsConstructor
public class MqSender {

    private final RabbitTemplate rabbitTemplate;

    /** * Send message */
    public void sendHello(String msg) {
       // Set the delay to 1 minute in milliseconds
       long expirationTime = 1000 * 60;
       
       rabbitTemplate.convertAndSend(
               // Send to TTL
               RabbitMqConfig.HELLO_TTL_EXCHANGE,
               RabbitMqConfig.HELLO_TTL_QUEUE,
               msg,
               message -> {
                      // Set the expiration time
                      message.getMessageProperties().setExpiration(String.valueOf(delayTimes));
                      returnmessage; }); ; }}Copy the code

MqReceiver

@Component
@RequiredArgsConstructor
public class MqReceiver {
   
   @RabbitListener(queues = RabbitMqConfig.HELLO_QUEUE)
    public void helloHandle(String msg)  {
    	// TODO message processing}}Copy the code

pit

The TTL queue will block. If the advanced delay time is longer than the advanced delay time, the advanced delay will wait for the advanced delay to expire before consumption. Therefore, only the queue with fixed delay time is applied.

The rabbitmq-delayed-message-exchange plugin is required to accommodate complex times