user468587
user468587

Reputation: 5031

Spring amqp batch receiving messages

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

Upvotes: 5

Views: 3862

Answers (1)

Gary Russell
Gary Russell

Reputation: 174554

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.

Upvotes: 5

Related Questions