我可以在一个请求中接收来自Spring AMQP RabbitTemplate的多条消息吗



我想在一个请求中接收来自Spring AMQP RabbitTemplate的多条消息(https://docs.spring.io/spring-amqp/api/org/springframework/amqp/rabbit/core/RabbitTemplate.html)。但我找不到这种方法。我能在春季AMQP中做到这一点吗?

我不想使用RabbitListener,因为它消耗了所有的时间。。

不,RabbitTemplate上没有这样的API,也没有办法在每次发布时发送多条消息。

你可以考虑用这个代替receive():

/**
* Execute the callback with a channel and reliably close the channel afterwards.
* @param action the call back.
* @param <T> the return type.
* @return the result from the
* {@link ChannelCallback#doInRabbit(com.rabbitmq.client.Channel)}.
* @throws AmqpException if one occurs.
*/
@Nullable
<T> T execute(ChannelCallback<T> action) throws AmqpException;

因此,您可以在同一个打开的通道上循环调用Channel.basicGet()

然而,还有更高的API:

/**
* Invoke the callback and run all operations on the template argument in a dedicated
* thread-bound channel and reliably close the channel afterwards.
* @param action the call back.
* @param <T> the return type.
* @return the result from the
* {@link OperationsCallback#doInRabbit(RabbitOperations operations)}.
* @throws AmqpException if one occurs.
* @since 2.0
*/
@Nullable
default <T> T invoke(OperationsCallback<T> action) throws AmqpException {

因此,您可以在循环中调用RabbitOperations.receive()

最新更新