当前位置: 首页 > 工具软件 > Spring AMQP > 使用案例 >

使用spring amqp 配置rabbitmq消息队列

钦德佑
2023-12-01

Rabbitmq 消息队列类型

  1. Direct Exchange(直连交换机)
  2. Fanout Exchange(扇型交换机)
  3. Topic Exchange(主题交换机)
  4. Headers Exchange(头交换机)

配置队列

    @Bean
    public Queue orderGeneratedQueue() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", ORDER_GENERATED_EXCHANGE_DLQ);
        arguments.put("x-message-ttl", TIME_TO_LIVE);
        return new Queue(ORDER_GENERATED_QUEUE, true, false, false, arguments);
    }

配置交换机

   @Bean
    public FanoutExchange orderGeneratedExchange() {
        return new FanoutExchange(ORDER_GENERATED_EXCHANGE, true, false);
    }

配置绑定关系

    @Bean
    public Binding bindingDlq() {
        return BindingBuilder.bind(orderGeneratedQueueDlq()).to(orderGeneratedExchangeDlq());
    }

接收消息

   @RabbitListener(queues = OrderGeneratedMount.ORDER_GENERATED_QUEUE_DLQ)
    public void listenDlq(OrderGeneratedPayload payload) {
        System.out.println("aaa");
    }

发送消息

    public void publisher(OrderGeneratedPayload payload) {
        rabbitTemplate.convertAndSend(OrderGeneratedMount.ORDER_GENERATED_EXCHANGE, OrderGeneratedMount.ORDER_GENERATED_EXCHANGE,payload);
    }
 类似资料: