How to pull messages from RabbitMQ Spring manually
<p>I have a need to pull records manually from a RabbitMQ queue, and not automatically. Below is what I have created to do so. However, I wonder if there is a better Spring/RabbitMQ way of doing this? </p>

<p>You will notice the use of channel.basicAck and channel.basicNack. I have placed these where they can acknowledge/NotAcknowledge if/when the consumer throws an exception, so that the record can stay queued.</p>

<p>I would have used RabbitTemplate.receiveAndConvert, but this would automatically acknowledge the record, even though, there could be reasons why it should not be acknowledged. </p>

<pre><code>import java.util.Properties;
import java.util.function.Consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.ChannelCallback;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.DefaultMessagePropertiesConverter;
import org.springframework.amqp.rabbit.support.MessagePropertiesConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.GetResponse;

@Service
public class RabbitAdminServices {

    private static final Logger logger = LoggerFactory.getLogger(RabbitAdminServices.class);

    @Autowired
    AmqpAdmin rabbitAdmin;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    MessageConverter messageConverter;

    private volatile MessagePropertiesConverter messagePropertiesConverter = new DefaultMessagePropertiesConverter();

    public int getCount(String queueName) {

        Properties properties = rabbitAdmin.getQueueProperties(queueName);
        return (Integer)properties.get(RabbitAdmin.QUEUE_MESSAGE_COUNT);
    }

    public &lt;T&gt; void processQueue(String queueName, Integer count, Class&lt;T&gt; clazz, Consumer&lt;T&gt; consumer) {

        int reprocessCount = getCount(queueName);
        int requestCount = reprocessCount;
        if(count != null) {
            requestCount = count;
        }
        for(int i = 0; i &lt; reprocessCount &amp;&amp; i &lt; requestCount; i++) {
            rabbitTemplate.execute(new ChannelCallback&lt;T&gt;() {

                @Override
                public T doInRabbit(Channel channel) throws Exception {
                    GetResponse response = channel.basicGet(queueName, false);
                    T result = null;
                    try {
                        MessageProperties messageProps = messagePropertiesConverter.toMessageProperties(response.getProps(), response.getEnvelope(), "UTF-8");
                        if(response.getMessageCount() &gt;= 0) {
                            messageProps.setMessageCount(response.getMessageCount());
                        }
                        Message message = new Message(response.getBody(), messageProps);
                        result = (T)messageConverter.fromMessage(message);
                        consumer.accept(result);
                        channel.basicAck(response.getEnvelope().getDeliveryTag(), false);
                    }
                    catch(Exception e) {
                        channel.basicNack(response.getEnvelope().getDeliveryTag(), false, true);
                    }
                    return result;
                }
            });

        }
    }
}
</code></pre>