rabbitmq本身支持消息推送,不仅仅是poll模式

本文介绍了RabbitMQ除了poll模式外,如何通过订阅(subscription)方式高效接收消息。通过实现Consumer接口,消息将在到达时自动推送,而无需显式请求。此外,讨论了DefaultConsumer的使用、消息确认、消费者取消以及多消费者线程调度等关键概念。
摘要由CSDN通过智能技术生成

1.  poll方式接受消息




      QueueingConsumer consumer = new QueueingConsumer(channel);
      channel.basicConsume(queueName, true, consumer);


      while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        String routingKey = delivery.getEnvelope().getRoutingKey();


        System.out.println(" [x] Received '" + routingKey + "':'" + message + "'");   
      }
    }


2. 订阅或者事件驱动方式接受消息


Receiving messages by subscription

import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;

The most efficient way to receive messages is to set up a subscription using the Consumer interface. The messages will then be delivered automatically as they arrive, rather than having to be explicitly requested.

When calling the API methods relating to Consumers, individual subscriptions are always referred to by their consumer tags, which can be either client- or server-generated as explained in 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值