Guk
Guk

Reputation: 71

Can I receive multiple messages from Spring AMQP RabbitTemplate at one request?

I want to receive multiple messages on one request from Spring AMQP RabbitTemplate (https://docs.spring.io/spring-amqp/api/org/springframework/amqp/rabbit/core/RabbitTemplate.html). But I can't find the method working this way. Can I do that in Spring AMQP?

I don't want to use RabbitListener because it consumes all the time..

Upvotes: 0

Views: 917

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121177

No, there is no such an API on the RabbitTemplate, as well as there is no way to send several message per single publish.

You may consider to use this one instead of 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;

So, you can call the Channel.basicGet() in a loop on the same opened channel.

However there is also this more higher 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 {

So, you can call RabbitOperations.receive() in a loop.

Upvotes: 2

Related Questions