RabbitMQ Spring Boot

447 阅读2分钟

普通队列

RabbitMqConfig

@Slf4j
@Configuration
public class RabbitMqConfig {

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

    /**
     * 交换机
     */
    @Bean
    public DirectExchange helloExchange() {
        return ExchangeBuilder
                // 交换机类型用最简单的 direct
                .directExchange(HELLO_EXCHANGE)
                // 持久化(可不设置,默认持久化)
                .durable(true)
                .build();
    }


    /**
     * 队列
     */
    @Bean
    public Queue helloQueue() {
        return new Queue(HELLO_QUEUE);
    }

    
    /**
     * 绑定
     *
     * @param helloQueue
     * @param helloExchange
     * @return
     */
    @Bean
    public Binding helloBinding(Queue helloQueue, DirectExchange helloExchange) {
        return BindingBuilder.bind(helloQueue).to(helloExchange).with(HELLO_QUEUE);
    }

}

MqSender

@Component
@RequiredArgsConstructor
public class MqSender {

    private final RabbitTemplate rabbitTemplate;

    /**
     * 发送消息
     */
    public void sendHello(String msg) {
        rabbitTemplate.convertAndSend(
                RabbitMqConfig.HELLO_EXCHANGE,
                RabbitMqConfig.HELLO_QUEUE,
                msg
           );
    }
}

MqReceiver

@Component
@RequiredArgsConstructor
public class MqReceiver {

   @RabbitListener(queues = RabbitMqConfig.HELLO_QUEUE)
    public void helloHandle(String msg)  {
    	// TODO 消息处理
    }
}

TTL延时队列

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交换机
     */
    @Bean
    public DirectExchange helloTtlExchange() {
        return ExchangeBuilder
                // 交换机类型用最简单的 direct
                .directExchange(HELLO_TTL_EXCHANGE)
                // 持久化(可不设置,默认持久化)
                .durable(true)
                .build();
    }


    /**
     * TTL队列
     */
    @Bean
    public Queue helloTtlQueue() {
          return QueueBuilder.durable(HELLO_TTL_QUEUE)
                // 绑定死信队列, 消息到期发送到死信队列
                .withArgument("x-dead-letter-exchange", HELLO_EXCHANGE)
                .withArgument("x-dead-letter-routing-key", HELLO_QUEUE)
                .build();
    }

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



    /**
     * 交换机
     */
    @Bean
    public DirectExchange helloExchange() {
        return ExchangeBuilder
                // 交换机类型用最简单的 direct
                .directExchange(HELLO_EXCHANGE)
                // 持久化(可不设置,默认持久化)
                .durable(true)
                .build();
    }


    /**
     * 队列
     */
    @Bean
    public Queue helloQueue() {
        return new Queue(HELLO_QUEUE);
    }

    
    /**
     * 绑定
     *
     * @param helloQueue
     * @param helloExchange
     * @return
     */
    @Bean
    public Binding helloBinding(Queue helloQueue, DirectExchange helloExchange) {
        return BindingBuilder.bind(helloQueue).to(helloExchange).with(HELLO_QUEUE);
    }

}

MqSender

@Component
@RequiredArgsConstructor
public class MqSender {

    private final RabbitTemplate rabbitTemplate;

    /**
     * 发送消息
     */
    public void sendHello(String msg) {
       // 设置延迟1分钟, 时间单位毫秒
       long expirationTime = 1000 * 60;
       
       rabbitTemplate.convertAndSend(
               // 发送给TTL
               RabbitMqConfig.HELLO_TTL_EXCHANGE,
               RabbitMqConfig.HELLO_TTL_QUEUE,
               msg,
               message -> {
                      // 设置过期时间
                      message.getMessageProperties().setExpiration(String.valueOf(delayTimes));
                      return message;
               });
         );
    }
}

MqReceiver

@Component
@RequiredArgsConstructor
public class MqReceiver {
   
   @RabbitListener(queues = RabbitMqConfig.HELLO_QUEUE)
    public void helloHandle(String msg)  {
    	// TODO 消息处理
    }
}

TTL队列会有阻塞,如果先进的延迟时间比后进的时间要长,后进的要等先进的到期才能消费,所以只适用了固定延迟时间的队列。

适配复杂时间需要配合rabbitmq-delayed-message-exchange插件使用