4
votes

We use Spring AMQP to read message from RabbitMQ, right now we only read one message at a time off the queue, is there anyway I can read multiple messages off the queue then process the batch?

I see there is a BatchingStrategy available in Spring, how can i plugin that one into the connectionFactory?

here is my code:

CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);

SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        JsonMessageConverter converter = new JsonMessageConverter();
        DefaultClassMapper defaultClassMapper = new DefaultClassMapper();
        defaultClassMapper.setDefaultType(Message.class);
        converter.setClassMapper(defaultClassMapper);
        factory.setMessageConverter(converter);
        factory.setConcurrentConsumers(3);

...

public class Processor implements IChannelProcessor {
@Override
    public void process(Message message) {
        validateMessageEvent(message);
        // process the message
1

1 Answers

3
votes

The BatchingStrategy is for putting multiple message segments into a single amqp message; the container automatically debatches such messages. It won't help for your purposes.

To do what you want; instead of using POJO messaging (@RabbitListener) you would have to use a message listener container with acknowledgeMode set to MANUAL with a ChannelAwareMessageListener.

When you have processed your batch of messages, call basicAck on the channel to ack all the messages in the batch.