轮询消费者

AmqpTemplate 本身可用于轮询 Message 接收。默认情况下,如果消息不可用,则立即返回 null。没有阻塞。从 1.5 版本开始,您可以设置一个以毫秒为单位的 receiveTimeout,并且接收方法将阻塞最长达该时间,等待消息。小于零的值表示无限期阻塞(或者至少直到与代理的连接丢失)。1.6 版本引入了 receive 方法的变体,允许在每次调用时传入超时。

由于接收操作为每条消息创建一个新的 QueueingConsumer,因此此技术不适用于高吞吐量环境。对于这些用例,请考虑使用异步消费者或将 receiveTimeout 设置为零。

从 2.4.8 版本开始,当使用非零超时时,您可以指定传递给 basicConsume 方法的参数,该方法用于将消费者与通道关联。例如:template.addConsumerArg("x-priority", 10)

有四种简单的 receive 方法可用。与发送端的 Exchange 一样,有一种方法要求在模板本身上直接设置了默认队列属性,还有一种方法接受运行时队列参数。1.6 版本引入了接受 timeoutMillis 的变体,以按请求覆盖 receiveTimeout。以下列表显示了这四种方法的定义:

Message receive() throws AmqpException;

Message receive(String queueName) throws AmqpException;

Message receive(long timeoutMillis) throws AmqpException;

Message receive(String queueName, long timeoutMillis) throws AmqpException;

与发送消息的情况一样,AmqpTemplate 具有一些方便的方法,用于接收 POJO 而不是 Message 实例,并且实现提供了一种自定义用于创建返回 ObjectMessageConverter 的方法:以下列表显示了这些方法:

Object receiveAndConvert() throws AmqpException;

Object receiveAndConvert(String queueName) throws AmqpException;

Object receiveAndConvert(long timeoutMillis) throws AmqpException;

Object receiveAndConvert(String queueName, long timeoutMillis) throws AmqpException;

从 2.0 版本开始,这些方法有一些变体,它们接受一个额外的 ParameterizedTypeReference 参数来转换复杂类型。模板必须配置 SmartMessageConverter。有关更多信息,请参阅 使用 RabbitTemplateMessage 转换

sendAndReceive 方法类似,从 1.3 版本开始,AmqpTemplate 具有几个方便的 receiveAndReply 方法,用于同步接收、处理和回复消息。以下列表显示了这些方法的定义:

<R, S> boolean receiveAndReply(ReceiveAndReplyCallback<R, S> callback)
       throws AmqpException;

<R, S> boolean receiveAndReply(String queueName, ReceiveAndReplyCallback<R, S> callback)
     throws AmqpException;

<R, S> boolean receiveAndReply(ReceiveAndReplyCallback<R, S> callback,
    String replyExchange, String replyRoutingKey) throws AmqpException;

<R, S> boolean receiveAndReply(String queueName, ReceiveAndReplyCallback<R, S> callback,
    String replyExchange, String replyRoutingKey) throws AmqpException;

<R, S> boolean receiveAndReply(ReceiveAndReplyCallback<R, S> callback,
     ReplyToAddressCallback<S> replyToAddressCallback) throws AmqpException;

<R, S> boolean receiveAndReply(String queueName, ReceiveAndReplyCallback<R, S> callback,
            ReplyToAddressCallback<S> replyToAddressCallback) throws AmqpException;

AmqpTemplate 实现负责 receivereply 阶段。在大多数情况下,您应该只提供 ReceiveAndReplyCallback 的实现,以对接收到的消息执行一些业务逻辑,并在需要时构建回复对象或消息。请注意,ReceiveAndReplyCallback 可以返回 null。在这种情况下,不发送回复,receiveAndReply 的工作方式类似于 receive 方法。这允许同一队列用于混合消息,其中一些消息可能不需要回复。

仅当提供的回调不是 ReceiveAndReplyMessageCallback 的实例时,才应用自动消息(请求和回复)转换,ReceiveAndReplyMessageCallback 提供原始消息交换契约。

ReplyToAddressCallback 对于需要自定义逻辑以根据接收到的消息和 ReceiveAndReplyCallback 的回复在运行时确定 replyTo 地址的情况非常有用。默认情况下,请求消息中的 replyTo 信息用于路由回复。

以下列表显示了基于 POJO 的接收和回复示例:

boolean received =
        this.template.receiveAndReply(ROUTE, new ReceiveAndReplyCallback<Order, Invoice>() {

                public Invoice handle(Order order) {
                        return processOrder(order);
                }
        });
if (received) {
        log.info("We received an order!");
}